Skip to content

Frequent logouts & Sync drops (HTTP 499) in background despite "Unrestricted" battery settings #231

@robotter112

Description

@robotter112

Title: [Bug/Android] Frequent logouts & Sync drops (HTTP 499) in background despite "Unrestricted" battery settings

Description
I am running a custom Matrix provider (Synapse) for the Grid app. While the iOS app runs perfectly stable, the Android app (Pixel 9 Pro XL / Pixel 7) frequently and randomly logs me out completely, forcing me back to the login screen.

This happens specifically when the phone is locked, put away, or the app is moved to the background. It appears that Android's background management kills the /sync connection (resulting in an HTTP 499 on the server). Instead of gracefully catching the timeout and reconnecting when waking up, the app invalidates the session entirely and logs the user out.

Crucial Context:
This happens despite granting the app "Allow all the time" for location services and explicitly setting Android Battery Optimization to "Unrestricted".

Environment

  • App Version: (Bitte hier deine Grid-App Version eintragen)
  • OS: Android 14 (Pixel 9 Pro XL)
  • Server: Synapse v1.152.0 (Custom Provider routed through a Cloudflare Tunnel)
  • Comparison: The exact same account/setup works flawlessly on iOS without any background logouts.

Steps to Reproduce

  1. Set up a custom Synapse homeserver.
  2. Log into the Grid App on Android using the custom provider URL.
  3. Grant "Allow all the time" location permissions and set Battery Optimization to "Unrestricted".
  4. Lock the screen and put the phone away for a while, letting Android background management (Doze) kick in.
  5. Open the app again.
  6. The app has lost the connection and completely logged out the user.

Expected Behavior
When Android puts the app to sleep or kills the idle /sync connection (timeout=30000), the app should catch the network drop. Upon waking up or via a background worker, it should silently re-establish the /sync request without destroying the user session.

Actual Behavior
The client unexpectedly kills the connection before the server responds (HTTP 499), and the app logs the user out. Upon immediate manual re-login, the server throws HTTP 400 errors because the app tries to upload One-Time-Keys that were already uploaded right before the background crash.

Server Logs (Synapse)

1. The connection drop when the phone goes to sleep (Client hangs up / HTTP 499):

2026-05-06 15:14:37,107 - synapse.http.site - 553 - INFO - GET-113 - Connection from client lost before response was sent
2026-05-06 15:14:39,599 - synapse.access.http.8008 - 643 - INFO - GET-113 - 213.211.xxx.xxx - 8008 - {@robotter112:mygrid.fastm.de} Processed request: 29.844sec/-2.492sec ru=(0.001sec, 0.000sec) db=(0.000sec/0.000sec/0) 0B 499! "GET /_matrix/client/v3/sync?filter=0&since=s119_144_0_1_1_1_15_23_0_1_2_1&timeout=30000 HTTP/1.1" "Dart/3.11 (dart:io)"

2. The side-effect upon manual re-login (HTTP 400 - Keys already exist):

2026-05-06 15:05:31,092 - synapse.http.server - 131 - INFO - POST-9 - <XForwardedForRequest at 0x1544fe5a91d0 method='POST' uri='/_matrix/client/v3/keys/upload' clientproto='HTTP/1.1' site='8008'> SynapseError: 400 - One time key signed_curve25519:AAAAAAAAAAE already exists. Old key: [...]

Additional Context
It feels like the Dart/Flutter Matrix SDK on Android doesn't properly utilize Android's Foreground Services or WakeLocks to keep the sync alive, and handles the resulting network loss as a fatal auth error.

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