mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-23 00:20:45 +00:00
fix(ssh): cancel background jobs via in-band Ctrl+C (#3725)
Deliver PTY job interrupts as terminal data instead of sending SSH signal channel requests. This addresses the background-cancellation log noise reported in #3577 while preserving the existing cancellation retries and result semantics. Add regression coverage asserting cancellation writes ETX without invoking the stream signal method.
This commit is contained in:
1 parent
f63be66ede
commit
ebd77e43bb
2 files changed
+38
-10
No files matched your search
@@ -254,17 +254,14 @@ function startPtyJob(ptyStream, command, options) {
|
||||
startupTimeoutMs: maxBufferedChars > 0 ? BG_STARTUP_TIMEOUT_MS : timeoutMs,
|
||||
});
|
||||
|
||||
// Deliver the interrupt as terminal data (ETX). This matches manual Ctrl+C
|
||||
// in the terminal and terminalBridge.interruptSession: the remote line
|
||||
// discipline turns \x03 into SIGINT. Never send an SSH "signal" channel
|
||||
// request here — real OpenSSH rejects it ("session_signal_req: session
|
||||
// signalling requires privilege separation"), which pollutes auth logs and
|
||||
// makes other host-side tooling report brute-force noise (#3577).
|
||||
function sendInterrupt(trigger = null) {
|
||||
let signalSent = false;
|
||||
let etxWritten = false;
|
||||
try {
|
||||
if (typeof ptyStream.signal === "function") {
|
||||
ptyStream.signal("INT");
|
||||
signalSent = true;
|
||||
}
|
||||
} catch {
|
||||
// Ignore signal failures and fall back to ETX.
|
||||
}
|
||||
try {
|
||||
if (typeof ptyStream.write === "function") {
|
||||
ptyStream.write("\x03");
|
||||
@@ -275,7 +272,7 @@ function startPtyJob(ptyStream, command, options) {
|
||||
}
|
||||
jobDiag.emit("send-interrupt", {
|
||||
trigger,
|
||||
signalSent,
|
||||
signalSent: false,
|
||||
etxWritten,
|
||||
session: diagSnapshot(),
|
||||
});
|
||||
|
||||
@@ -153,6 +153,37 @@ test("foreground PTY cancellation returns only a bounded tail", async () => {
|
||||
assert.equal(result.outputTruncated, true);
|
||||
});
|
||||
|
||||
test("PTY cancellation delivers Ctrl+C as terminal data without a channel signal request", async () => {
|
||||
const calls = [];
|
||||
class SignalSpyPty extends EventEmitter {
|
||||
signal(name) {
|
||||
calls.push(["signal", name]);
|
||||
}
|
||||
write(data) {
|
||||
if (data === "\x03") calls.push(["write", data]);
|
||||
const marker = markerFromWrite(data);
|
||||
if (!marker || this.started) return;
|
||||
this.started = true;
|
||||
queueMicrotask(() => {
|
||||
this.emit("data", Buffer.from(`${marker}_S\n`));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const pty = new SignalSpyPty();
|
||||
const job = startPtyJob(pty, "cancel-signal-request", {
|
||||
shellKind: "posix",
|
||||
timeoutMs: 1_000,
|
||||
expectedPrompt: "$ ",
|
||||
});
|
||||
await new Promise((resolve) => setImmediate(resolve));
|
||||
job.cancel();
|
||||
pty.emit("data", Buffer.from("$ "));
|
||||
const result = await job.resultPromise;
|
||||
assert.equal(result.error, "Cancelled");
|
||||
assert.deepEqual(calls, [["write", "\x03"]]);
|
||||
});
|
||||
|
||||
test("background PTY jobs preserve output that has no trailing newline", async () => {
|
||||
const job = startPtyJob(new ShellBackedPty(), "printf 'abc'", {
|
||||
shellKind: "posix",
|
||||
|
||||
Reference in new issue
Block a user