Repository navigation
Description
After the stopService() fix in 1c9c2e0, the app still crashes approximately 2 seconds after tapping Disconnect. This is a separate bug in the teardown sequence inside MhrvVpnService.teardown().
Root cause:
The current teardown order is:
1. Tun2proxy.stop() (2s timeout)
2. tun.close() (no-op, fd already detached)
3. tun2proxyThread.join() (4s timeout)
4. Native.stopProxy() ← CRASH: tun2proxy worker still in native code
The tun2proxy worker thread blocks inside Native.runTun2proxy() for the entire lifetime of the VPN. When Tun2proxy.stop() is called, it signals the native code to exit — but runTun2proxy is a blocking call that may be stuck on a socket read from the proxy's SOCKS5 port. The 2-second stop timeout and 4-second join timeout are not enough if tun2proxy has in-flight connections.
After the 4-second join gives up (thread still alive), Native.stopProxy(handle) shuts down the Rust runtime — including the SOCKS5 socket that tun2proxy's worker thread is still reading from in native code. The worker then accesses freed memory → SIGSEGV → process crash.
Evidence:
· Crash happens ~2 seconds after Disconnect (matches stopper.join(2_000) in step 1)
· Does NOT happen in PROXY_ONLY mode (no TUN, no tun2proxy thread)
· Worker thread isAlive is true after 4-second join — the Log.w for this case exists in the code but the crash happens anyway
Fix:
Swap the shutdown order so the Rust proxy is killed before tun2proxy is asked to stop. When the proxy's socket is closed, tun2proxy's blocking read will wake up with an error and exit cleanly through its own error path instead of crashing into freed memory.
Current (broken):
// 1. Stop tun2proxy first
if (tun2proxyRunning.get()) {
val stopper = Thread({ Tun2proxy.stop() }, ...).apply { start() }
stopper.join(2_000)
}
// 2. Close TUN
tun?.close(); tun = null
// 3. Join worker thread
tun2proxyThread?.join(4_000)
// 4. Stop proxy LAST — worker still in native code!
val handle = proxyHandle
proxyHandle = 0L
if (handle != 0L) Native.stopProxy(handle)Fixed (stop proxy first):
// 1. Stop proxy FIRST — kills the SOCKS5 socket tun2proxy is reading from
val handle = proxyHandle
proxyHandle = 0L
if (handle != 0L) {
try { Native.stopProxy(handle) } catch (t: Throwable) {
Log.e(TAG, "Native.stopProxy: ${t.message}", t)
}
}
// 2. Now signal tun2proxy to stop — its blocking read will fail cleanly
// because the socket is already closed by step 1
if (tun2proxyRunning.get()) {
val stopper = Thread({ Tun2proxy.stop() }, "mhrv-tun2proxy-stop").apply { start() }
try { stopper.join(2_000) } catch (_: InterruptedException) {}
if (stopper.isAlive) Log.w(TAG, "Tun2proxy.stop did not return within 2s")
}
// 3. Close TUN (no-op when fd was detached, kept for PROXY_ONLY path)
try { tun?.close() } catch (t: Throwable) { Log.w(TAG, "tun.close: ${t.message}") }
tun = null
// 4. Wait for worker thread to fully exit — should be quick now
// since step 1 killed its upstream
try { tun2proxyThread?.join(4_000) } catch (_: InterruptedException) {}
if (tun2proxyThread?.isAlive == true) {
Log.w(TAG, "tun2proxy thread still alive after join — proceeding anyway")
}
tun2proxyThread = null
// 5. Publish stopped state
VpnState.setProxyHandle(0L)
VpnState.setRunning(false)Why this works:
· Native.stopProxy() closes the listening SOCKS5 socket
· tun2proxy's worker thread, blocked on a read() from that socket, gets an error/failure
· Native.runTun2proxy() returns naturally through its error handling
· The worker thread's finally block sets tun2proxyRunning = false
· No race, no use-after-free, no SIGSEGV
Additional note:
The existing code comment in teardown() says "the runtime shutdown below will knock the rest of the world over" — but this assumption is incorrect. Native.stopProxy() does not (and cannot) forcibly terminate a separate native thread blocked on a socket read. It frees memory that the other thread is still using, which is the definition of a use-after-free bug.
Steps to reproduce:
- Start VPN in VPN_TUN mode (any mode that uses TUN: Apps Script, Direct, or Full)
- Wait for it to connect
- Tap Disconnect
- Crash occurs ~2 seconds later
Does not reproduce in: PROXY_ONLY mode (no TUN/tun2proxy)
Affected file: MhrvVpnService.kt — method teardown()
Activity
fix: v1.9.9 — Android second disconnect crash + tunnel-node drain cor…
therealaleph commentedon May 4, 2026
@ilok67 — confirmed root cause + fix applied. Shipped in 49b6fbf (v1.9.9, CI building now).
Order swap exactly as you proposed: Native.stopProxy first → Tun2proxy.stop second → tun.close → join. The new module comment cites your issue + explains why the original ordering's "runtime shutdown will knock the rest of the world over" assumption was wrong (Native.stopProxy can't forcibly terminate a separate native thread; it just frees memory the other thread is still using).
Two precise back-to-back disconnect-crash root causes from you in two days. Genuinely appreciated — both reports were specific enough to ship one-line fixes off of.
[reply via Anthropic Claude | reviewed by @therealaleph]