Skip to content

fix: handle multiple lifecycle hooks without closed pipe reuse#3741

Merged
DrJosh9000 merged 2 commits intomainfrom
fix/lifecycle-hooks-pipe-handling
Mar 11, 2026
Merged

fix: handle multiple lifecycle hooks without closed pipe reuse#3741
DrJosh9000 merged 2 commits intomainfrom
fix/lifecycle-hooks-pipe-handling

Conversation

@lox
Copy link
Contributor

@lox lox commented Mar 6, 2026

Problem

The agent startup lifecycle hook runner supports multiple hook paths (HooksPath + AdditionalHooksPaths), but the shared output pipe writer was closed inside the per-hook loop.

That means the first hook could run successfully, then the second hook attempted to write to a closed pipe and failed with:

io: read/write on closed pipe

Impact

When users configure additional lifecycle hook paths, startup/shutdown hooks can fail even though hook scripts themselves are valid.

This is user-visible and can break agent bootstrap behaviour in environments that rely on multiple global hook directories.

Reproduction

Re-enabled TestAgentStartupHookWithAdditionalPaths, which previously failed with the closed-pipe error on the second hook execution.

Fix

  • return early when no lifecycle hooks are found (avoid unnecessary scanner goroutine)
  • keep the pipe/scanner alive for the whole hook batch
  • close the writer and wait for scanner completion once via deferred cleanup, after all hooks have run
  • remove per-hook w.Close() / wg.Wait() inside the loop

This preserves streaming log output while preventing closed-pipe reuse across hooks.

Testing

  • go test ./clicommand -run 'TestAgentStartupHookWithAdditionalPaths|TestAgentStartupHook|TestAgentShutdownHook' -count=1

@lox lox requested review from a team as code owners March 6, 2026 06:08
Copy link
Contributor

@DrJosh9000 DrJosh9000 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@DrJosh9000 DrJosh9000 merged commit 7b734e6 into main Mar 11, 2026
2 checks passed
@DrJosh9000 DrJosh9000 deleted the fix/lifecycle-hooks-pipe-handling branch March 11, 2026 06:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants