Compare commits

...
2 changed files with 533 additions and 90 deletions

No files matched your search

@@ -5,6 +5,7 @@ const Module = require("node:module");
const {
createConnectionRef,
acquireConnectionRef,
releaseConnectionRef,
resetSshTransportRegistryForTests,
} = require("./sshConnectionPool.cjs");
@@ -431,6 +432,9 @@ test("Copy Tab records a distinct remote shell for each shared terminal", async
const sessions = new Map();
const sourceConn = makePidTrackingReusableConn();
const source = makeSourceSession(sourceConn, { hostname: "10.0.0.1", username: "alice" });
// Live tabs normally already know their login shellPid (cwd / sessionOps).
// Copy Tab must not burn a discovery exec before opening the reused shell.
source.shellPid = "111";
sessions.set("source", source);
const start = registerStartHandler(bridge, sessions);
@@ -441,10 +445,12 @@ test("Copy Tab records a distinct remote shell for each shared terminal", async
hostname: "10.0.0.1",
username: "alice",
sourceSessionId: "source",
sshChannelOpenRateLimitBackoffMs: 1,
},
);
assert.deepEqual(sourceConn.shellPidSnapshots, [["111"], ["111", "222"]]);
assert.ok(sourceConn.shellPidSnapshots.length >= 1);
assert.deepEqual(sourceConn.shellPidSnapshots.at(-1), ["111", "222"]);
assert.equal(source.shellPid, "111");
assert.equal(sessions.get("copy").shellPid, "222");
});
@@ -484,6 +490,7 @@ test("Copy Tab with jump hosts still discovers shell PIDs after a short delay",
username: "alice",
jumpHosts,
});
source.shellPid = "111";
sessions.set("source", source);
const start = registerStartHandler(bridge, sessions);
@@ -499,20 +506,20 @@ test("Copy Tab with jump hosts still discovers shell PIDs after a short delay",
},
);
assert.ok(sourceConn.shellPidSnapshots.length >= 2, "jump-host reuse must still run discovery");
assert.ok(sourceConn.shellPidSnapshots.length >= 1, "jump-host reuse must still run post-open discovery");
assert.equal(source.shellPid, "111");
assert.equal(sessions.get("copy").shellPid, "222");
assert.equal(sourceConn.openedShells.length, 1);
});
test("Copy Tab retries rate-limited pre-open shell PID discovery on jump hosts", async (t) => {
test("Copy Tab retries rate-limited post-open shell PID discovery on jump hosts", async (t) => {
const { bridge } = loadBridgeWithMockedSsh2(t);
const sessions = new Map();
const sourceConn = makeReusableConn();
let execCalls = 0;
sourceConn.exec = (_command, callback) => {
execCalls += 1;
// First pre-open scan is rate-limited; later scans succeed.
// First post-open scan is rate-limited; later scans succeed.
if (execCalls === 1) {
callback(new Error("(SSH) Channel open failure: channelOpen too offen type=session"));
return;
@@ -520,8 +527,7 @@ test("Copy Tab retries rate-limited pre-open shell PID discovery on jump hosts",
const stream = new EventEmitter();
stream.stderr = new EventEmitter();
stream.close = () => {};
const snapshot = execCalls === 2 ? ["111"] : ["111", "222"];
const pids = `${snapshot.join("\n")}\n__NETCATTY_SHELL_SCAN_COMPLETE__\n`;
const pids = "111\n222\n__NETCATTY_SHELL_SCAN_COMPLETE__\n";
setImmediate(() => {
stream.emit("data", Buffer.from(pids));
stream.emit("close", 0);
@@ -534,6 +540,7 @@ test("Copy Tab retries rate-limited pre-open shell PID discovery on jump hosts",
username: "alice",
jumpHosts,
});
source.shellPid = "111";
sessions.set("source", source);
const start = registerStartHandler(bridge, sessions);
@@ -549,7 +556,7 @@ test("Copy Tab retries rate-limited pre-open shell PID discovery on jump hosts",
},
);
assert.ok(execCalls >= 3, "pre-open discovery must retry after channelOpen too offen");
assert.ok(execCalls >= 2, "post-open discovery must retry after channelOpen too offen");
assert.equal(source.shellPid, "111");
assert.equal(sessions.get("copy").shellPid, "222");
});
@@ -561,7 +568,7 @@ test("Copy Tab retries rate-limited PID discovery for direct bastion targets", a
let execCalls = 0;
sourceConn.exec = (_command, callback) => {
execCalls += 1;
// Direct bastion (no jumpHosts): first pre-open scan is rate-limited.
// Direct bastion (no jumpHosts): first post-open scan is rate-limited.
if (execCalls === 1) {
callback(new Error("(SSH) Channel open failure: channelOpen too offen type=session"));
return;
@@ -569,18 +576,19 @@ test("Copy Tab retries rate-limited PID discovery for direct bastion targets", a
const stream = new EventEmitter();
stream.stderr = new EventEmitter();
stream.close = () => {};
const snapshot = execCalls === 2 ? ["111"] : ["111", "222"];
const pids = `${snapshot.join("\n")}\n__NETCATTY_SHELL_SCAN_COMPLETE__\n`;
const pids = "111\n222\n__NETCATTY_SHELL_SCAN_COMPLETE__\n";
setImmediate(() => {
stream.emit("data", Buffer.from(pids));
stream.emit("close", 0);
});
callback(null, stream);
};
sessions.set("source", makeSourceSession(sourceConn, {
const source = makeSourceSession(sourceConn, {
hostname: "bastion.example",
username: "alice",
}));
});
source.shellPid = "111";
sessions.set("source", source);
const start = registerStartHandler(bridge, sessions);
await start(
@@ -594,27 +602,23 @@ test("Copy Tab retries rate-limited PID discovery for direct bastion targets", a
},
);
assert.ok(execCalls >= 3, "direct bastion reuse must retry rate-limited PID discovery");
assert.ok(execCalls >= 2, "direct bastion reuse must retry rate-limited PID discovery");
assert.equal(sessions.get("source").shellPid, "111");
assert.equal(sessions.get("copy").shellPid, "222");
});
test("Copy Tab retries rate-limited post-open shell PID discovery on jump hosts", async (t) => {
test("Copy Tab claims an unambiguous leftover PID when the source never recorded one", async (t) => {
const { bridge } = loadBridgeWithMockedSsh2(t);
const sessions = new Map();
const sourceConn = makeReusableConn();
let execCalls = 0;
sourceConn.exec = (_command, callback) => {
execCalls += 1;
// Pre-open discovery (1) succeeds with the source shell. The first
// post-open discovery (2) is rate-limited; later attempts succeed.
if (execCalls === 2) {
callback(new Error("(SSH) Channel open failure: channelOpen too offen type=session"));
return;
}
const stream = new EventEmitter();
stream.stderr = new EventEmitter();
stream.close = () => {};
// First post-open snapshot still only sees the source shell; later scans
// include the copied shell so waitForNewInteractiveShellPid can finish.
const snapshot = execCalls === 1 ? ["111"] : ["111", "222"];
const pids = `${snapshot.join("\n")}\n__NETCATTY_SHELL_SCAN_COMPLETE__\n`;
setImmediate(() => {
@@ -623,14 +627,267 @@ test("Copy Tab retries rate-limited post-open shell PID discovery on jump hosts"
});
callback(null, stream);
};
const jumpHosts = [{ hostname: "bastion.example", username: "jump", port: 22 }];
sessions.set("source", makeSourceSession(sourceConn, {
hostname: "10.0.0.1",
username: "alice",
}));
const start = registerStartHandler(bridge, sessions);
await start(
{ sender: makeSender() },
{
sessionId: "copy",
hostname: "10.0.0.1",
username: "alice",
sourceSessionId: "source",
sshChannelOpenRateLimitBackoffMs: 1,
},
);
assert.ok(execCalls >= 2);
assert.equal(sessions.get("source").shellPid, "111");
assert.equal(sessions.get("copy").shellPid, "222");
});
test("Copy Tab waits before claiming a sole PID after the source closes", async (t) => {
const { bridge } = loadBridgeWithMockedSsh2(t);
const terminalBridge = require("./terminalBridge.cjs");
const sessions = new Map();
const sourceConn = makeDeferredShellConn();
let execCalls = 0;
sourceConn.exec = (_command, callback) => {
execCalls += 1;
const stream = new EventEmitter();
stream.stderr = new EventEmitter();
stream.close = () => {};
// Source tab already closed: early scans can still list the dying source
// shell before the copied shell is visible.
const snapshot = execCalls < 3 ? ["111"] : ["222"];
const pids = `${snapshot.join("\n")}\n__NETCATTY_SHELL_SCAN_COMPLETE__\n`;
setImmediate(() => {
stream.emit("data", Buffer.from(pids));
stream.emit("close", 0);
});
callback(null, stream);
};
const source = makeSourceSession(sourceConn, {
hostname: "10.0.0.1",
username: "alice",
jumpHosts,
});
sessions.set("source", source);
terminalBridge.init({ sessions, electronModule: {} });
const start = registerStartHandler(bridge, sessions);
const startPromise = start(
{ sender: makeSender() },
{
sessionId: "copy",
hostname: "10.0.0.1",
username: "alice",
sourceSessionId: "source",
sshChannelOpenRateLimitBackoffMs: 1,
},
);
await new Promise((resolve) => setImmediate(resolve));
terminalBridge.closeSession({ sender: {} }, { sessionId: "source" });
sourceConn.flushShell();
await startPromise;
assert.ok(execCalls >= 3, "must wait through the dying-source-only scans");
assert.equal(sessions.get("copy").shellPid, "222");
});
test("Copy Tab disambiguates a two-PID first scan when the source never recorded shellPid", async (t) => {
const { bridge } = loadBridgeWithMockedSsh2(t);
const sessions = new Map();
const sourceConn = makeReusableConn();
let execCalls = 0;
sourceConn.exec = (_command, callback) => {
execCalls += 1;
const stream = new EventEmitter();
stream.stderr = new EventEmitter();
stream.close = () => {};
// OSC 7 sources skip the cwd probe, so shellPid is unset. The copied shell
// is already visible in the first post-open scan — no gradual appearance.
// Ages (etimes seconds; higher = older) distinguish source from copy.
const pids = "111 40\n222 1\n__NETCATTY_SHELL_SCAN_COMPLETE__\n";
setImmediate(() => {
stream.emit("data", Buffer.from(pids));
stream.emit("close", 0);
});
callback(null, stream);
};
sessions.set("source", makeSourceSession(sourceConn, {
hostname: "10.0.0.1",
username: "alice",
}));
const start = registerStartHandler(bridge, sessions);
await start(
{ sender: makeSender() },
{
sessionId: "copy",
hostname: "10.0.0.1",
username: "alice",
sourceSessionId: "source",
sshChannelOpenRateLimitBackoffMs: 1,
},
);
assert.equal(execCalls, 1, "must not require waitForNew retries once both PIDs are visible");
assert.equal(sessions.get("source").shellPid, "111");
assert.equal(sessions.get("copy").shellPid, "222");
});
test("Copy Tab uses process age when PID wrap makes the copy numerically smaller", async (t) => {
const { bridge } = loadBridgeWithMockedSsh2(t);
const sessions = new Map();
const sourceConn = makeReusableConn();
sourceConn.exec = (_command, callback) => {
const stream = new EventEmitter();
stream.stderr = new EventEmitter();
stream.close = () => {};
// After PID wrap the copied shell can receive a lower numeric PID while
// still being the younger process (etimes 1 vs 90).
const pids = "50 1\n4000 90\n__NETCATTY_SHELL_SCAN_COMPLETE__\n";
setImmediate(() => {
stream.emit("data", Buffer.from(pids));
stream.emit("close", 0);
});
callback(null, stream);
};
sessions.set("source", makeSourceSession(sourceConn, {
hostname: "10.0.0.1",
username: "alice",
}));
const start = registerStartHandler(bridge, sessions);
await start(
{ sender: makeSender() },
{
sessionId: "copy",
hostname: "10.0.0.1",
username: "alice",
sourceSessionId: "source",
sshChannelOpenRateLimitBackoffMs: 1,
},
);
assert.equal(sessions.get("source").shellPid, "4000");
assert.equal(sessions.get("copy").shellPid, "50");
});
test("Copy Tab leaves PIDs unassigned when both shells share the same etimes second", async (t) => {
const { bridge } = loadBridgeWithMockedSsh2(t);
const sessions = new Map();
const sourceConn = makeReusableConn();
sourceConn.exec = (_command, callback) => {
const stream = new EventEmitter();
stream.stderr = new EventEmitter();
stream.close = () => {};
// Same whole-second etimes tick: age inequality cannot separate them, and
// numeric PID order is unsafe after wrap — leave both unassigned.
const pids = "111 1\n222 1\n__NETCATTY_SHELL_SCAN_COMPLETE__\n";
setImmediate(() => {
stream.emit("data", Buffer.from(pids));
stream.emit("close", 0);
});
callback(null, stream);
};
sessions.set("source", makeSourceSession(sourceConn, {
hostname: "10.0.0.1",
username: "alice",
}));
const start = registerStartHandler(bridge, sessions);
await start(
{ sender: makeSender() },
{
sessionId: "copy",
hostname: "10.0.0.1",
username: "alice",
sourceSessionId: "source",
sshChannelOpenRateLimitBackoffMs: 1,
},
);
assert.equal(sessions.get("source").shellPid, undefined);
assert.equal(sessions.get("copy").shellPid, undefined);
});
test("Copy Tab leaves PIDs unassigned when remote ps lacks etimes", async (t) => {
const { bridge } = loadBridgeWithMockedSsh2(t);
const sessions = new Map();
const sourceConn = makeReusableConn();
sourceConn.exec = (_command, callback) => {
const stream = new EventEmitter();
stream.stderr = new EventEmitter();
stream.close = () => {};
// Age-less "pid" lines only — hosts without etimes. Do not guess via PID
// order; a wrap between source and copy would swap the assignment.
const pids = "111\n222\n__NETCATTY_SHELL_SCAN_COMPLETE__\n";
setImmediate(() => {
stream.emit("data", Buffer.from(pids));
stream.emit("close", 0);
});
callback(null, stream);
};
sessions.set("source", makeSourceSession(sourceConn, {
hostname: "10.0.0.1",
username: "alice",
}));
const start = registerStartHandler(bridge, sessions);
await start(
{ sender: makeSender() },
{
sessionId: "copy",
hostname: "10.0.0.1",
username: "alice",
sourceSessionId: "source",
sshChannelOpenRateLimitBackoffMs: 1,
},
);
assert.equal(sessions.get("source").shellPid, undefined);
assert.equal(sessions.get("copy").shellPid, undefined);
});
test("Copy Tab reconciles an untracked source even when another sibling PID is known", async (t) => {
const { bridge } = loadBridgeWithMockedSsh2(t);
const sessions = new Map();
const sourceConn = makeReusableConn();
sourceConn.exec = (_command, callback) => {
const stream = new EventEmitter();
stream.stderr = new EventEmitter();
stream.close = () => {};
// Tracked sibling 100 plus untracked source 200 and new copy 300.
const pids = "100 120\n200 40\n300 1\n__NETCATTY_SHELL_SCAN_COMPLETE__\n";
setImmediate(() => {
stream.emit("data", Buffer.from(pids));
stream.emit("close", 0);
});
callback(null, stream);
};
const source = makeSourceSession(sourceConn, {
hostname: "10.0.0.1",
username: "alice",
});
const tracked = {
conn: sourceConn,
stream: makeStream(),
chainConnections: [],
webContentsId: 1,
zmodemSentry: { cancel() {} },
hostname: "10.0.0.1",
username: "alice",
_reuseEndpoint: source._reuseEndpoint,
shellPid: "100",
};
acquireConnectionRef(tracked, source.connRef);
sessions.set("tracked", tracked);
sessions.set("source", source);
const start = registerStartHandler(bridge, sessions);
await start(
{ sender: makeSender() },
@@ -639,14 +896,13 @@ test("Copy Tab retries rate-limited post-open shell PID discovery on jump hosts"
hostname: "10.0.0.1",
username: "alice",
sourceSessionId: "source",
jumpHosts,
sshChannelOpenRateLimitBackoffMs: 1,
},
);
assert.ok(execCalls >= 3, "post-open discovery must retry after channelOpen too offen");
assert.equal(source.shellPid, "111");
assert.equal(sessions.get("copy").shellPid, "222");
assert.equal(sessions.get("tracked").shellPid, "100");
assert.equal(sessions.get("source").shellPid, "200");
assert.equal(sessions.get("copy").shellPid, "300");
});
test("Copy Tab retries bastion channelOpen too offen before falling back", async (t) => {
@@ -692,11 +948,78 @@ test("Copy Tab retries bastion channelOpen too offen before falling back", async
assert.equal(getConnectionReuseFallbackEvents(sender).length, 0);
});
test("Copy Tab opens the shell before PID discovery so bastion rate limits do not burn the session slot", async (t) => {
const { bridge, getClientConstructCount } = loadBridgeWithMockedSsh2(t);
const sessions = new Map();
const sourceConn = makeReusableConn();
const channelOrder = [];
let execCalls = 0;
sourceConn.exec = (_command, callback) => {
execCalls += 1;
channelOrder.push("exec");
// Bastions rate-limit rapid session channels. If discovery runs first it
// burns the budget and every subsequent shell open is rejected.
if (sourceConn.openedShells.length === 0) {
callback(new Error("(SSH) Channel open failure: channelOpen too offen type=session"));
return;
}
const stream = new EventEmitter();
stream.stderr = new EventEmitter();
stream.close = () => {};
const snapshot = ["111", "222"];
const pids = `${snapshot.join("\n")}\n__NETCATTY_SHELL_SCAN_COMPLETE__\n`;
setImmediate(() => {
stream.emit("data", Buffer.from(pids));
stream.emit("close", 0);
});
callback(null, stream);
};
sourceConn.shell = (_opts, _shellOpts, cb) => {
channelOrder.push("shell");
if (execCalls > 0 && sourceConn.openedShells.length === 0) {
setImmediate(() => cb(new Error("(SSH) Channel open failure: channelOpen too offen type=session")));
return;
}
const stream = makeStream();
sourceConn.openedShells.push(stream);
setImmediate(() => cb(null, stream));
};
const source = makeSourceSession(sourceConn, {
hostname: "bastion.example",
username: "alice",
});
source.shellPid = "111";
sessions.set("source", source);
const start = registerStartHandler(bridge, sessions);
const sender = makeSender();
const result = await start(
{ sender },
{
sessionId: "copy",
hostname: "bastion.example",
username: "alice",
sourceSessionId: "source",
sshChannelOpenRateLimitBackoffMs: 1,
},
);
assert.equal(result.sessionId, "copy");
assert.equal(getClientConstructCount(), 0);
assert.equal(sourceConn.openedShells.length, 1);
assert.equal(channelOrder[0], "shell", "shell must open before any discovery exec");
assert.ok(execCalls >= 1, "PID discovery still runs after the shell opens");
assert.equal(sessions.get("copy").shellPid, "222");
assert.equal(getConnectionReuseFallbackEvents(sender).length, 0);
});
test("Copy Tab waits briefly when the new remote shell is not visible immediately", async (t) => {
const { bridge } = loadBridgeWithMockedSsh2(t);
const sessions = new Map();
const sourceConn = makePidTrackingReusableConn({ delayFirstNewPid: true });
sessions.set("source", makeSourceSession(sourceConn, { hostname: "10.0.0.1", username: "alice" }));
const source = makeSourceSession(sourceConn, { hostname: "10.0.0.1", username: "alice" });
source.shellPid = "111";
sessions.set("source", source);
const start = registerStartHandler(bridge, sessions);
await start(
@@ -706,10 +1029,13 @@ test("Copy Tab waits briefly when the new remote shell is not visible immediatel
hostname: "10.0.0.1",
username: "alice",
sourceSessionId: "source",
sshChannelOpenRateLimitBackoffMs: 1,
},
);
assert.deepEqual(sourceConn.shellPidSnapshots, [["111"], ["111"], ["111", "222"]]);
assert.ok(sourceConn.shellPidSnapshots.length >= 2);
assert.deepEqual(sourceConn.shellPidSnapshots[0], ["111"]);
assert.deepEqual(sourceConn.shellPidSnapshots.at(-1), ["111", "222"]);
assert.equal(sessions.get("copy").shellPid, "222");
});
@@ -760,17 +1086,31 @@ test("concurrent Copy Tab requests serialize shell discovery per connection", as
const { bridge } = loadBridgeWithMockedSsh2(t);
const sessions = new Map();
const sourceConn = makePidTrackingReusableConn();
sessions.set("source", makeSourceSession(sourceConn, { hostname: "10.0.0.1", username: "alice" }));
const source = makeSourceSession(sourceConn, { hostname: "10.0.0.1", username: "alice" });
source.shellPid = "111";
sessions.set("source", source);
const start = registerStartHandler(bridge, sessions);
await Promise.all([
start(
{ sender: makeSender() },
{ sessionId: "copy-1", hostname: "10.0.0.1", username: "alice", sourceSessionId: "source" },
{
sessionId: "copy-1",
hostname: "10.0.0.1",
username: "alice",
sourceSessionId: "source",
sshChannelOpenRateLimitBackoffMs: 1,
},
),
start(
{ sender: makeSender() },
{ sessionId: "copy-2", hostname: "10.0.0.1", username: "alice", sourceSessionId: "source" },
{
sessionId: "copy-2",
hostname: "10.0.0.1",
username: "alice",
sourceSessionId: "source",
sshChannelOpenRateLimitBackoffMs: 1,
},
),
]);
@@ -803,11 +1143,23 @@ test("concurrent copies keep distinct shell IDs when the source closes immediate
const copies = Promise.all([
start(
{ sender: makeSender() },
{ sessionId: "copy-1", hostname: "10.0.0.1", username: "alice", sourceSessionId: "source" },
{
sessionId: "copy-1",
hostname: "10.0.0.1",
username: "alice",
sourceSessionId: "source",
sshChannelOpenRateLimitBackoffMs: 1,
},
),
start(
{ sender: makeSender() },
{ sessionId: "copy-2", hostname: "10.0.0.1", username: "alice", sourceSessionId: "source" },
{
sessionId: "copy-2",
hostname: "10.0.0.1",
username: "alice",
sourceSessionId: "source",
sshChannelOpenRateLimitBackoffMs: 1,
},
),
]);
+148 -57
View File
@@ -191,16 +191,22 @@ function createStartSessionApi(ctx) {
with (ctx) {
const listInteractiveShellPids = async (conn) => {
if (!conn || typeof conn.exec !== "function") {
return Promise.resolve({ available: false, pids: [] });
return Promise.resolve({ available: false, pids: [], ages: {} });
}
const scanCompleteMarker = "__NETCATTY_SHELL_SCAN_COMPLETE__";
// Emit "pid etimes" when possible. etimes (elapsed seconds) is a
// wrap-safe ordering key: higher means older. Plain "pid" remains valid
// for hosts that lack etimes.
const script = `SELF=$$
ps_output=$(ps -e -o pid=,ppid=,tty=,comm= 2>/dev/null) || exit 69
ps_output=$(ps -e -o pid=,ppid=,tty=,comm=,etimes= 2>/dev/null) || ps_output=$(ps -e -o pid=,ppid=,tty=,comm= 2>/dev/null) || exit 69
{
printf '%s\n' "$ps_output" | awk -v pp="$PPID" -v self="$SELF" '
function isshell(c) { sub(/^.*\\//, "", c); sub(/^-/, "", c); return c ~ /^(ba|z|fi|k|da|a|c|tc)?sh$/ }
$1 != self && $2 == pp && $3 !~ /^\\?+$/ && isshell($4) { print $1 }
$1 != self && $2 == pp && $3 !~ /^\\?+$/ && isshell($4) {
if (NF >= 5 && $5 ~ /^[0-9]+$/) print $1, $5+0
else print $1
}
'
if [ -r /proc/$SELF/environ ]; then
conn=$(tr '\\0' '\\n' < /proc/$SELF/environ 2>/dev/null | sed -n 's/^SSH_CONNECTION=//p' | head -n1)
@@ -217,11 +223,16 @@ ps_output=$(ps -e -o pid=,ppid=,tty=,comm= 2>/dev/null) || exit 69
pcomm=$(cat "/proc/$ppid/comm" 2>/dev/null)
case "$pcomm" in sshd|dropbear|dropbearmulti) ;; *) continue ;; esac
tty=$(ps -p "$pid" -o tty= 2>/dev/null | tr -d '[:space:]')
[ -n "$tty" ] && [ "$tty" != "?" ] && printf '%s\\n' "$pid"
[ -n "$tty" ] && [ "$tty" != "?" ] || continue
etimes=$(ps -p "$pid" -o etimes= 2>/dev/null | tr -d '[:space:]')
case "$etimes" in
''|*[!0-9]*) printf '%s\\n' "$pid" ;;
*) printf '%s %s\\n' "$pid" "$etimes" ;;
esac
done
fi
fi
} | awk '/^[0-9]+$/ && !seen[$1]++ { print $1 }'
} | awk '/^[0-9]+/ && !seen[$1]++ { print }'
printf '%s\n' '${scanCompleteMarker}'`;
try {
@@ -237,15 +248,24 @@ printf '%s\n' '${scanCompleteMarker}'`;
const lines = result.stdout.split(/\r?\n/);
const completed = lines.includes(scanCompleteMarker);
const available = completed && (result.code === null || result.code === 0);
return {
available,
pids: available ? lines.filter((value) => /^\d+$/.test(value)) : [],
};
const pids = [];
const ages = {};
if (available) {
for (const line of lines) {
const match = /^(\d+)(?:\s+(\d+))?$/.exec(String(line || "").trim());
if (!match) continue;
const pid = match[1];
if (!pids.includes(pid)) pids.push(pid);
if (match[2] !== undefined) ages[pid] = Number(match[2]);
}
}
return { available, pids, ages };
} catch (error) {
return {
available: false,
rateLimited: isSshChannelOpenRateLimitedError(error),
pids: [],
ages: {},
};
}
};
@@ -700,48 +720,17 @@ printf '%s\n' '${scanCompleteMarker}'`;
const rows = options.rows || 24;
const sender = event.sender;
const conn = sourceSession.conn;
let discoveryConnectionError = null;
const onDiscoveryConnectionError = (err) => {
discoveryConnectionError = err;
};
let shellDiscoveryBeforeOpen = { available: false, pids: [] };
// Bastions (whether configured as jumpHosts or used as the direct SSH
// target) often rate-limit rapid session channel opens ("channelOpen too
// offen"). Retry rate-limited PID scans on every Copy Tab reuse so a
// successful shell open still gets shellPid tracking. Only the optional
// pre-scan pause is jump-host specific to avoid delaying the happy path
// on ordinary direct hosts.
const hasJumpHosts = Array.isArray(options.jumpHosts) && options.jumpHosts.length > 0;
// Bastions (jumpHosts or direct targets) rate-limit rapid session channel
// opens ("channelOpen too offen"). Opening a discovery exec *before* the
// shell burns that budget and Copy Tab falls back to a fresh login
// (issue #2704). Open the shell first; discover shellPid afterwards.
const configuredBackoffMs = Number(options.sshChannelOpenRateLimitBackoffMs);
const discoveryBackoffMs = Number.isFinite(configuredBackoffMs) && configuredBackoffMs > 0
? configuredBackoffMs
: 150;
if (!options.skipShellPidDiscovery) {
conn.once("error", onDiscoveryConnectionError);
shellDiscoveryBeforeOpen = await listInteractiveShellPidsResilient(conn, {
initialDelayMs: hasJumpHosts ? discoveryBackoffMs : 0,
attempts: 4,
backoffMs: discoveryBackoffMs,
});
conn.removeListener("error", onDiscoveryConnectionError);
}
const shellPidsBeforeOpen = shellDiscoveryBeforeOpen.pids;
if (discoveryConnectionError) {
releaseConnectionRef(refHolder);
throw discoveryConnectionError;
}
const assignedPids = new Set(
[...sessions.values()]
.filter((candidate) => candidate?.connRef === connRef && candidate.shellPid)
.map((candidate) => String(candidate.shellPid)),
);
const unclaimedPids = shellPidsBeforeOpen.filter((pid) => !assignedPids.has(pid));
const unassignedSessions = [...sessions.values()].filter(
(candidate) => candidate?.connRef === connRef && !candidate.shellPid,
);
if (unclaimedPids.length === 1 && unassignedSessions.length === 1) {
unassignedSessions[0].shellPid = unclaimedPids[0];
}
const shellPidsBeforeOpen = [...sessions.values()]
.filter((candidate) => candidate?.connRef === connRef && candidate.shellPid)
.map((candidate) => String(candidate.shellPid));
log("reusing existing connection for new shell channel", {
sessionId,
@@ -856,17 +845,119 @@ printf '%s\n' '${scanCompleteMarker}'`;
} else {
refHolder.connRef = null;
}
const newShellPidPromise = shellDiscoveryBeforeOpen.available
? waitForNewInteractiveShellPid(
conn,
shellPidsBeforeOpen,
{
initialDelayMs: hasJumpHosts ? discoveryBackoffMs : 0,
const discoverCopiedShellPid = async () => {
if (options.skipShellPidDiscovery) return null;
const liveBaseline = () => [...sessions.values()]
.filter((candidate) => (
candidate?.connRef === connRef
&& candidate !== copiedSession
&& candidate.shellPid
))
.map((candidate) => String(candidate.shellPid));
const listUnassignedSiblings = () => [...sessions.values()].filter(
(candidate) => (
candidate?.connRef === connRef
&& candidate !== copiedSession
&& !candidate.shellPid
),
);
// Prefer PIDs already recorded on sibling tabs of this shared
// transport. Fall back to the pre-shell snapshot when the source
// closed before discovery runs but had a known shellPid.
let baseline = liveBaseline();
if (baseline.length === 0) {
baseline = shellPidsBeforeOpen;
}
// Also reconcile when some siblings are already tracked but the
// copy source (or another tab) still lacks shellPid — otherwise
// waitForNew sees multiple "new" PIDs and returns null.
const needsUntrackedReconcile = listUnassignedSiblings().length > 0;
if (baseline.length === 0 || needsUntrackedReconcile) {
const discovery = await listInteractiveShellPidsResilient(conn, {
initialDelayMs: discoveryBackoffMs,
attempts: 4,
backoffMs: discoveryBackoffMs,
},
)
: Promise.resolve(null);
void newShellPidPromise.then((newShellPid) => {
});
if (!discovery.available && !discovery.rateLimited) {
// Discovery is permanently unavailable (not rate-limited).
return null;
}
if (discovery.available && discovery.pids.length > 0) {
const assignedPids = new Set(liveBaseline());
for (const pid of baseline) assignedPids.add(String(pid));
const unclaimed = discovery.pids.filter((pid) => !assignedPids.has(pid));
const unassignedSiblings = listUnassignedSiblings();
if (unclaimed.length === 1 && unassignedSiblings.length === 1) {
unassignedSiblings[0].shellPid = unclaimed[0];
baseline = liveBaseline();
} else if (unassignedSiblings.length === 0 && unclaimed.length === 1) {
// Sole unclaimed PID is ambiguous once the source tab is
// gone: the closing source process may still be listed
// while the copied shell has not appeared yet. Wait for a
// PID beyond that candidate; if none appears and the
// candidate remains the only unclaimed shell, it is the
// copy (source process already exited).
const candidatePid = String(unclaimed[0]);
const waited = await waitForNewInteractiveShellPid(conn, [candidatePid], {
initialDelayMs: discoveryBackoffMs,
backoffMs: discoveryBackoffMs,
});
if (waited) return waited;
const recheck = await listInteractiveShellPids(conn);
if (!recheck.available) return null;
const assigned = new Set(liveBaseline());
const remaining = recheck.pids
.map(String)
.filter((pid) => !assigned.has(pid));
if (remaining.length === 1) return remaining[0];
return null;
} else if (unassignedSiblings.length === 1 && unclaimed.length === 2) {
// Source never recorded shellPid (e.g. OSC 7 cwd skipped
// the probe), so the first post-open scan already lists
// both shared shells. Reintroducing a pre-open exec would
// burn bastion channel budget (#2704). Disambiguate by
// process age (etimes): login shells on one transport are
// created sequentially, so the copied shell is younger.
// Numeric PID order is not a timestamp and fails when the
// PID allocator wraps between source and copy.
const ages = discovery.ages || {};
const left = String(unclaimed[0]);
const right = String(unclaimed[1]);
const leftAge = ages[left];
const rightAge = ages[right];
if (
Number.isFinite(leftAge)
&& Number.isFinite(rightAge)
&& leftAge !== rightAge
) {
const older = leftAge > rightAge ? left : right;
const newer = older === left ? right : left;
unassignedSiblings[0].shellPid = older;
return newer;
}
// Ages tied (same etimes second) or unavailable: refuse
// numeric PID order — after wrap the lower PID can be the
// copy. Leave the pair unassigned and keep only already-
// tracked PIDs in the baseline so waitForNew can still
// claim the copy if one of the two later disappears.
baseline = [...assignedPids];
} else if (assignedPids.size > 0) {
baseline = [...assignedPids];
}
}
// Empty successful scans (shell not visible yet) must not
// abort — waitForNew retries until the new shell appears.
}
return waitForNewInteractiveShellPid(conn, baseline, {
// Brief pause after the shell channel so bastion rate limits
// have a chance to clear before the discovery exec.
initialDelayMs: discoveryBackoffMs,
backoffMs: discoveryBackoffMs,
});
};
void discoverCopiedShellPid().then((newShellPid) => {
// Bind PID only to the session this reuse opened. A higher
// bootEpoch reconnect may already own sessionId in the map.
const liveSession = sessions.get(sessionId);