diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/service/V2RayVpnService.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/service/V2RayVpnService.kt index 3c70aa3a..90da87bf 100644 --- a/V2rayNG/app/src/main/java/com/v2ray/ang/service/V2RayVpnService.kt +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/service/V2RayVpnService.kt @@ -93,6 +93,21 @@ class V2RayVpnService : VpnService(), ServiceControl { override fun onDestroy() { super.onDestroy() Log.i(AppConfig.TAG, "StartCore-VPN: Service destroyed") + + // Ensure VPN interface is properly closed when the service is destroyed without + // going through stopAllService() (e.g. when killed unexpectedly). isRunning is + // set to false at the start of stopAllService(), so this guard prevents a double-close. + if (isRunning) { + try { + if (::mInterface.isInitialized) { + mInterface.close() + Log.i(AppConfig.TAG, "StartCore-VPN: VPN interface closed in onDestroy") + } + } catch (e: Exception) { + Log.e(AppConfig.TAG, "StartCore-VPN: Failed to close interface in onDestroy", e) + } + } + NotificationManager.cancelNotification() } @@ -356,9 +371,19 @@ class V2RayVpnService : VpnService(), ServiceControl { //which means the first v2ray core somehow failed to stop and release the port. stopSelf() + // Add a small delay to allow the async core stop operation to complete + // before closing the VPN interface, preventing a race condition that can + // leave the VPN icon in the status bar after stopping the service. + try { + Thread.sleep(100) + } catch (e: InterruptedException) { + Log.w(AppConfig.TAG, "StartCore-VPN: Sleep interrupted", e) + } + try { if (::mInterface.isInitialized) { mInterface.close() + Log.i(AppConfig.TAG, "StartCore-VPN: VPN interface closed") } } catch (e: Exception) { Log.e(AppConfig.TAG, "StartCore-VPN: Failed to close interface", e)