Skip to content

Fix system.run hanging indefinitely for IPC-connected CLI tools (regression after v0.4.3) - #29

Closed
shanselman with Copilot wants to merge 2 commits into
masterfrom
copilot/fix-system-run-hang-issue
Closed

shanselman with Copilot wants to merge 2 commits into
masterfrom
copilot/fix-system-run-hang-issue

Conversation

Copilot AI commented Feb 26, 2026 •

Copy link
Copy Markdown
Contributor

system.run hangs forever when invoking CLI tools that connect to a running process via local IPC (e.g. Obsidian.com version, docker version). Commands that completed in ~400ms on v0.4.3 never return on v0.4.5+.

Root cause

WaitForExitAsync() in .NET 6+ internally calls WaitForExit() after the process exits, which blocks until async stream reads (BeginOutputReadLine) reach EOF. When an IPC CLI spawns child processes during its socket/pipe handshake, those children inherit the stdout pipe write handle. After the CLI exits, the pipe stays open — EOF never arrives — and WaitForExitAsync() hangs indefinitely.

Fix

LocalCommandRunner.cs — swap WaitForExitAsync for the Exited event:

// Before: hangs when child processes inherit the stdout pipe write handle
await process.WaitForExitAsync(timeoutCts.Token);

// After: Exited fires on process exit only, not stream EOF
var exitTcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
process.EnableRaisingEvents = true;
process.Exited += (_, _) => exitTcs.TrySetResult(true);
// ...
await exitTcs.Task.WaitAsync(timeoutCts.Token);

After the process exits, remaining buffered output is drained by running WaitForExit() on a background thread, bounded by OutputDrainTimeoutMs = 500. If orphaned children are still holding the write handle, we log a warning and proceed with whatever output was already received — no indefinite hang.

SystemRunTests.cs — adds a Windows IntegrationFact (Run_CompletesPromptly_WhenOrphanChildProcessHoldsStdoutHandle) that starts a background child process inheriting the pipe handle and asserts the command completes in under 3 s.

Original prompt

This section details on the original issue you should resolve

<issue_title>system.run hangs indefinitely for CLI tools that connect to a running process via local IPC (regression after v0.4.3)</issue_title>
<issue_description>First of all, thank you for building and maintaining this Hub. It's a fantastic companion app and really adds a lot to the OpenClaw experience on Windows.

After upgrading from v0.4.3 to v0.4.5, system.run commands that invoke Obsidian.com (the Obsidian native CLI) hang indefinitely instead of returning output. Reverting to v0.4.3 restores the expected behavior (~400ms response time).

Steps to reproduce:

  1. Have Obsidian running with a vault open
  2. Send a system.run invocation: { "command": "Obsidian.com version", "shell": "cmd", "timeout": 10000 }
  3. Command never returns, and gateway times out after 30s

Expected: 1.12.4 (installer 1.11.7) returned in ~400ms (works correctly in v0.4.3)

Actual: Command hangs until gateway timeout

Additional findings:
• where.exe Obsidian.com returns instantly (198ms) - the executable is found in PATH
• Running Obsidian.com version in an interactive PowerShell session on the same machine works fine
• Both shell: cmd and shell: powershell hang the same way
• The Hub correctly runs all commands via powershell.exe -NoProfile -NonInteractive - something about the subprocess execution context appears to have changed between v0.4.3 and v0.4.5

Note for reproduction without Obsidian: The issue likely affects any CLI that communicates with a running process via local IPC (named pipe or localhost socket), and not just Obsidian. docker version (with Docker Desktop running) follows the same pattern and may be easier to reproduce.

Environment:
• Windows 11 Pro, interactive desktop session
• Hub: upgraded from v0.4.3 → v0.4.5 (regression), working on v0.4.3
• Obsidian v1.12.4</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Fix system.run hanging issue for CLI tools Fix system.run hanging indefinitely for IPC-connected CLI tools (regression after v0.4.3) Feb 26, 2026
@shanselman
shanselman marked this pull request as ready for review March 4, 2026 18:07
@github-actions github-actions Bot mentioned this pull request Mar 17, 2026
36 tasks
shanselman added a commit that referenced this pull request Mar 17, 2026
Address review findings from PRs #29 and #32:

- QuoteArgIfNeeded: check all shell metacharacters (& | ; < > etc.),
  not just whitespace/quotes — prevents command injection
- cmd.exe: use doubled-quote escaping (%"%") instead of backslash
  which cmd doesn't recognize
- FormatExecCommand: align metacharacter checks with QuoteArgIfNeeded
  so system.run.prepare shows the same command that actually executes
- StringBuilder: add lock around stdout/stderr event handlers and
  ToString() reads to prevent concurrent-access corruption

Co-authored-by: danedane <[email protected]>
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: Copilot <[email protected]>
@shanselman

Copy link
Copy Markdown
Collaborator

Hey @copilot-swe-agent — thank you for the solid fix here! The \Exited\ event approach is exactly right for avoiding the \WaitForExitAsync\ EOF hang.

This work has been cherry-picked into #38, which also includes the argv quoting improvements from #32. As part of review, we added \lock\ synchronization around the \StringBuilder\ stdout/stderr handlers to address a thread-safety issue with the concurrent event callbacks.

Thanks again — and thanks to @milosm for the excellent bug report in #28! 🙏

Copilot AI requested a review from shanselman March 17, 2026 01:30
shanselman added a commit that referenced this pull request Mar 17, 2026
Merges the consolidated fix for system.run hang (#29), argv quoting (#32), and review hardening. Fixes #28.
@shanselman

Copy link
Copy Markdown
Collaborator

Closing — this fix has been cherry-picked into #38 (now merged) along with additional hardening (StringBuilder thread safety). Thank you @copilot-swe-agent for the fix and @milosm for reporting #28! 🙏

@shanselman shanselman closed this Mar 17, 2026
@shanselman
shanselman deleted the copilot/fix-system-run-hang-issue branch March 17, 2026 05:01
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.

system.run hangs indefinitely for CLI tools that connect to a running process via local IPC (regression after v0.4.3)

2 participants