From 22c233f0cdffe9177e60509bb3b34c06cf253550 Mon Sep 17 00:00:00 2001 From: Aron Yang Date: Sun, 26 Oct 2025 11:09:23 +0800 Subject: [PATCH] Fix TUN mode cleanup on Linux/macOS (#8202) --- v2rayN/ServiceLib/Manager/CoreAdminManager.cs | 2 +- v2rayN/ServiceLib/Sample/kill_as_sudo_linux_sh | 18 +++++++++++++++--- v2rayN/ServiceLib/Sample/kill_as_sudo_osx_sh | 14 ++++++++++++++ 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/v2rayN/ServiceLib/Manager/CoreAdminManager.cs b/v2rayN/ServiceLib/Manager/CoreAdminManager.cs index 0d52f1c7..dbd19d38 100644 --- a/v2rayN/ServiceLib/Manager/CoreAdminManager.cs +++ b/v2rayN/ServiceLib/Manager/CoreAdminManager.cs @@ -34,7 +34,7 @@ public class CoreAdminManager StringBuilder sb = new(); sb.AppendLine("#!/bin/bash"); var cmdLine = $"{fileName.AppendQuotes()} {string.Format(coreInfo.Arguments, Utils.GetBinConfigPath(configPath).AppendQuotes())}"; - sb.AppendLine($"sudo -S {cmdLine}"); + sb.AppendLine($"exec sudo -S -- {cmdLine}"); var shFilePath = await FileManager.CreateLinuxShellFile("run_as_sudo.sh", sb.ToString(), true); var procService = new ProcessService( diff --git a/v2rayN/ServiceLib/Sample/kill_as_sudo_linux_sh b/v2rayN/ServiceLib/Sample/kill_as_sudo_linux_sh index 7f62a532..21e17408 100644 --- a/v2rayN/ServiceLib/Sample/kill_as_sudo_linux_sh +++ b/v2rayN/ServiceLib/Sample/kill_as_sudo_linux_sh @@ -28,15 +28,15 @@ fi kill_children() { local parent=$1 local children=$(ps -o pid --no-headers --ppid "$parent") - + # Output information about processes being terminated echo "Processing children of PID: $parent..." - + # Process each child for child in $children; do # Recursively find and kill child's children first kill_children "$child" - + # Force kill the child process echo "Terminating child process: $child" kill -9 "$child" 2>/dev/null || true @@ -47,6 +47,18 @@ echo "============================================" echo "Starting termination of process $PID and all its children" echo "============================================" +# Try graceful termination first +echo "Attempting graceful termination (SIGTERM) of PID: $PID" +kill -15 "$PID" 2>/dev/null || true +sleep 1 +# If still running, fall back to kill_children +if ps -p $PID > /dev/null; then + echo "Process $PID did not exit after SIGTERM; proceeding with forced termination of its children and itself" +else + echo "Process $PID exited cleanly after SIGTERM" + exit 0 +fi + # Find and kill all child processes kill_children "$PID" diff --git a/v2rayN/ServiceLib/Sample/kill_as_sudo_osx_sh b/v2rayN/ServiceLib/Sample/kill_as_sudo_osx_sh index 94011d6f..61160fa2 100644 --- a/v2rayN/ServiceLib/Sample/kill_as_sudo_osx_sh +++ b/v2rayN/ServiceLib/Sample/kill_as_sudo_osx_sh @@ -42,6 +42,20 @@ echo "============================================" echo "Starting termination of process $PID and all its descendants" echo "============================================" +# Try graceful termination first +echo "Attempting graceful termination (SIGTERM) of PID: $PID" +kill -15 "$PID" 2>/dev/null || true +sleep 1 + +# If still running, fall back to kill_descendants +# Use the macOS-native 'kill -0' check +if kill -0 $PID 2>/dev/null; then + echo "Process $PID did not exit after SIGTERM; proceeding with forced termination of its descendants and itself" +else + echo "Process $PID exited cleanly after SIGTERM" + exit 0 +fi + # Find and kill all descendant processes kill_descendants "$PID"