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 {