summaryrefslogtreecommitdiff
path: root/internal/executor/claude.go
diff options
context:
space:
mode:
authorClaudomator Agent <agent@claudomator>2026-03-09 07:28:02 +0000
committerClaudomator Agent <agent@claudomator>2026-03-09 07:28:13 +0000
commit3b4c50ead2885e9caef85193b68c12ccdb671ef1 (patch)
treec365e615721c352c04afc6c66eff775517e40d45 /internal/executor/claude.go
parentb4371d0a7dd625730c9d1e1b4010de5a7065e903 (diff)
executor: document kill-goroutine safety and add goroutine-leak test
The pgid-kill goroutine in execOnce() uses a select with both ctx.Done() and the killDone channel. Add a detailed comment explaining why the goroutine cannot block indefinitely: the killDone arm fires unconditionally when cmd.Wait() returns (whether the process exited naturally or was killed), so the goroutine always exits before execOnce() returns. Add TestExecOnce_NoGoroutineLeak_OnNaturalExit to verify this: it samples runtime.NumGoroutine() before and after execOnce() with a no-op binary ("true") and a background context (never cancelled), asserting no net goroutine growth. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Diffstat (limited to 'internal/executor/claude.go')
-rw-r--r--internal/executor/claude.go12
1 files changed, 12 insertions, 0 deletions
diff --git a/internal/executor/claude.go b/internal/executor/claude.go
index 9184333..db4d0fa 100644
--- a/internal/executor/claude.go
+++ b/internal/executor/claude.go
@@ -285,6 +285,18 @@ func (r *ClaudeRunner) execOnce(ctx context.Context, args []string, workingDir s
stdoutW.Close()
// killDone is closed when cmd.Wait() returns, stopping the pgid-kill goroutine.
+ //
+ // Safety: this goroutine cannot block indefinitely. The select has two arms:
+ // • ctx.Done() — fires if the caller cancels (e.g. timeout, user cancel).
+ // The goroutine sends SIGKILL and exits immediately.
+ // • killDone — closed by close(killDone) below, immediately after cmd.Wait()
+ // returns. This fires when the process exits for any reason (natural exit,
+ // SIGKILL from the ctx arm, or any other signal). The goroutine exits without
+ // doing anything.
+ //
+ // Therefore: for a task that completes normally with a long-lived (non-cancelled)
+ // context, the killDone arm fires and the goroutine exits. There is no path where
+ // this goroutine outlives execOnce().
killDone := make(chan struct{})
go func() {
select {