From 9954a3c599dadfc54f0447ea815d9b0f07b2298c Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Tue, 2 Nov 2021 21:46:15 +0000 Subject: [PATCH] Add handling for closing the socket --- app.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/app.go b/app.go index a28a2152..b61331a1 100644 --- a/app.go +++ b/app.go @@ -10,9 +10,11 @@ import ( "net/http" "net/url" "os" + "os/signal" "sort" "strings" "sync" + "syscall" "time" "github.com/coreos/go-oidc/v3/oidc" @@ -352,6 +354,19 @@ func (h *Headscale) Serve() error { panic(err) } + // Handle common process-killing signals so we can gracefully shut down: + sigc := make(chan os.Signal, 1) + signal.Notify(sigc, os.Interrupt, os.Kill, syscall.SIGTERM) + go func(c chan os.Signal) { + // Wait for a SIGINT or SIGKILL: + sig := <-c + log.Printf("Caught signal %s: shutting down.", sig) + // Stop listening (and unlink the socket if unix type): + socketListener.Close() + // And we're done: + os.Exit(0) + }(sigc) + networkListener, err := net.Listen("tcp", h.cfg.Addr) if err != nil { panic(err)