amplify-android: TransferService causes ForegroundServiceDidNotStartInTimeException

Before opening, please confirm:

Language and Async Model

Java

Amplify Categories

Authentication, Storage

Gradle script dependencies

implementation "com.amplifyframework:core:1.36.4"
implementation "com.amplifyframework:aws-storage-s3:1.36.4"
implementation "com.amplifyframework:aws-auth-cognito:1.36.4"


Environment information

------------------------------------------------------------
Gradle 7.0.2
------------------------------------------------------------

Build time:   2021-05-14 12:02:31 UTC
Revision:     1ef1b260d39daacbf9357f9d8594a8a743e2152e

Kotlin:       1.4.31
Groovy:       3.0.7
Ant:          Apache Ant(TM) version 1.10.9 compiled on September 27 2020
JVM:          1.8.0_301 (Oracle Corporation 25.301-b09)
OS:           Mac OS X 10.16 x86_64


Please include any relevant guides or documentation you’re referencing

No response

Describe the bug

Hi!

One of the latest versions 1.36.4 causes fatal exceptions. The cause is obvious from the crash description - Context.startForegroundService() did not then call Service.startForeground() … com.amazonaws.mobileconnectors.s3.transferutility.TransferService

ActivityThread.java line 2147
android.app.ActivityThread.throwRemoteServiceException

Fatal Exception: android.app.ForegroundServiceDidNotStartInTimeException
Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{2637b1f u0 com.example.package/com.amazonaws.mobileconnectors.s3.transferutility.TransferService}

We’ve observed the crash on Android 9, 11, 12.

And the following list of devices at the moment: Galaxy S20+ 5G Galaxy A13 5G Galaxy A13 Galaxy S10 moto g stylus 5G (2022) Lenovo Tab M10 (FHD) Galaxy A12

We’ve reverted back to one the previous versions for now, so actually there might be more of them. The data is obtained from Firebase crash reports.

Reproduction steps (if applicable)

Use amplifyframework v1.36.4

Code Snippet

// Initialization in Application class

LogFactory.setLevel(LogFactory.Level.OFF);

Amplify.addPlugin(new AWSCognitoAuthPlugin());
Amplify.addPlugin(new AWSS3StoragePlugin());

AmplifyConfiguration amplifyConfig = AmplifyConfiguration.builder(context)
		.devMenuEnabled(false)
		.build();
Amplify.configure(amplifyConfig, context);



// Uploading, somewhere in the app

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.US);
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));

Map<String, String> metadata = new ArrayMap<>(4);
metadata.put("date", dateFormat.format(date));
metadata.put("token", authToken);

StorageUploadFileOptions options =
		StorageUploadFileOptions.builder()
				.accessLevel(StorageAccessLevel.PUBLIC)
				.contentType(mimeType)
				.metadata(metadata)
				.build();

Amplify.Storage.uploadFile(
		"gl_" + new Date().getTime() + "_" + fileName,
		file,
		options,
		result -> {
			// write success logs
		},
		failure -> {
			// write failure logs
		}
);

Log output

for Android 12

ActivityThread.java line 2147
android.app.ActivityThread.throwRemoteServiceException

Fatal Exception: android.app.ForegroundServiceDidNotStartInTimeException
Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{2637b1f u0 com.example.package/com.amazonaws.mobileconnectors.s3.transferutility.TransferService}

android.app.ActivityThread.throwRemoteServiceException (ActivityThread.java:2147)
android.app.ActivityThread.access$2900 (ActivityThread.java:310)
android.app.ActivityThread$H.handleMessage (ActivityThread.java:2376)
android.os.Handler.dispatchMessage (Handler.java:106)
android.os.Looper.loopOnce (Looper.java:226)
android.os.Looper.loop (Looper.java:313)
android.app.ActivityThread.main (ActivityThread.java:8663)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:567)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1135)

-----

for Android 9

ActivityThread.java line 1749
android.app.ActivityThread$H.handleMessage

Fatal Exception: android.app.RemoteServiceException
Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{ca31769 u0 com.example.package/com.amazonaws.mobileconnectors.s3.transferutility.TransferService}

android.app.ActivityThread$H.handleMessage (ActivityThread.java:1749)
android.os.Handler.dispatchMessage (Handler.java:106)
android.os.Looper.loop (Looper.java:193)
android.app.ActivityThread.main (ActivityThread.java:6758)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:493)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:858)


amplifyconfiguration.json

No response

GraphQL Schema

// Put your schema below this line


Additional information and screenshots

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 31 (15 by maintainers)

Most upvoted comments

@tylerjroach The issue seems to have gone away; I tested on my physical phone, which crashed every time I tried.

Thanks

Hi @yaroslav-v and @cmllamosas, Amplify Android 1.37.1 has been released. We have reworked how the ForegroundService notification is created and it no longer uses the startForegroundService method that was causing problems. Let us know if this release resolves the problems you were seeing.

@yaroslav-v Are these transfers you are initiating downloadFile, uploadFile, or uploadInputStream, or a combination of multiple transfer types? If you would like to talk more synchronously about the instances of the crash that you are seeing, I would love to continue chatting on our Discord (https://discord.com/invite/amplify).

@yaroslav-v Thank you for the update.

I believe you may be running into a scenario where startForegroundService is called on the TransferService. This should trigger TransferService.onStartCommand, which calls the required startForeground method. In the time it takes to move from startForegroundService to startForeground, it is possible that all existing transfers complete between that time, and stopForeground ends up being called.

This is a case we will be working on a fix for.

We have a call scheduled with @cmllamosas today to research if he is running into a different scenario.

@cmllamosas I can see 2 instances where TransferService is being moved to the foreground and then stopped. A pdf download and a failed image download. If there were any more transfer requests than those two, then we may be close to identifying the issue. If it were only those two transfer requests, it’s difficult to see the reason for the crash, as I can point to 2 calls to us calling startForeground, and 2 service stops.

Would you be willing to join our discord (https://discord.com/invite/amplify) and reach out to me there? Maybe we can set up a Video chat where we can take a look at your specific setup and see what we can discover.

@yaroslav-v, let me know if you see the crash again, or if the reason for the crash you observed may be debug app related as discussed.