Skip to content

fix(client/android): prevent ForegroundServiceDidNotStartInTimeException on autostart - #2879

Open
sentry[bot] wants to merge 3 commits into
masterfrom
seer/fix/android-fgs-exception
Open

sentry[bot] wants to merge 3 commits into
masterfrom
seer/fix/android-fgs-exception

Conversation

@sentry

@sentry sentry Bot commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

This PR builds upon #2886 to further harden the VpnTunnelService's foreground service lifecycle, preventing ForegroundServiceDidNotStartInTimeException and ensuring robust notification handling.

While #2886 correctly moved the startForegroundWithNotification() call to the beginning of auto-connect paths, this PR addresses additional edge cases where startForeground() could still fail or be rejected by the system, leading to crashes or silent failures.

Key changes include:

  • Guaranteed Notification Icon: Introduced getSmallIconResId() to ensure that all notifications (primary and fallback) always have a valid small icon. This prevents startForeground() from failing if the app's small_icon drawable is missing (e.g., in Capacitor builds).
  • FLAG_IMMUTABLE for PendingIntent: Added PendingIntent.FLAG_IMMUTABLE to the PendingIntent created for the notification. This is required for apps targeting Android 12 (API 31) and above, preventing getActivity() from throwing and thus ensuring the notification can be built successfully.
  • Retry Mechanism for startForeground(): Implemented startForegroundCompat() which attempts to call startForeground(). If the initial attempt with the primary notification fails (e.g., due to system rejection), the service now retries once with a minimal fallback notification. This ensures the startForegroundService() contract is met, even if the primary notification cannot be displayed.
  • Android 8-9 Compatibility: Ensured startForeground() is correctly called without a service type for Android versions below API 29, addressing a previous oversight where it was skipped entirely on these versions.
  • Improved Error Logging: All failures related to building or starting the foreground notification are now logged at SEVERE level, providing better visibility into potential issues.

Fixes OUTLINE-CLIENTS-C2NDW

@sentry <feedback>: Autofix iterates on these changes
@sentry stop iterating: Autofix stops iterating on this run

This PR was automatically generated by Sentry at no cost. You can adjust this setting at any time.

@sentry
sentry Bot requested a review from a team as a code owner October 5, 2026 23:04
@github-actions github-actions Bot added the size/S label Oct 5, 2026
@greptile-apps

greptile-apps Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

RetriggerConfidence Score: 4/5

[Medium risk] Changes VPN service startup behavior on device boot.

This PR is not ready to merge while a rejected foreground notification can still cause the boot reconnect crash.

Findings

  1. P1 Boot reconnect can still crash ▶

Summary

Android automatic reconnects now enter the foreground before trying to start a tunnel, then stop if reconnect fails. Notification setup also has a fallback for automatic starts.

  • Automatic reconnects show a notification first and stop when they fail.
Diagram
%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Boot or app update] --> B[startForegroundService]
  B --> C[Build primary notification]
  C -->|Build fails| D[Build fallback]
  C -->|Build succeeds| E[startForeground]
  D --> E
  E -->|Rejected| F[Log and continue]
  E -->|Accepted| G[Try tunnel]
  F --> G
  G -->|Tunnel succeeds| H[Service keeps running]
  G -->|Tunnel fails| I[Stop service]
Loading

Reviews (1) · Last reviewed commit: "fix(client/android): prevent ForegroundS..."

@github-actions github-actions Bot added size/M and removed size/S labels Oct 5, 2026
@fortuna

fortuna commented Oct 9, 2026

Copy link
Copy Markdown
Collaborator

@sentry This has conflicts that need to be resolved. Also, isn't it already fixed by #2886?

Co-authored-by: sentry[bot] <39604003+sentry[bot]@users.noreply.github.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant