Skip to content

fix: propagate context cancellation to background jobs and handle interrupted waits#3247

Open
lockp111 wants to merge 1 commit into
charmbracelet:mainfrom
lockp111:fix/job-tool
Open

fix: propagate context cancellation to background jobs and handle interrupted waits#3247
lockp111 wants to merge 1 commit into
charmbracelet:mainfrom
lockp111:fix/job-tool

Conversation

@lockp111

@lockp111 lockp111 commented Jul 3, 2026

Copy link
Copy Markdown

What

Background shell jobs were launched with context.Background(), making them immune to Esc (agent cancellation) and session shutdown. This left zombie processes that couldn't be cleaned up without manual job_kill.

Additionally, job_output with wait=true silently discarded the WaitContext return value, so context cancellation during a wait was invisible — the tool returned stale output as if nothing happened.

Changes

internal/agent/tools/bash.go

  • Both run_in_background and synchronous auto-background paths now use the agent's request context (ctx) instead of context.Background() when calling bgManager.Start.
  • When the agent is canceled (Esc or session shutdown), the context cancellation propagates through shellCtxexec.CommandContext → process killed → goroutine exits → done channel closes.

internal/agent/tools/job_output.go

  • WaitContext return value is now checked. When it returns false (context canceled), the tool returns an "interrupted" status with the current partial output, instead of silently falling through to the normal path.

internal/agent/tools/job_test.go

  • Added 3 new tests:
    • TestJobOutput_WaitContextCanceled — canceled context returns "interrupted"
    • TestJobOutput_WaitCompletes — normal completion returns "completed"
    • TestJobOutput_NoWaitReturnsRunning — no-wait path returns immediately with "running"

Why

  • Background jobs (servers, long builds) started by the agent now get properly cleaned up on Esc
  • job_output wait path no longer silently ignores context cancellation
  • No API changes, no new dependencies
  • All existing tests pass unchanged

💘 Generated with Crush

Assisted-by: Crush:deepseek-v4-pro

…errupted waits

Background jobs started with context.Background() were immune to Esc/session
cancellation, leaving zombie processes on cancel. Use the agent context so
context cancellation kills the background process.

Also check WaitContext return value in job_output so canceled waits return
"interrupted" instead of proceeding with stale output.

💘 Generated with Crush

Assisted-by: Crush:deepseek-v4-pro
@charmcli

charmcli commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@lockp111

lockp111 commented Jul 3, 2026

Copy link
Copy Markdown
Author

I have read the Contributor License Agreement (CLA) and hereby sign the CLA.

@lockp111

Copy link
Copy Markdown
Author

@meowgorithm @andreynering who can take a look at this MR?

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