Skip to content

fix(sdk): clean up cancelled local shell commands - #5907

Open
Mason Daugherty (mdrxy) wants to merge 20 commits into
mainfrom
mdrxy/sdk/local-shell-process-cleanup
Open

fix(sdk): clean up cancelled local shell commands#5907
Mason Daugherty (mdrxy) wants to merge 20 commits into
mainfrom
mdrxy/sdk/local-shell-process-cleanup

Conversation

@mdrxy

Copy link
Copy Markdown
Member

Related: #5873

Local shell commands now clean up their supported process scope on timeout, interruption, and async cancellation so cancelled work does not continue consuming resources in the background.


LocalShellBackend currently inherits the protocol's thread-offloaded aexecute, so cancelling its caller cannot stop an already-running synchronous shell command. Detached descendants can also outlive timeout and interruption handling.

This changes command execution from subprocess.run to subprocess.Popen, starts POSIX commands in an isolated session and process group, and adds cancellation-aware output collection. Cleanup kills and reaps the POSIX process group or the direct Windows shell, closes captured pipes, bounds cleanup waits, and logs failures without replacing the active control-flow exception.

The async wrapper preserves execute overrides, prevents queued commands from starting after cancellation, retains uncooperative workers until completion, and documents that Windows descendants cannot be terminated as a group. This PR targets main directly and is independent of the narrower terminal-input fix in #5873.

Test plan
  • uv run --no-sync --group test pytest tests/unit_tests/backends/test_local_shell_backend.py tests/unit_tests/test_local_shell.py -q
  • package pre-commit lint and type checks
  • real POSIX descendant cleanup on timeout and async cancellation
  • mocked Windows direct-process cleanup

@github-actions github-actions Bot added deepagents Related to the `deepagents` SDK / agent harness fix A bug fix (PATCH) internal User is a member of the `langchain-ai` GitHub organization size: L 500-999 LOC labels Aug 27, 2026

@open-swe open-swe Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ Open SWE Review: No issues found

Open SWE reviewed this PR and found no potential bugs to report.

Open in WebView Open SWE trace

Mason Daugherty (mdrxy) added a commit that referenced this pull request Aug 27, 2026
Fixes #4329
Related: #5907

Local shell commands can no longer access the parent process's
controlling terminal on POSIX systems, preventing credential prompts
from stealing keyboard input from the dcode TUI.

---

The dcode agent routes `execute` through `LocalShellBackend`. Although
that backend gives subprocesses `stdin=subprocess.DEVNULL`, programs
such as Git and SSH can bypass standard input by opening `/dev/tty`
directly. When they prompt for credentials, they then compete with
Textual for the same terminal input, disrupting or effectively breaking
the chat input until the process exits.

Start each local shell command in a new POSIX session so it no longer
has access to dcode's controlling terminal. Interactive commands now
fail cleanly through captured output rather than intercepting
keystrokes. This also aligns the agent `execute` path with dcode's
existing local `!` shell behavior.

Windows behavior is unchanged because `start_new_session` is enabled
only on non-Windows platforms. Focused coverage runs the backend beneath
a real controlling pseudo-terminal, proves the parent can open
`/dev/tty`, and verifies the detached command cannot.

Made by [Open
SWE](https://openswe.vercel.app/agents/58b7b723-1498-5094-af76-6ebd4bda4823)

Co-authored-by: Sumit Kumar
<187615421+sumit1kr@users.noreply.github.com>

---------

Co-authored-by: open-swe[bot] <open-swe@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

deepagents Related to the `deepagents` SDK / agent harness fix A bug fix (PATCH) internal User is a member of the `langchain-ai` GitHub organization size: L 500-999 LOC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant