fix(service): prevent notification flickering when auto-tunnel is active#1196
Open
naonak wants to merge 1 commit intowgtunnel:masterfrom
Open
fix(service): prevent notification flickering when auto-tunnel is active#1196naonak wants to merge 1 commit intowgtunnel:masterfrom
naonak wants to merge 1 commit intowgtunnel:masterfrom
Conversation
Two separate issues fixed: 1. Race condition in foreground service lifecycle (REMOVE vs DETACH): onDestroy() was calling STOP_FOREGROUND_REMOVE which globally cancels the notification ID, potentially removing a notification already posted by a newly started service instance. Changed to STOP_FOREGROUND_DETACH in both AutoTunnelService and BaseTunnelForegroundService.onDestroy(). The intentional stop path (stop()) still uses STOP_FOREGROUND_REMOVE before stopSelf() to cleanly remove the notification. 2. Notification drawer re-layout causing visual flicker: The statsJob was updating the VPN notification every second via notify(). On Android 16, each update triggers a re-layout of the notification shade, causing both the VPN and auto-tunnel notifications to visually jump when stacked. Fixed by: - Using startForeground() instead of notify() (correct API for FGS) - Only updating when traffic stats actually change - Reducing interval from 1s to 5s Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.
Summary
Fix two issues causing the "Tunnel running" notification (ID 100) to flicker visually when auto-tunnel is active (I have Android 16).
Problem
When both the VPN notification (ID 100) and the auto-tunnel notification (ID 122) are visible in the notification shade simultaneously:
Race condition on service lifecycle:
onDestroy()inAutoTunnelServiceandBaseTunnelForegroundServicecalledSTOP_FOREGROUND_REMOVE, which globally cancels the notification ID. During a rapid stop/start cycle (e.g. network transition), this could remove a notification already posted by the newly started service instance, causing Android 16 to kill the foreground service that lost its notification.Notification drawer flicker: The
statsJobupdated the VPN notification every second viaNotificationManager.notify(). Each update triggers a full re-layout of the notification shade. With two stacked notifications from the same app, this causes both to visually jump every second.Solution
onDestroy()in both services to useSTOP_FOREGROUND_DETACHinstead ofSTOP_FOREGROUND_REMOVE. The intentional stop path (stop()) still usesSTOP_FOREGROUND_REMOVEbeforestopSelf()to cleanly remove the notification.statsJobfromNotificationManager.notify()toServiceCompat.startForeground()(correct API for updating a foreground service notification).Technical Design
Files changed:
AutoTunnelService.kt: AddedSTOP_FOREGROUND_REMOVEinstop()beforestopSelf(); changedonDestroy()toSTOP_FOREGROUND_DETACH.BaseTunnelForegroundService.kt: ChangedonDestroy()toSTOP_FOREGROUND_DETACH; refactoredstatsJobto usestartForeground(), conditional updates, and 5s interval.Before / After (
statsJob):Test Plan