Skip to content

Commit 481e422

Browse files
committed
hscontrol: cancel tailsql on graceful shutdown
Serve stored a Background context for tailsql and called context.Done() during signal shutdown. Done only returns the done channel and does not cancel, so tailsql never stopped when Headscale shut down. Create a cancellable child of the Serve context, run `runTailSQLService` in the listener errgroup so its error is surfaced, and call the cancel func on shutdown - tailsql unblocks on ctx.Done.
1 parent cfd845c commit 481e422

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

hscontrol/app.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ func (h *Headscale) Serve() error {
737737
log.Info().Msg("metrics server disabled (metrics_listen_addr is empty)")
738738
}
739739

740-
var tailsqlContext context.Context
740+
var tailsqlCancel context.CancelFunc
741741

742742
if tailsqlEnabled {
743743
if h.cfg.Database.Type != types.DatabaseSqlite {
@@ -752,9 +752,12 @@ func (h *Headscale) Serve() error {
752752
log.Fatal().Msg("tailsql requires TS_AUTHKEY to be set")
753753
}
754754

755-
tailsqlContext = context.Background()
755+
var tailsqlCtx context.Context
756+
tailsqlCtx, tailsqlCancel = context.WithCancel(ctx)
756757

757-
go runTailSQLService(ctx, util.TSLogfWrapper(), tailsqlStateDir, h.cfg.Database.Sqlite.Path) //nolint:errcheck
758+
errorGroup.Go(func() error {
759+
return runTailSQLService(tailsqlCtx, util.TSLogfWrapper(), tailsqlStateDir, h.cfg.Database.Sqlite.Path)
760+
})
758761
}
759762

760763
// Handle common process-killing signals so we can gracefully shut down:
@@ -833,9 +836,9 @@ func (h *Headscale) Serve() error {
833836
log.Error().Err(err).Msg("failed to shutdown socket server")
834837
}
835838

836-
if tailsqlContext != nil {
839+
if tailsqlCancel != nil {
837840
info("shutting down tailsql")
838-
tailsqlContext.Done()
841+
tailsqlCancel()
839842
}
840843

841844
// Close network listeners

0 commit comments

Comments
 (0)