backport v1.11: Cancel the context when Main hands back no Control (#1891)

The goroutines Main starts stop only when its context does, and only a caller
holding the Control can arrange that. Cancelling solely on error left a config
test, which returns no Control and no error, with the lighthouse query worker
and a hostname resolver per dns named static host running for the life of the
process. Every embedder that validates a config in process rather than by
exec'ing pays for that on each config load, dnclient and the apple clients
included.


(cherry picked from commit 9651ffdb0d)

Co-authored-by: Nate Brown <nbrown.us@gmail.com>
This commit is contained in:
Caleb JasikandNate Brown authored and GitHub committed 2026-09-22 12:06:11 -05:00
1 parent 4dbeff889c
commit 14a2fe0a99
2 files changed
+87 -2

No files matched your search

+5 -2
View File
@@ -29,9 +29,12 @@ const _ = noiseutil.RejectHeadroom - 4*maxRoutines
func Main(c *config.C, configTest bool, buildVersion string, l *slog.Logger, deviceFactory overlay.DeviceFactory) (retcon *Control, reterr error) {
ctx, cancel := context.WithCancel(context.Background())
// Automatically cancel the context if Main returns an error, to signal all created goroutines to quit.
// The goroutines started below stop only when this context does, and only a caller holding the
// Control can arrange that. Cancel whenever we are not handing one back, which covers an error
// and a config test alike: a config test used to leave the lighthouse query worker, and a
// hostname resolver per dns named static host, running for the life of the process.
defer func() {
if reterr != nil {
if retcon == nil {
cancel()
}
}()