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.
This commit is contained in:
Igor Serganov
2026-07-28 10:15:02 -07:00
committed by Kristoffer Dalby
parent 89fa72e08f
commit a9f80d8802
+8 -5
View File
@@ -736,7 +736,7 @@ func (h *Headscale) Serve() error {
log.Info().Msg("metrics server disabled (metrics_listen_addr is empty)")
}
var tailsqlContext context.Context
var tailsqlCancel context.CancelFunc
if tailsqlEnabled {
if h.cfg.Database.Type != types.DatabaseSqlite {
@@ -751,9 +751,12 @@ func (h *Headscale) Serve() error {
log.Fatal().Msg("tailsql requires TS_AUTHKEY to be set")
}
tailsqlContext = context.Background()
var tailsqlCtx context.Context
tailsqlCtx, tailsqlCancel = context.WithCancel(ctx)
go runTailSQLService(ctx, util.TSLogfWrapper(), tailsqlStateDir, h.cfg.Database.Sqlite.Path) //nolint:errcheck
errorGroup.Go(func() error {
return runTailSQLService(tailsqlCtx, util.TSLogfWrapper(), tailsqlStateDir, h.cfg.Database.Sqlite.Path)
})
}
// Handle common process-killing signals so we can gracefully shut down:
@@ -832,9 +835,9 @@ func (h *Headscale) Serve() error {
log.Error().Err(err).Msg("failed to shutdown socket server")
}
if tailsqlContext != nil {
if tailsqlCancel != nil {
info("shutting down tailsql")
tailsqlContext.Done()
tailsqlCancel()
}
// Close network listeners