Fix API server foreground service lifecycle#29
Open
thereisnotime wants to merge 1 commit intotechjarves:mainfrom
Open
Fix API server foreground service lifecycle#29thereisnotime wants to merge 1 commit intotechjarves:mainfrom
thereisnotime wants to merge 1 commit intotechjarves:mainfrom
Conversation
The local API server was not activating the foreground service when started, causing it to be killed by Android when the app moved to the background. This is inconsistent with model loading and downloads, which already use WakelockService to stay alive. - Call WakelockService.enableForInference() when the HTTP server binds successfully so the foreground service keeps the process alive - Call WakelockService.disable() when the server stops - Fix onClose() to properly await stop() (was fire-and-forget, risking the HttpServer not closing before process teardown)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The local API server does not activate the Android foreground service when started. This causes it to be killed by the OS when the user switches to another app, making the "serve the local API to other apps" use case (already mentioned in the battery optimisation prompt) non-functional in practice.
The
onClose()method also callsstop()without awaiting it — sincestop()isasync, theHttpServermay not finish closing before the process tears down.Changes
start(): callWakelockService.enableForInference()after the server binds successfully, consistent with how model loading already keeps the process alivestop(): callWakelockService.disable()when the server shuts downonClose(): makeasyncandawait stop()so the server closes cleanly on service disposalThe wakelock calls are wrapped in a try/catch so that environments where
WakelockServiceis unavailable degrade gracefully.Testing
Start the API server, switch to another app (or lock the screen), verify the server remains reachable at
localhost:4891.