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.

(cherry picked from commit a9f80d8802)
This commit is contained in:
Igor Serganov
2026-07-28 10:15:02 -07:00
committed by Kristoffer Dalby
parent 33db8c6b29
commit 032470a5d3
+8 -5
View File
@@ -876,7 +876,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 {
@@ -891,9 +891,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:
@@ -975,9 +978,9 @@ func (h *Headscale) Serve() error {
grpcListener.Close()
}
if tailsqlContext != nil {
if tailsqlCancel != nil {
info("shutting down tailsql")
tailsqlContext.Done()
tailsqlCancel()
}
// Close network listeners