Commit 7a213e5
authored
🤖 fix: use stdin.abort() to prevent hangs on SSH commands (#504)
## Problem
Commands like `cat /tmp/results.json | jq '.'` would sometimes hang
forever over SSH, not respecting timeouts. This was observed over SSH
with the bash tool, and it's unclear if it happens on local workspaces.
## Root Cause
The issue stems from using `WritableStream.close()` to close stdin:
- **`close()` is async** and waits for graceful acknowledgment from the
remote end
- Over SSH with ControlMaster multiplexing, the SSH channel's stdin
buffer might not be immediately flushed
- The `close()` promise waits for acknowledgment that may never come
- Even timeouts couldn't help because the promise was already stuck in
Node.js stream machinery
## Solution
**Use `stdin.abort()` instead of `stdin.close()` for immediate
closure:**
- **`abort()`** - Immediate, synchronous force-close. Marks stream as
errored, releases locks, doesn't wait
- **`close()`** - Graceful, async close. Waits for all writes to be
acknowledged
Applied selectively:
- **bash tool**: Use `abort()` - commands don't need stdin at all
- **SSHRuntime mv/rm/stat**: Use `abort()` - commands don't read stdin
- **SSHRuntime writeFile**: Keep `close()` - needs to flush data written
to stdin
## Testing
- All 45 bash tool tests pass
- SSH integration tests pass:
- `runtimeExecuteBash` - verifies bash commands over SSH work
- `renameWorkspace` - verifies mv operations work
- `runtimeFileEditing` - verifies file writes (which use stdin) work
correctly
- All 921 unit tests pass
- Typecheck passes
## Implementation Details
Changed 4 locations:
1. `src/services/tools/bash.ts` - Use `abort()` to close stdin
immediately
2. `src/runtime/SSHRuntime.ts` (3 places) - Use `abort()` for mv/rm/stat
commands
Kept `close()` in 1 location:
- `SSHRuntime.writeFile()` - Needs graceful close to ensure all data is
written
---
_Generated with `cmux`_1 parent f664385 commit 7a213e5
File tree
4 files changed
+90
-7
lines changed- src
- runtime
- services/tools
- tests/ipcMain
4 files changed
+90
-7
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
929 | 929 | | |
930 | 930 | | |
931 | 931 | | |
932 | | - | |
| 932 | + | |
| 933 | + | |
933 | 934 | | |
934 | 935 | | |
935 | 936 | | |
| |||
999 | 1000 | | |
1000 | 1001 | | |
1001 | 1002 | | |
1002 | | - | |
| 1003 | + | |
| 1004 | + | |
1003 | 1005 | | |
1004 | 1006 | | |
1005 | 1007 | | |
| |||
1072 | 1074 | | |
1073 | 1075 | | |
1074 | 1076 | | |
1075 | | - | |
| 1077 | + | |
| 1078 | + | |
1076 | 1079 | | |
1077 | 1080 | | |
1078 | 1081 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
637 | 637 | | |
638 | 638 | | |
639 | 639 | | |
640 | | - | |
| 640 | + | |
| 641 | + | |
641 | 642 | | |
642 | 643 | | |
643 | 644 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
147 | 147 | | |
148 | 148 | | |
149 | 149 | | |
150 | | - | |
151 | | - | |
152 | | - | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
153 | 155 | | |
154 | 156 | | |
155 | 157 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
266 | 266 | | |
267 | 267 | | |
268 | 268 | | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
269 | 346 | | |
270 | 347 | | |
271 | 348 | | |
0 commit comments