-
-
Notifications
You must be signed in to change notification settings - Fork 693
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground() #549
Comments
Thank for reporting this @Borna-f 😉 Can you replicate it in an Android instrumented test? |
When making a Pull Request (and running workflows) the android-tests (probably also sometimes) fails with the following error: net.gotev.uploadservice.BinaryUploadTests > successfulBinaryUploadAfterOneRetry[test(AVD) - 10] FAILED
FAILURE: Build failed with an exception.
So somewhere the services are having issues, causing the app to crash. Is that helpful/enough information? |
Just to clarify. It is not specified to the specific test mentioned in the previous comment. This crash can happen at any task. i.e. here is another log I've got from another task Task :uploadservice:connectedDebugAndroidTest net.gotev.uploadservice.BinaryUploadTests > userCancelledBinaryUpload[test(AVD) - 10] FAILED
FAILURE: Build failed with an exception.
I see my S3 name somewhere there. Can it be something related to my newly added module? |
Update: Maybe this solution worth a shot? |
@Borna-f Thank you for the details 😉
I also spot this happening very rarely on CI builds, but always thought it to be some random CI problem, like the emulator disappearing unexpectedly sometimes during the execution 😄 What I can't still understand is how come that error happens after the upload is finished. It would have made much more sense to me to debug it if it happened at the start. We should find a way to replicate this in the tests. It may be a service lifecycle implementation problem.
In this case though you get this error after the upload is finished. I thought about the following two cases:
Let's dive in the code and continue from there.
android-upload-service/uploadservice/src/main/java/net/gotev/uploadservice/UploadService.kt Lines 145 to 165 in 2f179c3
android-upload-service/uploadservice/src/main/java/net/gotev/uploadservice/UploadService.kt Lines 167 to 187 in 2f179c3
In this case: Given Upload Service is not started and you are using default settings in UploadServiceConfig
When you perform a single upload with a single file, using either binary or multipart
Then the upload is performed and the service is shut down 10s after finishing the upload task the following happens at a lifecycle level starting from UploadService (skipping previous execution stages from intent creation to service invocation on purpose as the problem is not there):
|
A couple of items A) In the workflows it is crashing in the start of the upload (service lifecycle implementation problem.) So I think for that if you can call the services.startForeground() explicitly after context.startForegroundService(), that would make the problem less occurring based on the answered stackoverflow I shared earlier? I face this error frequency the other way around. The error happens to me like very frequent and I can say only sometimes my build passes 😃 B) My uploads are indeed fast. Like in few KB and they end less than 1 seconds! I see in taskCompleted you have stopForeground(true). Can this be related to that? C) At the end, when I review the example I shared on the first comment, this is happening when the task is being shutdown after the 10 seconds timeout. So maybe the task is trying to shutdown while it was already shut down? I don't know a lot about services (and also the details of your implementation of services) so I am just trying to give thoughts and possible scenarios that may happen. Maybe fixing some of these (i.e. issues that may rise on the start of the upload due to Service Lifecycle Problem) would fix the other ones as well? Let me know what you think. Thanks. I still owe you test scenarios but I thought maybe these would give you enough clues for debug. |
…fication, to prevent android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
@Borna-f many different scenarios can happen. Let's start tackling the first one 😉 and then see how it goes. Look at the |
…fication, to prevent android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground() (#551)
… notification, to prevent android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground() (gotev#551)
… notification, to prevent android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground() (gotev#551)
* gotev#549: startForeground immediately in onCreate with a placeholder notification, to prevent android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground() (gotev#551) * fix: changes assert condition to be lower or equal for interrupted uploads Co-authored-by: Alex Gotev <alex@gotev.net>
The observed pattern leading to the
Before 629ddc7 the startForeground happened only in After many unsuccessful retries to create a unit test which replicates this behavior systematically, I haven't found evidence that the problem is due to something in Upload Service's code. Perhaps it is a runtime scenario which happens when the workload of the device is heavy at that specific moment, slowing down the single Service lifecycle method invocation. With the proposed technique, this scenario should not cause exceptions anymore. |
Got some more insights on this: https://stackoverflow.com/a/50691494 Other devs reported this sequence:
If this is the sequence leading to a crash, I don't think there's something which can be done in the library to prevent it, as it's a Google Design Flaw. |
We are also still seeing this error sometimes (even at |
@mlazowik thank you for the link! This would change the lifecycle and maybe some mechanisms. I would love to see a PR and continue the discussion on it 😉 |
Great! Someone from my company should deliver in the coming weeks. Do you think this issue should be reopened? |
@mlazowik great! Waiting for the PR then. If it gets too cumbersome or troublesome, I'm also considering moving away from the Service architecture entirely and use WorkManager instead |
Hey, has there been any movement on this? I not really an Android dev but I'd happy to take a stab at it. This error has been popping up in our logs a lot and I've been tasked to do something about it. |
You can go ahead |
I think #578 and #579 are the best mitigations possible at the current state of the art. There's no way to fully prevent the RemoteServiceException from happening: #579 (comment) but at least now it's confined to the following use-case:
Only the switch to WorkManager could fully prevent those crashes. |
Hopefully newly released |
Thank you @xOIBrandon and @gotev! |
Hi,
I sometimes get the following error when the upload finishes and it crashes my app. Any idea or help? The last part after --- is the error. I brought the previous logs to get you an idea of when it does happen. (It is related to uploadservice/UploadService).
Thanks
I/UploadService: UploadService - (uploadId: N/A) - Service is about to be stopped because idle timeout of 10s has been reached
I/UploadService: NotificationActionsObserver - (uploadId: N/A) - unregistered
I/UploadService: UploadService - (uploadId: N/A) - Stopping foreground execution
I/UploadService: UploadService - (uploadId: N/A) - UploadService destroyed
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: myApp, PID: 5855
android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{5986d70 u0 myApp/net.gotev.uploadservice.UploadService}
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2182)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:8107)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
The text was updated successfully, but these errors were encountered: