Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix(windows): a concurrent duplicate -L forward shares the in-flight listen

review0f46a25f73e9
diff --git a/src/core/remote-ssh/native/native-invoke.test.ts b/src/core/remote-ssh/native/native-invoke.test.ts
index b4b80bee5..a111b9953 100644
--- a/src/core/remote-ssh/native/native-invoke.test.ts
+++ b/src/core/remote-ssh/native/native-invoke.test.ts
@@ -267,6 +267,23 @@ describe('native-invoke', () => {
}
})
+ it('two concurrent requests for one spec are both acknowledged (one listener, not a self-race)', async () => {
+ const { srv, port: remotePort } = await echo()
+ try {
+ const m = mux()
+ await runSshArgv(m, childArgs(conn(), CP(), 'true'))
+ const lport = await freePort()
+ const args = localForwardArgs(conn(), CP(), lport, '127.0.0.1', remotePort)
+ const [a, b] = await Promise.all([runSshArgv(m, args), runSshArgv(m, args)])
+ expect([a.code, b.code]).toEqual([0, 0])
+ expect(await roundTrip(lport, 'both')).toBe('both')
+ expect((await runSshArgv(m, localForwardCancelArgs(conn(), CP(), lport, '127.0.0.1', remotePort))).code).toBe(0)
+ expect(await canListen(lport)).toBe(true)
+ } finally {
+ srv.close()
+ }
+ })
+
it('a port someone else holds is OpenSSH\'s "Port forwarding failed", exit 255', async () => {
const m = mux()
await runSshArgv(m, childArgs(conn(), CP(), 'true'))
diff --git a/src/core/remote-ssh/native/native-mux.ts b/src/core/remote-ssh/native/native-mux.ts
index 0877bb515..3bc57a049 100644
--- a/src/core/remote-ssh/native/native-mux.ts
+++ b/src/core/remote-ssh/native/native-mux.ts
@@ -99,6 +99,8 @@ interface Conn {
/** `-L` listeners this connection holds, by spec (see `localForwardKey`). They close with it,
* as an OpenSSH master's listeners die with the master process. */
localForwards: Map<string, LocalListener>
+ /** `-L` specs whose listen is still in flight, so a concurrent duplicate shares its answer. */
+ localPending: Map<string, Promise<void>>
target: SshTarget
options: SshOptions
/** Channels open on this connection right now. */
@@ -529,8 +531,23 @@ export class NativeMux {
async forwardLocal(controlPath: string, fwd: LocalForward): Promise<void> {
const conn = this.conns.get(controlPath)
if (!conn?.alive) throw new Error(noMasterMessage(controlPath))
- await conn.ready
const key = localForwardKey(fwd)
+ // A second request for a spec whose listen is still in flight shares that answer — otherwise it
+ // would race our own listener to EADDRINUSE and report "Port forwarding failed" for a forward
+ // that is in fact ours (the duplicate OpenSSH would simply acknowledge).
+ const inFlight = conn.localPending.get(key)
+ if (inFlight) return inFlight
+ const p = this.listenLocal(controlPath, conn, fwd, key)
+ conn.localPending.set(key, p)
+ try {
+ await p
+ } finally {
+ if (conn.localPending.get(key) === p) conn.localPending.delete(key)
+ }
+ }
+
+ private async listenLocal(controlPath: string, conn: Conn, fwd: LocalForward, key: string): Promise<void> {
+ await conn.ready
if (conn.localForwards.has(key)) return
const sockets = new Set<net.Socket>()
const server = net.createServer((sock) => {
@@ -631,6 +648,7 @@ export class NativeMux {
closed,
forwards: new Map(),
localForwards: new Map(),
+ localPending: new Map(),
target,
options,
channels: 0,