Skip to content

[v1.1] Bugfix: Delay in ServerService start causes it to crash #18

@patrickd-

Description

@patrickd-

Expected task duration: 1 hour
Reward for accepted PR: US$30

Summary

Some users are having trouble sharing files because the app appears to crash immediately. Thanks to crashlogs we know that the most likely culprit is that the system is killing the app because the service doesn't call the startForeground() function in time – it needs to be called within 5 seconds of calling startForegroundService(). It seems that we're not always achieving this at the moment because the app is handling the information of the item being shared first – because it needs that information to pass it along with the notification to be shared when calling startForeground(). But instead, to stay within the time limit, we can call the function with a placeholder notification and update it with the proper information later.

Problem Description

In #11 users reported that sharing from the Owner profile appears to crash the app.

type: crash
osVersion: google/tokay/tokay:16/BP2A.250705.008/2025073000:user/release-keys
package: digital.ventral.ips:1, targetSdk 34
process: digital.ventral.ips
processUptime: 5066 + 532 ms
installer: app.accrescent.client

android.app.RemoteServiceException$ForegroundServiceDidNotStartInTimeException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{6ede8c5 u0 digital.ventral.ips/.ServerService c:digital.ventral.ips}
	at android.app.ActivityThread.generateForegroundServiceDidNotStartInTimeException(ActivityThread.java:2400)
	at android.app.ActivityThread.throwRemoteServiceException(ActivityThread.java:2368)
	at android.app.ActivityThread.-$$Nest$mthrowRemoteServiceException(Unknown Source:0)
	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2750)
	at android.os.Handler.dispatchMessage(Handler.java:110)
	at android.os.Looper.loopOnce(Looper.java:248)
	at android.os.Looper.loop(Looper.java:338)
	at android.app.ActivityThread.main(ActivityThread.java:9106)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:593)
	at com.android.internal.os.ExecInit.main(ExecInit.java:50)
	at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
	at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:371)

Thankfully the crash reason is quite clear: "ForegroundServiceDidNotStartInTimeException: Context.startForegroundService() did not then call Service.startForeground()"

Based on this, it's clear that the work happening within onStartCommand() before startForeground() is called can sometimes take too long.

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {

            // A BUNCH OF STUFF HAPPENING THAT COULD TAKE TO LONG SOMETIMES

            // ...

            startForeground(NOTIFICATION_ID, createNotification())

    }

Implementation Instructions

We can prevent this issue by immediately calling startForeground(), and deal with the fact that the sharing notification's data isn't available yet by starting with a placeholder notification which is then replaced by the proper notification.

What the placeholder contains is likely not important as it will likely be only visible for a few seconds in the worst case. It could simply say "Sharing service is starting..."

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
            startForeground(NOTIFICATION_ID, createPlaceholderNotification())

            // A BUNCH OF STUFF HAPPENING THAT COULD TAKE TO LONG SOMETIMES

            // ...

            // Replace current notification with real one from createNotification()

    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions