From 735440d1a34ff9b3de679df4bc085133c684c9ef Mon Sep 17 00:00:00 2001 From: Anton Schubert Date: Fri, 17 Jun 2022 10:58:22 +0200 Subject: [PATCH 1/7] add timeout for http shutdown, add db disconnect --- app.go | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/app.go b/app.go index 917d32b..78c1ad6 100644 --- a/app.go +++ b/app.go @@ -54,12 +54,13 @@ const ( ) const ( - AuthPrefix = "Bearer " - Postgres = "postgres" - Sqlite = "sqlite3" - updateInterval = 5000 - HTTPReadTimeout = 30 * time.Second - privateKeyFileMode = 0o600 + AuthPrefix = "Bearer " + Postgres = "postgres" + Sqlite = "sqlite3" + updateInterval = 5000 + HTTPReadTimeout = 30 * time.Second + HTTPShutdownTimeout = 3 * time.Second + privateKeyFileMode = 0o600 registerCacheExpiration = time.Minute * 15 registerCacheCleanup = time.Minute * 20 @@ -668,8 +669,13 @@ func (h *Headscale) Serve() error { Msg("Received signal to stop, shutting down gracefully") // Gracefully shut down servers - promHTTPServer.Shutdown(ctx) - httpServer.Shutdown(ctx) + ctx, cancel := context.WithTimeout(context.Background(), HTTPShutdownTimeout) + if err := promHTTPServer.Shutdown(ctx); err != nil { + log.Error().Err(err).Msg("Failed to shutdown prometheus http") + } + if err := httpServer.Shutdown(ctx); err != nil { + log.Error().Err(err).Msg("Failed to shutdown http") + } grpcSocket.GracefulStop() // Close network listeners @@ -680,7 +686,21 @@ func (h *Headscale) Serve() error { // Stop listening (and unlink the socket if unix type): socketListener.Close() + // Close db connections + db, err := h.db.DB() + if err != nil { + log.Error().Err(err).Msg("Failed to get db handle") + } + err = db.Close() + if err != nil { + log.Error().Err(err).Msg("Failed to close db") + } + + log.Info(). + Msg("Headscale stopped") + // And we're done: + cancel() os.Exit(0) } } From 8111b0aa834afc32d7852e7c1663b70b2d939e18 Mon Sep 17 00:00:00 2001 From: Anton Schubert Date: Fri, 17 Jun 2022 11:07:35 +0200 Subject: [PATCH 2/7] update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ccd4f71..8a14abf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,7 @@ - Add configuration option to allow Tailscale clients to use a random WireGuard port. [kb/1181/firewalls](https://tailscale.com/kb/1181/firewalls) [#624](https://github.com/juanfont/headscale/pull/624) - Improve obtuse UX regarding missing configuration (`ephemeral_node_inactivity_timeout` not set) [#639](https://github.com/juanfont/headscale/pull/639) - Fix nodes being shown as 'offline' in `tailscale status` [648](https://github.com/juanfont/headscale/pull/648) +- Improve shutdown behaviour [651](https://github.com/juanfont/headscale/pull/651) ## 0.15.0 (2022-03-20) From c8378e8b7dd66009ef3e36729875190c35fdc0df Mon Sep 17 00:00:00 2001 From: Juan Font Alonso Date: Wed, 22 Jun 2022 14:40:40 +0200 Subject: [PATCH 3/7] Quick fix to segfault on CLI when Headscale is not running (fix #652) --- cmd/headscale/cli/utils.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cmd/headscale/cli/utils.go b/cmd/headscale/cli/utils.go index 327c8c1..50bd15f 100644 --- a/cmd/headscale/cli/utils.go +++ b/cmd/headscale/cli/utils.go @@ -55,6 +55,7 @@ func getHeadscaleCLIClient() (context.Context, v1.HeadscaleServiceClient, *grpc. Err(err). Caller(). Msgf("Failed to load configuration") + os.Exit(-1) // we get here if logging is supressed (i.e., json output) } log.Debug(). @@ -116,6 +117,7 @@ func getHeadscaleCLIClient() (context.Context, v1.HeadscaleServiceClient, *grpc. conn, err := grpc.DialContext(ctx, address, grpcOptions...) if err != nil { log.Fatal().Caller().Err(err).Msgf("Could not connect: %v", err) + os.Exit(-1) // we get here if logging is supressed (i.e., json output) } client := v1.NewHeadscaleServiceClient(conn) From 0fa943e4b74c394f73eef06df9c3402810b90f6c Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Sun, 26 Jun 2022 09:29:33 +0200 Subject: [PATCH 4/7] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a14abf..7b70c32 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,7 +31,7 @@ - Add configuration option to allow Tailscale clients to use a random WireGuard port. [kb/1181/firewalls](https://tailscale.com/kb/1181/firewalls) [#624](https://github.com/juanfont/headscale/pull/624) - Improve obtuse UX regarding missing configuration (`ephemeral_node_inactivity_timeout` not set) [#639](https://github.com/juanfont/headscale/pull/639) - Fix nodes being shown as 'offline' in `tailscale status` [648](https://github.com/juanfont/headscale/pull/648) -- Improve shutdown behaviour [651](https://github.com/juanfont/headscale/pull/651) +- Improve shutdown behaviour [#651](https://github.com/juanfont/headscale/pull/651) ## 0.15.0 (2022-03-20) From 4637400d2916be712f22e0ed48c2f03769687b85 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Sun, 26 Jun 2022 09:30:16 +0200 Subject: [PATCH 5/7] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b70c32..ad3bbfb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,7 +30,7 @@ - Add -c option to specify config file from command line [#285](https://github.com/juanfont/headscale/issues/285) [#612](https://github.com/juanfont/headscale/pull/601) - Add configuration option to allow Tailscale clients to use a random WireGuard port. [kb/1181/firewalls](https://tailscale.com/kb/1181/firewalls) [#624](https://github.com/juanfont/headscale/pull/624) - Improve obtuse UX regarding missing configuration (`ephemeral_node_inactivity_timeout` not set) [#639](https://github.com/juanfont/headscale/pull/639) -- Fix nodes being shown as 'offline' in `tailscale status` [648](https://github.com/juanfont/headscale/pull/648) +- Fix nodes being shown as 'offline' in `tailscale status` [#648](https://github.com/juanfont/headscale/pull/648) - Improve shutdown behaviour [#651](https://github.com/juanfont/headscale/pull/651) ## 0.15.0 (2022-03-20) From 72d1d2630e720c42470bd12af9ab8f82cb084c72 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Sun, 26 Jun 2022 09:52:04 +0200 Subject: [PATCH 6/7] Update cmd/headscale/cli/utils.go --- cmd/headscale/cli/utils.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/headscale/cli/utils.go b/cmd/headscale/cli/utils.go index 50bd15f..56da6be 100644 --- a/cmd/headscale/cli/utils.go +++ b/cmd/headscale/cli/utils.go @@ -55,7 +55,7 @@ func getHeadscaleCLIClient() (context.Context, v1.HeadscaleServiceClient, *grpc. Err(err). Caller(). Msgf("Failed to load configuration") - os.Exit(-1) // we get here if logging is supressed (i.e., json output) + os.Exit(-1) // we get here if logging is suppressed (i.e., json output) } log.Debug(). From 34f489b1f43e6e25b269f4ad931285e76b7541b6 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Sun, 26 Jun 2022 09:52:11 +0200 Subject: [PATCH 7/7] Update cmd/headscale/cli/utils.go --- cmd/headscale/cli/utils.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/headscale/cli/utils.go b/cmd/headscale/cli/utils.go index 56da6be..c07e3a2 100644 --- a/cmd/headscale/cli/utils.go +++ b/cmd/headscale/cli/utils.go @@ -117,7 +117,7 @@ func getHeadscaleCLIClient() (context.Context, v1.HeadscaleServiceClient, *grpc. conn, err := grpc.DialContext(ctx, address, grpcOptions...) if err != nil { log.Fatal().Caller().Err(err).Msgf("Could not connect: %v", err) - os.Exit(-1) // we get here if logging is supressed (i.e., json output) + os.Exit(-1) // we get here if logging is suppressed (i.e., json output) } client := v1.NewHeadscaleServiceClient(conn)