Repository navigation
fix(android): tighten VPN session lifecycle reliability - #187
therealaleph merged 1 commit into
Conversation
|
Reviewed locally — five clean reliability fixes, low blast radius (Android-only), good rationale on each. Merging. Verified:
Each fix makes sense:
Releasing as v1.6.1 (patch — all fixes, no new behavior). [reply via Anthropic Claude | reviewed by @therealaleph] |
Five small but real Android-only fixes: 1. Connect/Disconnect button gated on VpnState.isRunning state-flow with 12s backstop, replacing the fixed 2s transitionCooldown timer. Closes the race where a tap-after-Stop hit "Address already in use" because the previous teardown's listener-socket release wasn't done. 2. Tun2proxy.stop() wrapped in 2s join() — if the native call hangs, bounded teardown still releases the listener port instead of holding the teardown thread. 3. fd-leak fixed between parcelFd.detachFd() and Thread.start(): an OOM-thrown Thread.start used to orphan the detached fd. Now adopted into a fresh ParcelFileDescriptor purely so we can close() it. 4. Misleading teardown doc-comment rewritten — the "step 2 closes the TUN fd to force EBADF on read" claim has been factually wrong since detachFd landed. 5. Recursive crash trap: Log.e in MhrvApp's uncaught handler now wrapped in try/catch so a logd failure during exception logging falls through to the previous handler with the real exception. No Rust changes; 98 lib + 22 tunnel-node tests still pass. Local Android build verified, APK installed on mhrv_test emulator, launches cleanly with v1.6.1 in title. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
What's fixed
1. Connect button could fire while previous Stop was still tearing down
The Compose Connect/Disconnect button was gated on a fixed 2s
transitionCooldowntimer. Worst-case teardown is around 9s (Tun2proxy.stop()+ 4s tun2proxy join + 5srt.shutdown_timeout), so users could tap Connect after the cooldown expired but before the previous Stop had released the listener socket.Native.startProxythen failed with "Address already in use" and the UI looked stuck.The button now suspends on
VpnState.isRunninguntil the service actually flips the flag, with a 12swithTimeoutOrNullbackstop in case the service fails before flipping (e.g.,establish()returned null). Side benefit: same gate also debounces the rapid-tap EGL renderer crash the old timer was guarding against.2.
Tun2proxy.stop()had no timeoutThe teardown thread trusted the JNI call to return quickly. Wrapped it in a side thread with a 2s
join(). If the native call hangs, we log and proceed — the bounded tun2proxy join + boundedrt.shutdown_timeoutbelow it still release the listener port.3. fd leak window between
parcelFd.detachFd()andThread.start()Once
detachFd()returned, ownership transferred toTun2proxy.run'scloseFdOnDrop. IfThread.start()threw (OOM under memory pressure), nobody owned the fd and it leaked for the life of the process. Now wrapped in try/catch; on failure we adopt the int fd into a freshParcelFileDescriptorpurely so we canclose()it, then the half-started service is torn down cleanly (stopProxy,stopForeground,stopSelf).4. Misleading teardown doc-comment
The doc-block claimed step 2 ("close the TUN fd") forces tun2proxy's
read()to return EBADF. That call has been a no-op sincedetachFdwas added — the real exit mechanism is the cooperativeTun2proxy.stop()plus the bounded runtime shutdown. Rewrote the doc-block to call out the bound on each step, and corrected the inline step-2 comment so future debuggers don't chase a phantom safety net.5. Recursive crash trap in MhrvApp's uncaught handler
The default-uncaught-exception handler called
Log.eunguarded. If logging itself threw (logd dead, OOM in message format), we'd recurse into our own handler with a half-handled original exception. Wrapped theLog.ein try/catch so the previous handler still fires with the real failure.Files touched
android/app/src/main/java/com/therealaleph/mhrv/MhrvApp.kt— guardLog.ein uncaught handlerandroid/app/src/main/java/com/therealaleph/mhrv/MhrvVpnService.kt— boundedTun2proxy.stop(), fd-leak guard, rewritten teardown doc-blockandroid/app/src/main/java/com/therealaleph/mhrv/ui/HomeScreen.kt—awaitingRunningstate-driven button gate