Skip to content

Fix potential pipe deadlock in shell tool execution#140

Merged
ilblackdragon merged 1 commit intonearai:mainfrom
AI-Reviewer-QS:fix/shell-pipe-deadlock
Feb 20, 2026
Merged

Fix potential pipe deadlock in shell tool execution#140
ilblackdragon merged 1 commit intonearai:mainfrom
AI-Reviewer-QS:fix/shell-pipe-deadlock

Conversation

@AI-Reviewer-QS
Copy link
Copy Markdown
Contributor

Summary

In ShellTool::execute_direct(), child.wait() was called before draining stdout/stderr pipes. When a command produces output exceeding the OS pipe buffer size (~64KB on Linux, ~16KB on macOS), the child process blocks on the write, and wait() never returns — causing a deadlock.

Fix: Restructure to drain stdout and stderr concurrently with child.wait() using tokio::join!. Pipes are taken from the child handle before entering the timeout block. Each pipe reader uses take(MAX_OUTPUT_SIZE) for memory-bounded reads, followed by a drain loop to consume any remaining output so the child can exit cleanly.

Test: Added test_large_output_command that generates 128KB of output (exceeding typical pipe buffer sizes) and verifies the command completes with exit code 0.

Changes

  • src/tools/builtin/shell.rs: Restructure pipe reading and process waiting to run concurrently, add large-output test

Test plan

  • cargo test tools::builtin::shell — all tests pass including new large-output test
  • cargo clippy — no warnings
  • cargo fmt --check — formatted

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @AI-Reviewer-QS, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a critical deadlock scenario in the shell tool execution where commands generating output larger than OS pipe buffers could cause the process to hang indefinitely. The solution involves a robust restructuring of how child process output streams are handled, allowing for concurrent reading of stdout and stderr while waiting for the process to complete. This significantly enhances the reliability of shell command execution, particularly for long-running or verbose processes, and includes improved command approval mechanisms.

Highlights

  • Deadlock Prevention: Resolved a potential pipe deadlock in ShellTool::execute_direct that occurred when child processes produced output exceeding OS pipe buffer sizes.
  • Concurrent Pipe Draining: Implemented concurrent draining of stdout and stderr pipes using tokio::join! alongside child.wait(), ensuring the child process can exit cleanly.
  • Bounded and Complete Output Handling: Ensured memory-bounded reads for initial output (MAX_OUTPUT_SIZE) followed by a full drain of any remaining pipe content to prevent child process blocking.
  • Improved Command Parameter Extraction: Refactored command parameter extraction using require_str and enhanced the requires_approval_for method to correctly parse commands from various JSON structures.
  • New Test Coverage: Added several new tests, including a specific test_large_output_command to validate the deadlock fix and tests for the updated command approval logic.
Changelog
  • src/tools/builtin/shell.rs
    • Imported require_str for cleaner parameter validation.
    • Refactored execute_direct to concurrently drain stdout/stderr and wait for the child process using tokio::join!, preventing deadlocks with large outputs.
    • Implemented bounded and complete pipe draining for stdout and stderr.
    • Updated execute to use the require_str helper for command extraction.
    • Added a new requires_approval_for method to ShellTool to handle specific command approval logic, including parsing command arguments from various JSON structures.
    • Introduced several new unit tests for command extraction, approval logic, and a dedicated test for large output commands to confirm the deadlock fix.
Activity
  • The pull request was created to fix a potential pipe deadlock in shell tool execution.
  • The author provided a detailed summary of the problem, the fix, and the test plan.
  • The author confirmed that all tests pass, including the new large-output test, and that cargo clippy and cargo fmt --check show no issues.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request successfully addresses a critical deadlock vulnerability in shell command execution by concurrently reading stdout/stderr for large outputs, and includes a dedicated test case. However, a significant security concern remains: the pattern matching used to detect destructive commands is based on simple string containment, which is easily bypassed, allowing sensitive operations to proceed without explicit approval. I've also provided suggestions to enhance code clarity by using more idiomatic Rust for draining output, improve test accuracy by asserting the exact truncated output size, and promote better code reuse by extracting command extraction logic.

Comment thread src/tools/builtin/shell.rs
Comment thread src/tools/builtin/shell.rs Outdated
Comment thread src/tools/builtin/shell.rs Outdated
Comment thread src/tools/builtin/shell.rs Outdated
@AI-Reviewer-QS AI-Reviewer-QS force-pushed the fix/shell-pipe-deadlock branch 4 times, most recently from aeee6a7 to a1c4ab5 Compare February 19, 2026 05:25
@sumleo sumleo force-pushed the fix/shell-pipe-deadlock branch from a1c4ab5 to dabb097 Compare February 19, 2026 05:49
@ilblackdragon
Copy link
Copy Markdown
Member

You have LLM folder changes which are not relevant to this PR

Drain stdout and stderr concurrently with child.wait() using tokio::join
to prevent deadlocks when command output exceeds the OS pipe buffer
(64KB on Linux, 16KB on macOS).

Use AsyncReadExt::take() for memory-bounded reads and
tokio::io::copy to sink for draining excess output.

Add regression test that generates 128KB of output to verify the
fix prevents deadlocks.
@AI-Reviewer-QS AI-Reviewer-QS force-pushed the fix/shell-pipe-deadlock branch from 5a2d692 to 36bd9fc Compare February 20, 2026 01:33
@AI-Reviewer-QS
Copy link
Copy Markdown
Contributor Author

Removed the unrelated LLM formatting changes — the PR now only touches src/tools/builtin/shell.rs.

@ilblackdragon ilblackdragon merged commit 9906190 into nearai:main Feb 20, 2026
2 checks passed
This was referenced Feb 20, 2026
jaswinder6991 pushed a commit to jaswinder6991/ironclaw that referenced this pull request Feb 26, 2026
Drain stdout and stderr concurrently with child.wait() using tokio::join
to prevent deadlocks when command output exceeds the OS pipe buffer
(64KB on Linux, 16KB on macOS).

Use AsyncReadExt::take() for memory-bounded reads and
tokio::io::copy to sink for draining excess output.

Add regression test that generates 128KB of output to verify the
fix prevents deadlocks.
bkutasi pushed a commit to bkutasi/ironclaw that referenced this pull request Mar 28, 2026
Drain stdout and stderr concurrently with child.wait() using tokio::join
to prevent deadlocks when command output exceeds the OS pipe buffer
(64KB on Linux, 16KB on macOS).

Use AsyncReadExt::take() for memory-bounded reads and
tokio::io::copy to sink for draining excess output.

Add regression test that generates 128KB of output to verify the
fix prevents deadlocks.
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