fix(process): release handles and close worker on incomplete cleanup (#2321)
* fix(process): unreference handles on incomplete provider process stop Signed-off-by: Vinícius DS Andrade <95101505+viniciusdsandrade@users.noreply.github.com> * fix(javascript): close worker and release handles on incomplete cleanup Signed-off-by: Vinícius DS Andrade <95101505+viniciusdsandrade@users.noreply.github.com> * fix(javascript): defer worker stdin close until incomplete cleanup Signed-off-by: Vinícius DS Andrade <95101505+viniciusdsandrade@users.noreply.github.com> * test(javascript): assert cleanup error instance on inspection uncertainty Signed-off-by: Vinícius DS Andrade <95101505+viniciusdsandrade@users.noreply.github.com> --------- Signed-off-by: Vinícius DS Andrade <95101505+viniciusdsandrade@users.noreply.github.com> Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
co-authored by
morluto
parent
6e25cc3c76
commit
1e38a038c6
@@ -83,6 +83,7 @@ export class JavaScriptAnalysisWorker {
|
||||
readonly #resultBudgetBytes: number;
|
||||
/** Null when the runtime enforces the V8 heap flags instead. */
|
||||
readonly #processMemoryLimitBytes: number | null;
|
||||
readonly #onStdinError = (): void => {};
|
||||
readonly #onStdout = (chunk: Buffer | string): void => {
|
||||
try {
|
||||
this.#pending +=
|
||||
@@ -784,20 +785,40 @@ export class JavaScriptAnalysisWorker {
|
||||
if (this.#supervisor !== undefined) {
|
||||
const supervisor = this.#supervisor;
|
||||
const stopped = await supervisor.stop();
|
||||
supervisor.launch.process.stdout?.off("data", this.#onStdout);
|
||||
if (stopped.status === "incomplete") {
|
||||
try {
|
||||
const stdin = supervisor.launch.process.stdin;
|
||||
if (
|
||||
stdin !== undefined &&
|
||||
stdin !== null &&
|
||||
!stdin.destroyed &&
|
||||
!stdin.writableEnded
|
||||
) {
|
||||
stdin.off("error", this.#onStdinError);
|
||||
stdin.once("error", this.#onStdinError);
|
||||
stdin.end();
|
||||
}
|
||||
} catch {
|
||||
// Cooperative protocol stdin may already be closed or broken.
|
||||
}
|
||||
this.#resetState();
|
||||
pendingCleanup.add(this);
|
||||
return this.#cleanupError(stopped.reason, original);
|
||||
}
|
||||
supervisor.launch.process.stdout?.off("data", this.#onStdout);
|
||||
supervisor.dispose();
|
||||
this.#supervisor = undefined;
|
||||
}
|
||||
this.#resetState();
|
||||
return undefined;
|
||||
}
|
||||
|
||||
#resetState(): void {
|
||||
this.#ready = false;
|
||||
this.#frames = [];
|
||||
this.#pending = "";
|
||||
this.#decoder = new StringDecoder("utf8");
|
||||
this.#protocolFailure = undefined;
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -61,6 +61,8 @@ export interface ProviderProcessHandle extends Pick<
|
||||
/** True once an enforced process memory limit reported an allocation failure. */
|
||||
readonly memoryLimitReached?: boolean;
|
||||
kill(signal?: NodeJS.Signals | number): boolean;
|
||||
unref?(): void;
|
||||
ref?(): void;
|
||||
}
|
||||
|
||||
/** Process handle returned by a provider-specific launcher. */
|
||||
@@ -371,13 +373,33 @@ export class ProviderProcessSupervisor {
|
||||
this.launch.process.off("error", this.#onError);
|
||||
}
|
||||
|
||||
/** Release event-loop references so incomplete cleanup does not prevent host exit. */
|
||||
unref(): void {
|
||||
const process = this.launch.process;
|
||||
process.unref?.();
|
||||
(process.stdin as { unref?: () => void } | null | undefined)?.unref?.();
|
||||
(process.stdout as { unref?: () => void } | null | undefined)?.unref?.();
|
||||
(process.stderr as { unref?: () => void } | null | undefined)?.unref?.();
|
||||
}
|
||||
|
||||
/** Restore event-loop references when retrying supervised process cleanup. */
|
||||
ref(): void {
|
||||
const process = this.launch.process;
|
||||
process.ref?.();
|
||||
(process.stdin as { ref?: () => void } | null | undefined)?.ref?.();
|
||||
(process.stdout as { ref?: () => void } | null | undefined)?.ref?.();
|
||||
(process.stderr as { ref?: () => void } | null | undefined)?.ref?.();
|
||||
}
|
||||
|
||||
async #stop(
|
||||
options: ProviderProcessStopOptions,
|
||||
): Promise<ProviderProcessStopResult> {
|
||||
this.ref();
|
||||
let completed = false;
|
||||
try {
|
||||
const result = await this.#stopWithin(options);
|
||||
completed = result.status !== "incomplete";
|
||||
if (!completed) this.unref();
|
||||
return result;
|
||||
} finally {
|
||||
if (completed) this.dispose();
|
||||
|
||||
@@ -121,3 +121,40 @@ it("reports a reached process memory limit only when the handle observed one", (
|
||||
supervisor.dispose();
|
||||
}
|
||||
});
|
||||
|
||||
it("unreferences process and stream handles when stop reports incomplete cleanup", async () => {
|
||||
const unrefProcess = vi.fn();
|
||||
const unrefStdin = vi.fn();
|
||||
const unrefStdout = vi.fn();
|
||||
const unrefStderr = vi.fn();
|
||||
const producer = Object.assign(new EventEmitter(), {
|
||||
stdout: Object.assign(new PassThrough(), { unref: unrefStdout }),
|
||||
stdin: Object.assign(new PassThrough(), { unref: unrefStdin }),
|
||||
stderr: Object.assign(new PassThrough(), { unref: unrefStderr }),
|
||||
exitCode: null,
|
||||
signalCode: null,
|
||||
kill: () => false,
|
||||
unref: unrefProcess,
|
||||
});
|
||||
const supervisor = new ProviderProcessSupervisor({
|
||||
process: producer,
|
||||
ownsProcessLifetime: true,
|
||||
cleanup: async () => ({
|
||||
cleaned: false,
|
||||
reason: "process table could not be inspected: spawn EPERM",
|
||||
}),
|
||||
});
|
||||
try {
|
||||
const result = await supervisor.stop();
|
||||
expect(result).toMatchObject({
|
||||
status: "incomplete",
|
||||
reason: "process table could not be inspected: spawn EPERM",
|
||||
});
|
||||
expect(unrefProcess).toHaveBeenCalledTimes(1);
|
||||
expect(unrefStdin).toHaveBeenCalledTimes(1);
|
||||
expect(unrefStdout).toHaveBeenCalledTimes(1);
|
||||
expect(unrefStderr).toHaveBeenCalledTimes(1);
|
||||
} finally {
|
||||
supervisor.dispose();
|
||||
}
|
||||
});
|
||||
|
||||
@@ -67,6 +67,7 @@ export class WindowsOwnedProcess extends EventEmitter {
|
||||
#closed = false;
|
||||
#failure: string | undefined;
|
||||
#timer: NodeJS.Timeout | undefined;
|
||||
#unreferenced = false;
|
||||
#cleanup: Promise<ProcessCleanupResult> | undefined;
|
||||
readonly #memoryLimitBytes: number | undefined;
|
||||
#memoryLimitReached = false;
|
||||
@@ -222,6 +223,18 @@ export class WindowsOwnedProcess extends EventEmitter {
|
||||
return this.#cleanup;
|
||||
}
|
||||
|
||||
/** Release event-loop references so unsettled jobs do not prevent host exit. */
|
||||
unref(): void {
|
||||
this.#unreferenced = true;
|
||||
this.#timer?.unref();
|
||||
}
|
||||
|
||||
/** Restore event-loop references when retrying process lifecycle observation. */
|
||||
ref(): void {
|
||||
this.#unreferenced = false;
|
||||
this.#timer?.ref();
|
||||
}
|
||||
|
||||
async #stop(): Promise<ProcessCleanupResult> {
|
||||
if (this.#closed) return { cleaned: true, signaled: false };
|
||||
let timer: NodeJS.Timeout | undefined;
|
||||
@@ -255,6 +268,7 @@ export class WindowsOwnedProcess extends EventEmitter {
|
||||
#schedule(): void {
|
||||
if (this.#closed || this.#timer !== undefined) return;
|
||||
this.#timer = setTimeout(() => this.#poll(), 25);
|
||||
if (this.#unreferenced) this.#timer.unref();
|
||||
}
|
||||
|
||||
#poll(): void {
|
||||
|
||||
Reference in New Issue
Block a user