[Init] Initial commit - NetMesh terminal manager
Some checks failed
build-packages / resolve bundled mosh-client (push) Has been cancelled
build-packages / resolve bundled et-client (push) Has been cancelled
build-packages / build-macos (push) Has been cancelled
build-packages / build-windows (push) Has been cancelled
build-packages / build-linux-x64 (push) Has been cancelled
build-packages / build-linux-arm64 (push) Has been cancelled
build-packages / release (push) Has been cancelled
build-packages / update Nix release metadata (push) Has been cancelled
build-packages / bump homebrew tap (push) Has been cancelled
test / lint-and-test (push) Has been cancelled
AI automation / Route event (push) Has been cancelled
AI automation / Hand reopened issue to maintainers (push) Has been cancelled
AI automation / Clean source issue state (push) Has been cancelled
AI automation / Reconcile handoffs (push) Has been cancelled
AI automation / Classify issue (push) Has been cancelled
AI automation / Claude Code smoke (push) Has been cancelled
AI automation / Review issue follow-up (push) Has been cancelled
AI automation / Publish issue follow-up (push) Has been cancelled
AI automation / Implement with Claude Code (push) Has been cancelled
AI automation / Publish implement PR (push) Has been cancelled
AI automation / Continue queued issue comments (push) Has been cancelled
AI automation / Codex review loop (push) Has been cancelled
AI automation / Publish Codex fix (push) Has been cancelled
AI automation / Clear Codex dispatch marker (push) Has been cancelled
AI automation / Own PR re-request Codex (push) Has been cancelled
AI automation / External PR re-request Codex (push) Has been cancelled
AI automation / Poll Codex reaction / retry (push) Has been cancelled
build-et-binaries / build-linux-x64 (push) Has been cancelled
build-et-binaries / build-linux-arm64 (push) Has been cancelled
build-et-binaries / build-macos-universal (push) Has been cancelled
build-et-binaries / build-windows-x64 (push) Has been cancelled
build-et-binaries / release (push) Has been cancelled
Some checks failed
build-packages / resolve bundled mosh-client (push) Has been cancelled
build-packages / resolve bundled et-client (push) Has been cancelled
build-packages / build-macos (push) Has been cancelled
build-packages / build-windows (push) Has been cancelled
build-packages / build-linux-x64 (push) Has been cancelled
build-packages / build-linux-arm64 (push) Has been cancelled
build-packages / release (push) Has been cancelled
build-packages / update Nix release metadata (push) Has been cancelled
build-packages / bump homebrew tap (push) Has been cancelled
test / lint-and-test (push) Has been cancelled
AI automation / Route event (push) Has been cancelled
AI automation / Hand reopened issue to maintainers (push) Has been cancelled
AI automation / Clean source issue state (push) Has been cancelled
AI automation / Reconcile handoffs (push) Has been cancelled
AI automation / Classify issue (push) Has been cancelled
AI automation / Claude Code smoke (push) Has been cancelled
AI automation / Review issue follow-up (push) Has been cancelled
AI automation / Publish issue follow-up (push) Has been cancelled
AI automation / Implement with Claude Code (push) Has been cancelled
AI automation / Publish implement PR (push) Has been cancelled
AI automation / Continue queued issue comments (push) Has been cancelled
AI automation / Codex review loop (push) Has been cancelled
AI automation / Publish Codex fix (push) Has been cancelled
AI automation / Clear Codex dispatch marker (push) Has been cancelled
AI automation / Own PR re-request Codex (push) Has been cancelled
AI automation / External PR re-request Codex (push) Has been cancelled
AI automation / Poll Codex reaction / retry (push) Has been cancelled
build-et-binaries / build-linux-x64 (push) Has been cancelled
build-et-binaries / build-linux-arm64 (push) Has been cancelled
build-et-binaries / build-macos-universal (push) Has been cancelled
build-et-binaries / build-windows-x64 (push) Has been cancelled
build-et-binaries / release (push) Has been cancelled
This commit is contained in:
97
electron/terminalWorker/workerProcessGuards.cjs
Normal file
97
electron/terminalWorker/workerProcessGuards.cjs
Normal file
@@ -0,0 +1,97 @@
|
||||
"use strict";
|
||||
|
||||
const {
|
||||
classifyProcessError,
|
||||
} = 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.
|
||||
*/
|
||||
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 : () => {};
|
||||
const logError = typeof options.logError === "function"
|
||||
? options.logError
|
||||
: (...args) => console.error(...args);
|
||||
// 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;
|
||||
}
|
||||
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);
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
installTerminalWorkerErrorGuards,
|
||||
};
|
||||
Reference in New Issue
Block a user