Skip to content

Commit b3ef5cd

Browse files
Frank Guoclaude
andcommitted
Fix daemon goroutine leak: kill process on startup failure
ensureDaemon now detects early daemon exit via a wait channel and kills the daemon process if the poll window expires, preventing leaked goroutines that caused CI integration tests to timeout after 10 minutes. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 8db3c8a commit b3ef5cd

1 file changed

Lines changed: 17 additions & 2 deletions

File tree

cmd/rekal/cli/nomic/daemon.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,12 +314,24 @@ func ensureDaemon(gitRoot string) (*daemonClient, error) {
314314
if err := cmd.Start(); err != nil {
315315
return nil, fmt.Errorf("nomic: start daemon: %w", err)
316316
}
317-
// Detach — don't wait.
318-
go cmd.Wait() //nolint:errcheck
317+
318+
// Wait in background so we can detect early exit.
319+
waitCh := make(chan error, 1)
320+
go func() { waitCh <- cmd.Wait() }()
319321

320322
// Poll for socket readiness.
321323
deadline := time.Now().Add(startPoll)
322324
for time.Now().Before(deadline) {
325+
// Check if daemon already exited (failed to start).
326+
select {
327+
case err := <-waitCh:
328+
if err != nil {
329+
return nil, fmt.Errorf("nomic: daemon exited: %w", err)
330+
}
331+
return nil, fmt.Errorf("nomic: daemon exited unexpectedly")
332+
default:
333+
}
334+
323335
time.Sleep(100 * time.Millisecond)
324336
conn, err := net.DialTimeout("unix", sock, dialTimeout)
325337
if err != nil {
@@ -332,6 +344,9 @@ func ensureDaemon(gitRoot string) (*daemonClient, error) {
332344
dc.Close()
333345
}
334346

347+
// Timed out — kill the daemon process to avoid leaked goroutines.
348+
cmd.Process.Kill() //nolint:errcheck
349+
<-waitCh
335350
return nil, fmt.Errorf("nomic: daemon did not start within %v", startPoll)
336351
}
337352

0 commit comments

Comments
 (0)