Files
Netcatty/electron/terminalWorker/workerProcessGuards.cjs
T
3408accba7 fix(main): stop the process error guards from logging into a dead stdio sink (#4140)
* fix(main): stop the process error guards from logging into a dead stdio sink

#4128 reports the main process freezing with an `EIO` write error raised from
`logError` -> `console.error`. That is the same feedback loop as #4136: the
guard writes diagnostics through the sink that just failed, the failure comes
back as another process-level error, and the handler logs again.

The `ignore` short-circuit that fixed the terminal worker does not cover this
one, because `EIO` is not a benign stream teardown: it is classified as
`suppress`, which logs.

- Add `createDiagnosticLogger()`. It holds an `error` listener on the
  diagnostics streams so a failed stdio write is no longer re-raised as an
  uncaught exception, and it latches closed after an async sink error or a
  synchronous throw, so later diagnostics are dropped instead of re-entering.
- Use it for the main-process logger and warner in `main.cjs`.

Errors are still captured for the crash log through `captureError`.

* fix(error-guards): forward the first stdio failure to the crash log

createDiagnosticLogger holds an `error` listener on stdout/stderr, so it
consumes the stream failure before the process guards can see it. Once the
sink latched closed, the failure was dropped from the crash log entirely:
a closed stdout pipe produced one report before the sink guard landed and
none after.

- Add an `onError` option to `createDiagnosticLogger`. It latches the sink
  closed first, then forwards only the first stream error; a throwing
  reporter cannot escape.
- Main process: report the failure through
  `crashLogBridge.captureError("stdio", err)`.
- Terminal worker: fold the inline `consoleBroken` latch onto
  `createDiagnosticLogger` and forward the first failure through
  `report`/`reportWorkerError`.

Regression coverage spawns a subprocess whose stdout/stderr pipe is closed
by the parent and asserts exactly one report is produced.

* ci: retrigger tests after unrelated plugin SIGKILL flake

PluginLogSettings.test.tsx was SIGKILL'd and the lint-and-test job was
canceled; the failure is unrelated to the stdio guard change.

---------

Co-authored-by: shibo.li <shibo.li@d-robotics.cc>
Co-authored-by: 陈大猫 <16399091+binaricat@users.noreply.github.com>
2026-11-05 21:34:37 +08:00

137 lines
5.4 KiB
JavaScript

"use strict";
const {
classifyProcessError,
createDiagnosticLogger,
} = require("../bridges/processErrorGuards.cjs");
/**
* Terminal worker process error guards.
*
* Every terminal, SSH, SFTP, and port-forwarding session shares this
* utilityProcess, so a single stray async error must never let Node's default
* `uncaughtException` behavior exit the worker with code 1 — that would
* disconnect every session at once. This mirrors the main-process guards
* (`bridges/processErrorGuards.cjs`), but worker policy is stricter: once the
* worker is running, every process-level error is suppressed. The error is
* still reported (see `report`) so the main process can record it in the
* crash log for later diagnosis.
*
* Startup errors are NOT suppressed: until `options.isRuntimeStarted()`
* returns true, every error is fatal and re-thrown so the worker exits.
* An IPC-retained utilityProcess that swallowed a startup failure
* would otherwise stay alive without a message listener, leaving every
* manager request pending forever instead of rejecting/replacing it.
*
* Benign stream teardown (EPIPE / ERR_STREAM_DESTROYED) is classified as
* `ignore` and must NOT call `logError`/`console.error`: writing diagnostics
* through an already-broken console pipe re-enters this handler and busy-
* loops (see #4136). A shared diagnostic logger also latches the default
* console.error path closed after stdout/stderr emit `error`, forwarding the
* first failure to `report` so it still reaches the crash log.
*/
function installTerminalWorkerErrorGuards(options = {}) {
const processObject = options.processObject || process;
if (!processObject?.on || !processObject?.removeListener) {
throw new Error("A process-like EventEmitter is required");
}
const report = typeof options.report === "function" ? options.report : () => {};
// Holding an `error` listener keeps a failed write on stdout/stderr from
// being rethrown as an uncaught exception, which is how the loop used to
// restart. The first failure is forwarded to `report` because this listener
// consumes the event before the process guards see it.
const consoleLogger = createDiagnosticLogger(
(...args) => console.error(...args),
{
streams: [processObject.stdout, processObject.stderr],
onError(err) {
try {
report("stdio", err, {
action: "ignore",
reason: "diagnostics sink failure",
});
} catch {
// Error reporting must never be able to escalate into a worker crash.
}
},
},
);
const logError = typeof options.logError === "function"
? options.logError
: consoleLogger;
// Default to runtime semantics only when a caller provides no startup
// signal; process.cjs always passes one.
const isRuntimeStarted = typeof options.isRuntimeStarted === "function"
? options.isRuntimeStarted
: () => true;
const labelFor = (origin) => (
origin === "unhandledRejection" ? "unhandled rejection" : "uncaught exception"
);
const makeHandler = (origin) => (err) => {
// An error already marked fatal (e.g. a startup unhandled rejection that
// threw into the uncaughtException path) must exit, not be re-classified.
if (err?.__terminalWorkerFatalStartupError) {
throw err;
}
// The shared classifier ignores some stream/network errors regardless of
// startup state. Those are recoverable only after this worker can receive
// requests; swallowing them during initialization leaves a live dead end.
const decision = isRuntimeStarted()
? classifyProcessError(err, { runtimeStarted: true, origin })
: { action: "fatal", reason: "startup error before worker became usable" };
if (decision.action === "fatal") {
logError(
`Terminal worker ${labelFor(origin)} (${decision.reason}); exiting:`,
err,
);
try {
report(origin, err, decision);
} catch {
// Error reporting must never be able to escalate into a worker crash.
}
// Re-throw so Node's default behavior terminates the worker; the
// manager observes the exit and can reject or replace the worker.
const fatal = err instanceof Error ? err : new Error(String(err));
fatal.__terminalWorkerFatalStartupError = true;
throw fatal;
}
// Never log through a stream that just failed: ignore-classified EPIPE
// would otherwise call console.error → EPIPE → uncaughtException → loop.
if (decision.action === "ignore") {
try {
report(origin, err, decision);
} catch {
// Error reporting must never be able to escalate into a worker crash.
}
return;
}
logError(
`Suppressed terminal worker ${labelFor(origin)} (${decision.reason}):`,
err,
);
try {
report(origin, err, decision);
} catch {
// Error reporting must never be able to escalate into a worker crash.
}
};
const handleUncaughtException = makeHandler("uncaughtException");
const handleUnhandledRejection = makeHandler("unhandledRejection");
processObject.on("uncaughtException", handleUncaughtException);
processObject.on("unhandledRejection", handleUnhandledRejection);
return () => {
processObject.removeListener("uncaughtException", handleUncaughtException);
processObject.removeListener("unhandledRejection", handleUnhandledRejection);
consoleLogger.dispose();
};
}
module.exports = {
installTerminalWorkerErrorGuards,
};