From ae44fb222ec73cae8441f5a5d9a21f8585fe587f Mon Sep 17 00:00:00 2001 From: Jeff Halter <868228+jhalter@users.noreply.github.com> Date: Thu, 9 Jul 2026 18:54:04 -0700 Subject: Send a goodbye message to clients on signal-initiated shutdown SIGTERM and SIGINT canceled the server context directly, so clients were force-closed without the TranDisconnectMsg broadcast that the API shutdown endpoint sends. The signal handler now calls Server.Shutdown with a goodbye message, giving both shutdown paths the same behavior: broadcast, a brief flush window, then force-close and drain. Default signal handling is restored before the graceful shutdown begins, so a second signal still kills the process immediately rather than waiting out the flush window. --- cmd/mobius-hotline-server/main.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'cmd') diff --git a/cmd/mobius-hotline-server/main.go b/cmd/mobius-hotline-server/main.go index 52099ad..bc332f0 100644 --- a/cmd/mobius-hotline-server/main.go +++ b/cmd/mobius-hotline-server/main.go @@ -35,6 +35,7 @@ var ( func main() { ctx, cancel := context.WithCancel(context.Background()) + defer cancel() sigChan := make(chan os.Signal, 1) signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGINT, os.Interrupt) @@ -255,9 +256,14 @@ func main() { reloadFunc() default: - // Canceling the context stops ListenAndServe, which unblocks main for a clean exit. + // Restore default signal handling first, so a second signal kills the process + // immediately instead of waiting out the graceful shutdown. signal.Stop(sigChan) - cancel() + + // Broadcast a goodbye message to connected clients, then stop ListenAndServe, + // which unblocks main for a clean exit. Shutdown waits a few seconds so client + // writers can flush the message before connections are force-closed. + srv.Shutdown([]byte("Server is shutting down.")) } } -- cgit