Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

A paste whose tmux never takes its buffer gives up after 5 s (#962)

Dee608ec724b5c2
diff --git a/cli/src/lib/tmux.spec.ts b/cli/src/lib/tmux.spec.ts
index ee9a4a52d..fd2c2e095 100644
--- a/cli/src/lib/tmux.spec.ts
+++ b/cli/src/lib/tmux.spec.ts
@@ -11,6 +11,7 @@ import {
engineProcessMatch,
engineProcessMatchScore,
isNoTmuxServerError,
+ LOAD_BUFFER_TIMEOUT_MS,
LSTART_MARKER_RE,
liveProcessRows,
parseProcessRow,
@@ -710,4 +711,37 @@ if [ "$1" = "load-buffer" ]; then cat > /dev/null; fi
rmSync(dir, { recursive: true, force: true })
}
})
+
+ it('gives up a paste whose tmux never takes its buffer, so the terminals waiting behind it open', async () => {
+ // A tmux client whose server does not answer waits as long as the server does. Inside the notify room
+ // that held every terminal open, every session made or killed, and every terminal closed with it.
+ const dir = mkdtempSync(join(tmpdir(), 'harness-tmux-hung-'))
+ writeFileSync(join(dir, 'tmux'), `#!/bin/sh
+if [ "$1" = "load-buffer" ]; then exec sleep 30; fi
+`)
+ chmodSync(join(dir, 'tmux'), 0o700)
+ const previous = process.env.PATH
+ const error = vi.spyOn(console, 'error').mockImplementation(() => {})
+ try {
+ process.env.PATH = `${dir}:${previous ?? ''}`
+ assumeTmuxVersion({ major: 3, minor: 4 })
+ const started = performance.now()
+ const pasted = pasteRawIntoTmux('%7', 'clipboard')
+ await new Promise((resolve) => setTimeout(resolve, 100))
+ const opened = await Promise.race([
+ tmuxControlGate.enter('attach').then((leave) => { leave(); return performance.now() - started }),
+ new Promise<null>((resolve) => setTimeout(() => resolve(null), 12_000)),
+ ])
+ expect(opened, 'a terminal opens behind a paste whose tmux does not answer').not.toBeNull()
+ expect(opened).toBeGreaterThanOrEqual(LOAD_BUFFER_TIMEOUT_MS - 50)
+ expect(await pasted).toBe(false)
+ expect(error).toHaveBeenCalledWith('[tmux] load-buffer for %7 failed')
+ } finally {
+ error.mockRestore()
+ resetTmuxVersionCache()
+ if (previous === undefined) delete process.env.PATH
+ else process.env.PATH = previous
+ rmSync(dir, { recursive: true, force: true })
+ }
+ }, 20_000)
})
diff --git a/cli/src/lib/tmux.ts b/cli/src/lib/tmux.ts
index d1ea7ece7..1cb9ed6d7 100644
--- a/cli/src/lib/tmux.ts
+++ b/cli/src/lib/tmux.ts
@@ -18,7 +18,7 @@ import { BYPASS_PERMISSION_FLAGS, PERMISSION_MODES, permissionModeApproves } fro
import { psEnv } from './childLocale.js'
import { nativeProcessImages } from './nativeProcessImages.js'
import { neutralizePasteControls } from './pasteText.js'
-import { patientExec } from './patientExec.js'
+import { patientDeadline, patientExec } from './patientExec.js'
import { inTmuxRoom } from './tmuxControlGate.js'
import { tmuxFeatures, type TmuxFeatures } from './tmuxVersion.js'
export { captureTmuxPane, tmuxCaptureArgs } from './tmuxCapture.js'
@@ -1153,12 +1153,33 @@ function tmuxEnter(pane: string): Promise<boolean> {
})
}
+/**
+ * How long a buffer may take to load before the paste is given up. A tmux client waits as long as its
+ * server does not answer, and this one runs in the gate's notify room (tmuxControlGate.ts): unbounded, it
+ * held every terminal open, session made or killed, and terminal closed behind it for as long. The other
+ * tmux commands here get 2 s; this one also carries the text over stdin.
+ */
+export const LOAD_BUFFER_TIMEOUT_MS = 5_000
+
/** Stage text in a NAMED tmux buffer via stdin (avoids arg-length limits + the user's default buffer). */
function tmuxLoadBuffer(name: string, content: string): Promise<boolean> {
return new Promise((resolve) => {
+ let settled = false
+ let cancel = (): void => {}
+ const settle = (loaded: boolean): void => {
+ if (settled) return
+ settled = true
+ cancel()
+ resolve(loaded)
+ }
const c = spawn('tmux', ['load-buffer', '-b', name, '-'])
- c.on('error', () => resolve(false))
- c.on('close', (code) => resolve(code === 0))
+ // Answered without waiting for its pipes to close: a client killed here may leave one open behind it.
+ cancel = patientDeadline(LOAD_BUFFER_TIMEOUT_MS, () => {
+ c.kill('SIGKILL')
+ settle(false)
+ })
+ c.on('error', () => settle(false))
+ c.on('close', (code) => settle(code === 0))
c.stdin.on('error', () => { /* EPIPE if tmux died first — 'close' still resolves false */ })
c.stdin.end(content)
})