Skip to content

Fix system.run hang, harden argv quoting, and add ShellQuoting tests - #38

Merged
shanselman merged 5 commits into
masterfrom
fix/system-run-quoting-and-hang
Mar 17, 2026
Merged

shanselman merged 5 commits into
masterfrom
fix/system-run-quoting-and-hang

Conversation

@shanselman

Copy link
Copy Markdown
Collaborator

Summary

This PR consolidates and improves work from #29 and #32, fixing a regression where system.run hangs indefinitely for IPC-connected CLI tools, hardening argv quoting against command injection, and extracting shared quoting logic with comprehensive test coverage.

Fixes #28

What's included

From PR #29 — Fix WaitForExitAsync hang (by @copilot-swe-agent)

Originally reported by @milosm in #28 — thank you for the excellent bug report with clear repro steps and root cause analysis! 🙏

  • Replace WaitForExitAsync with Exited event + TaskCompletionSource to avoid hanging when child processes inherit the stdout pipe write handle
  • Add 500ms output drain timeout so orphaned children don't block forever
  • Regression test: Run_CompletesPromptly_WhenOrphanChildProcessHoldsStdoutHandle

From PR #32 — Shell-aware argv quoting + system.run.prepare (by @danedane)

Thank you @danedane for identifying the arg-splitting bug and adding the system.run.prepare pre-flight handler — great work! 🎉

  • QuoteArgIfNeeded with shell-appropriate escaping (cmd.exe double quotes, PowerShell single quotes)
  • system.run.prepare handler that echoes execution plans without executing
  • FormatExecCommand helper matching gateway behavior

New in this PR — Review fixes and refactoring

Code review found several issues that are addressed here:

Issue Fix
🔴 Command injection — QuoteArgIfNeeded only checked spaces/quotes Now checks all shell metacharacters (& | ; < > ^ % ! $ \ * ?` etc.)
🔴 Wrong cmd.exe escaping — used backslash (\") which cmd doesn't recognize Fixed to doubled quotes ("")
🔴 StringBuilder thread safety — concurrent access from event handlers Added lock around stdout/stderr builders
🟡 Duplicated quoting logic — same metachar set copy-pasted in two files Extracted to shared ShellQuoting helper (follows GatewayUrlHelper pattern)
🟡 Inconsistent metachar detection — FormatExecCommand and QuoteArgIfNeeded checked different chars Both now call ShellQuoting.NeedsQuoting

Test coverage

Changed files

  • src/OpenClaw.Shared/ShellQuoting.cs — new shared quoting helper
  • src/OpenClaw.Shared/LocalCommandRunner.cs — hang fix + uses ShellQuoting
  • src/OpenClaw.Shared/Capabilities/SystemCapability.cs — system.run.prepare + delegates to ShellQuoting
  • tests/OpenClaw.Shared.Tests/ShellQuotingTests.cs — new 43 unit tests
  • tests/OpenClaw.Shared.Tests/SystemRunTests.cs — IPC hang regression test

Copilot AI and others added 5 commits March 16, 2026 18:24
When system.run receives a command as an argv array, BuildProcessArgs
joins the args with spaces before passing to the shell. Args containing
spaces (e.g. a -p prompt like 'List all files') would be split into
separate tokens by the shell, causing tools like GitHub Copilot CLI
to report 'too many arguments'.

Added QuoteArgIfNeeded() that wraps args containing spaces, quotes,
or tabs in double quotes with proper escaping.
The gateway's nodes 'run' action requires system.run.prepare to build
an execution plan before the actual run. Without it, the only path is
the 'invoke' action which has a hardcoded 30s gateway timeout — too
short for long-running tools like Copilot CLI.

system.run.prepare echoes back the argv, cwd, rawCommand, agentId,
and sessionKey without executing anything, enabling the gateway to
use its configurable timeout path for the actual execution.
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]>
Extract duplicated metacharacter quoting logic from LocalCommandRunner
and SystemCapability into a shared ShellQuoting static class, following
the established GatewayUrlHelper pattern.

- ShellQuoting.NeedsQuoting: single source of truth for metachar detection
- ShellQuoting.QuoteForShell: shell-aware quoting (cmd vs PowerShell)
- ShellQuoting.FormatExecCommand: display formatting for gateway
- IsShellMetachar: idiomatic switch expression instead of duplicated
  multi-case switch blocks
- 43 new unit tests covering all metacharacters, escaping edge cases,
  null handling, and both shell modes

Co-authored-by: danedane <[email protected]>
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: Copilot <[email protected]>
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