Fix task cancellation: abort handlers and kill shell processes#61
Merged
Fix task cancellation: abort handlers and kill shell processes#61
Conversation
- Add KillOnDrop wrapper around tokio::process::Child in execute_shell() to ensure spawned processes are killed when their task is dropped - Store JoinHandle alongside oneshot receiver in WaitingFor::Handler so cancel() can abort running handler tasks - Cancel only aborts foreground tasks; background tasks keep running - Kill chain: cancel() → handle.abort() → future dropped → KillOnDrop dropped → start_kill() → SIGKILL sent to child process - Add 5 cancellation tests covering pending queue, foreground abort, background preservation, and mixed scenarios - No changes to EffectHandler trait or handler signatures
d8a10dd to
9e86a52
Compare
- Add cancel_foreground() to ToolExecutor: aborts only the active
foreground task and returns error events for it
- Add has_running_foreground() query method
- App::cancel() now has two layers:
1. If foreground tool running → cancel just that tool, error sent
to LLM which continues its turn
2. Otherwise → cancel entire turn (agent stream + all tools)
- Esc behavior stacks: deny approval → cancel tool → end turn
SIGKILL to bash alone orphans its children (sleep, find, etc.) because SIGKILL can't be caught or forwarded. Fix by spawning bash in its own process group (setpgid) and killing the negative PID on drop, which kills the entire group including all children.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Cancelling tool execution (Ctrl-C) had two bugs:
JoinHandlewas discarded aftertokio::spawn, socancel()couldn't stop running handlerstokio::process::Childdoes NOT kill on drop, it detaches the processSolution
KillOnDropwrapper (io.rs): Wrapstokio::process::Childand callsstart_kill()(sync SIGKILL) in itsDropimpl. Spawned bash processes can never outlive their task.JoinHandlestored inWaitingFor::Handler(exec.rs): The handle lives alongside the oneshot receiver in the enum variant — no new fields onActivePipeline, no way for them to get out of sync.Background tasks survive cancel: Cancel means "stop the current LLM turn", not "kill everything". Running background tasks keep going.
Kill chain on Ctrl-C
No trait changes
EffectHandlertraitTests
5 new cancellation tests:
test_cancel_clears_pending_queuetest_cancel_aborts_running_foregroundtest_cancel_preserves_completed_background_taskstest_cancel_preserves_running_background_taskstest_cancel_with_mixed_running_and_completed_background