|
| 1 | +import { afterEach, describe, expect, test, vi } from "vitest"; |
| 2 | +import { InteractiveMode } from "../../../src/modes/interactive/interactive-mode.ts"; |
| 3 | + |
| 4 | +// Regression for https://github.com/earendil-works/pi/issues/5724 |
| 5 | +// |
| 6 | +// `proper-lockfile` installs `signal-exit`, whose signal listener re-sends |
| 7 | +// SIGTERM/SIGHUP when it observes no other process listeners during the same |
| 8 | +// signal dispatch. InteractiveMode must therefore keep its signal handlers |
| 9 | +// registered until async terminal cleanup has completed. |
| 10 | + |
| 11 | +type ShutdownThis = { |
| 12 | + isShuttingDown: boolean; |
| 13 | + unregisterSignalHandlers: () => void; |
| 14 | + runtimeHost: { dispose: () => Promise<void> }; |
| 15 | + ui: { terminal: { drainInput: (ms: number) => Promise<void> } }; |
| 16 | + stop: () => void; |
| 17 | +}; |
| 18 | + |
| 19 | +type InteractiveModePrototypeWithShutdown = { |
| 20 | + shutdown(this: ShutdownThis, options?: { fromSignal?: boolean }): Promise<void>; |
| 21 | +}; |
| 22 | + |
| 23 | +const interactiveModePrototype = InteractiveMode.prototype as unknown; |
| 24 | + |
| 25 | +class ProcessExitError extends Error {} |
| 26 | + |
| 27 | +function deferred(): { promise: Promise<void>; resolve: () => void } { |
| 28 | + let resolve: (() => void) | undefined; |
| 29 | + const promise = new Promise<void>((res) => { |
| 30 | + resolve = res; |
| 31 | + }); |
| 32 | + return { |
| 33 | + promise, |
| 34 | + resolve: () => resolve?.(), |
| 35 | + }; |
| 36 | +} |
| 37 | + |
| 38 | +async function callShutdown(context: ShutdownThis, options?: { fromSignal?: boolean }): Promise<void> { |
| 39 | + try { |
| 40 | + await (interactiveModePrototype as InteractiveModePrototypeWithShutdown).shutdown.call(context, options); |
| 41 | + } catch (error) { |
| 42 | + if (!(error instanceof ProcessExitError)) throw error; |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +describe("InteractiveMode SIGTERM shutdown with signal-exit (#5724)", () => { |
| 47 | + afterEach(() => { |
| 48 | + vi.restoreAllMocks(); |
| 49 | + }); |
| 50 | + |
| 51 | + test("keeps signal handlers registered while signal-triggered cleanup is pending", async () => { |
| 52 | + vi.spyOn(process, "exit").mockImplementation((() => { |
| 53 | + throw new ProcessExitError(); |
| 54 | + }) as typeof process.exit); |
| 55 | + |
| 56 | + const order: string[] = []; |
| 57 | + const dispose = deferred(); |
| 58 | + const context: ShutdownThis = { |
| 59 | + isShuttingDown: false, |
| 60 | + unregisterSignalHandlers: vi.fn(() => { |
| 61 | + order.push("unregister"); |
| 62 | + }), |
| 63 | + runtimeHost: { |
| 64 | + dispose: vi.fn(() => { |
| 65 | + order.push("dispose"); |
| 66 | + return dispose.promise; |
| 67 | + }), |
| 68 | + }, |
| 69 | + ui: { |
| 70 | + terminal: { |
| 71 | + drainInput: vi.fn(async () => { |
| 72 | + order.push("drainInput"); |
| 73 | + }), |
| 74 | + }, |
| 75 | + }, |
| 76 | + stop: vi.fn(() => { |
| 77 | + order.push("stop"); |
| 78 | + }), |
| 79 | + }; |
| 80 | + |
| 81 | + const shutdownPromise = callShutdown(context, { fromSignal: true }); |
| 82 | + await Promise.resolve(); |
| 83 | + |
| 84 | + expect(order).toEqual(["dispose"]); |
| 85 | + expect(context.unregisterSignalHandlers).not.toHaveBeenCalled(); |
| 86 | + |
| 87 | + dispose.resolve(); |
| 88 | + await shutdownPromise; |
| 89 | + |
| 90 | + expect(order).toEqual(["dispose", "drainInput", "stop"]); |
| 91 | + }); |
| 92 | +}); |
0 commit comments