Repository navigation
Description
When the user taps the Disconnect button, the app crashes. This happens because MainActivity.onStop callback sends ACTION_STOP via startService() and then immediately calls stopService() on the same service, creating a race condition with the teardown thread inside MhrvVpnService.
Root cause:
In MainActivity, the onStop lambda does:
onStop = {
val stopAction = Intent(this, MhrvVpnService::class.java)
.setAction(MhrvVpnService.ACTION_STOP)
startService(stopAction)
stopService(Intent(this, MhrvVpnService::class.java)) // ← crashes here
}Meanwhile in MhrvVpnService.onStartCommand, the ACTION_STOP branch spawns a background thread to run teardown() and then calls stopSelf():
ACTION_STOP -> {
try { stopForeground(STOP_FOREGROUND_REMOVE) } catch ...
Thread({
teardown()
stopSelf()
}, "mhrv-teardown").start()
START_NOT_STICKY
}The problem:
- startService(stopAction) delivers ACTION_STOP → spawns "mhrv-teardown" thread → thread starts teardown() (which includes Native.stopProxy(handle) and can take several seconds).
- stopService() is called immediately after on the same service. This triggers onDestroy() while the teardown thread is still running. Two threads now race through service lifecycle — the background thread calling stopSelf() on a service that stopService() is already destroying.
- Depending on timing, this can cause:
· stopSelf() crashing because the service is already being destroyed by stopService()
· onDestroy() racing with the teardown thread mid-cleanup
· The service process dying while native handles are still being released
Fix:
Remove the redundant stopService() call. The ACTION_STOP path in MhrvVpnService already handles the full graceful teardown (stop foreground → teardown on background thread → stopSelf). The extra stopService() is unnecessary and dangerous.
onStop = {
val stopAction = Intent(this, MhrvVpnService::class.java)
.setAction(MhrvVpnService.ACTION_STOP)
startService(stopAction)
// stopService(Intent(this, MhrvVpnService::class.java)) ← REMOVE THIS LINE
}Steps to reproduce:
- Start the VPN (tap Connect)
- Wait for it to be fully running
- Tap Disconnect
- App crashes
Expected behavior:
App should stop the VPN gracefully without crashing.
Additional notes:
The MhrvVpnService.teardown() already has an idempotency guard (tornDown AtomicBoolean), which is correct for handling the onDestroy path. But the root issue is that stopService() shouldn't be called at all when we've already sent ACTION_STOP — let the service handle its own shutdown.
Activity
therealaleph commentedon May 3, 2026
@ilok67 — confirmed root cause + fix. Applied in 1c9c2e0 (will land in v1.9.8): removed the redundant
stopService()call afterstartService(ACTION_STOP)inMainActivity.onStop. The graceful-teardown path insideMhrvVpnServicealready callsstopSelf()at the end of themhrv-teardownthread, so the secondstopService()was just racing the lifecycle for no benefit. ThetornDownAtomicBoolean protects against double-teardown of native state, but it can't protect against OS-levelonDestroyracing the still-running teardown thread, which is what the user-visible crash was.Zombie protection (the original reason
stopServicewas added):startService(ACTION_STOP)already covers it — Android creates a fresh service instance in the new process if the previous one died, the teardown runs as a no-op on already-clean state, and the new instance stops viastopSelf. No extrastopServiceneeded.Thanks for the detailed analysis — this was exactly the right level of triage to ship a one-line fix off of.
[reply via Anthropic Claude | reviewed by @therealaleph]
fix: v1.9.8 — Android disconnect crash + UI test-button gate for non-…