From 3adefe42028db1618f4db9f3f5506a0123a59083 Mon Sep 17 00:00:00 2001 From: yusing Date: Sun, 25 Jan 2026 12:20:47 +0800 Subject: [PATCH] fix: add startup timeout guard to prevent indefinite hangs Add a 10-second timeout mechanism during application initialization. If initialization fails to complete within the timeout window, the application logs a fatal error and exits. This prevents the proxy from becoming unresponsive during startup due to blocking operations in parallel initialization tasks (DNS providers, icon cache, system info poller, middleware loading, Docker client, API server, debug server, config watcher). The timeout guard uses a background goroutine that listens for either a completion signal (via closing the done channel) or the timeout expiration, providing a safety net for long-running or blocked initialization scenarios. --- cmd/main.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/cmd/main.go b/cmd/main.go index f227da7b..e91d1bbd 100755 --- a/cmd/main.go +++ b/cmd/main.go @@ -3,6 +3,7 @@ package main import ( "os" "sync" + "time" "github.com/rs/zerolog/log" "github.com/yusing/godoxy/internal/api" @@ -32,6 +33,16 @@ func parallel(fns ...func()) { } func main() { + done := make(chan struct{}, 1) + go func() { + select { + case <-done: + return + case <-time.After(time.Second * 10): + log.Fatal().Msgf("timeout waiting for initialization to complete, exiting...") + } + }() + initProfiling() logging.InitLogger(os.Stderr, memlogger.GetMemLogger()) @@ -86,6 +97,8 @@ func main() { uptime.Poller.Start() config.WatchChanges() + close(done) + task.WaitExit(config.Value().TimeoutShutdown) }