From 09e5e23c24c5c93f53e4c300ef0b3b07a7125098 Mon Sep 17 00:00:00 2001 From: M03ED <50927468+M03ED@users.noreply.github.com> Date: Tue, 19 May 2026 15:13:08 +0330 Subject: [PATCH] Bound Xray shutdown wait after termination signal --- main/run.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/main/run.go b/main/run.go index 2655f7833..c61844833 100644 --- a/main/run.go +++ b/main/run.go @@ -91,7 +91,6 @@ func executeRun(cmd *base.Command, args []string) { fmt.Println("Failed to start:", err) os.Exit(-1) } - defer server.Close() // Explicitly triggering GC to remove garbage from config loading. runtime.GC() @@ -102,6 +101,22 @@ func executeRun(cmd *base.Command, args []string) { signal.Notify(osSignals, os.Interrupt, syscall.SIGTERM) <-osSignals } + + closeDone := make(chan error, 1) + go func() { + closeDone <- server.Close() + }() + + select { + case err := <-closeDone: + if err != nil { + fmt.Println("Failed to close:", err) + os.Exit(1) + } + case <-time.After(10 * time.Second): + fmt.Println("Timed out while closing Xray.") + os.Exit(1) + } } func dumpConfig() int {