Skip to content

Fix API server foreground service lifecycle#29

Open
thereisnotime wants to merge 1 commit intotechjarves:mainfrom
thereisnotime:fix/api-server-foreground-service
Open

Fix API server foreground service lifecycle#29
thereisnotime wants to merge 1 commit intotechjarves:mainfrom
thereisnotime:fix/api-server-foreground-service

Conversation

@thereisnotime
Copy link
Copy Markdown

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 calls stop() without awaiting it — since stop() is async, the HttpServer may not finish closing before the process tears down.

Changes

  • start(): call WakelockService.enableForInference() after the server binds successfully, consistent with how model loading already keeps the process alive
  • stop(): call WakelockService.disable() when the server shuts down
  • onClose(): make async and await stop() so the server closes cleanly on service disposal

The wakelock calls are wrapped in a try/catch so that environments where WakelockService is unavailable degrade gracefully.

Testing

Start the API server, switch to another app (or lock the screen), verify the server remains reachable at localhost:4891.

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant