Fix cancellation regression: make edit_file_tool handle cancellation#46527
Merged
Fix cancellation regression: make edit_file_tool handle cancellation#46527
Conversation
4e82954 to
348d9b1
Compare
e58bd49 to
fbc3f0a
Compare
PR #46306 changed cancellation to wait for tools to complete. This was correct behavior - it allows tools like terminal to capture their output on cancellation. The real issue was that edit_file_tool didn't check for cancellation, so it would continue running until it finished. The fix adds cancellation handling to edit_file_tool using the same pattern as terminal_tool: use select! to race between processing edit events and detecting cancellation. When cancelled, it breaks out of the loop immediately. This keeps the thread.rs cancellation check after the tool_results loop (as PR #46306 intended), while ensuring edit_file_tool responds promptly to cancellation.
fbc3f0a to
20d715a
Compare
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.
PR #46306 changed cancellation to wait for tools to complete before returning. This was correct behavior - it allows tools like terminal to capture their output on cancellation. The real issue was that many tools didn't check for cancellation, so they would continue running until they finished.
The Problem
When the user pressed Escape to cancel during a tool operation, tools would continue running because they never checked for the cancellation signal. The thread correctly waited for tools to complete (so terminal could capture output), but tools like edit_file, grep, fetch, etc. would just keep going.
The Fix
Add cancellation handling to all tools using the same pattern as
terminal_tool: useselect!to race between the tool's main work andevent_stream.cancelled_by_user(). When cancelled, tools break out of their loops or return early.All Tools Now Cancellation-Aware
edit_file_toolterminal_toolgrep_toolfetch_toolweb_search_toolfind_path_toolread_file_toolcopy_path_toolmove_path_tooldelete_path_toolcreate_directory_toolsave_file_toolrestore_file_from_disk_toolopen_tooldiagnostics_toolContextServerTool(MCP)Synchronous tools (no async work, return immediately):
list_directory_tool- Reads worktree snapshot synchronouslynow_tool- Returns current time immediatelythinking_tool- Returns immediatelyMCP Tools Automatically Handled
MCP tools (user-defined tools via context servers) are now automatically cancellation-aware without any user action. The
ContextServerToolwrapper races the external server request againstevent_stream.cancelled_by_user().Testing
CancellationAwareTooltest helper that mirrors the cancellation patterntest_cancellation_aware_tool_responds_to_cancellationto properly await the cancel task and verify the tool detected cancellationRelease Notes: