mirror of
https://github.com/expo/eas-cli.git
synced 2026-11-23 16:00:26 -10:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ff91dd3393 | ||
|
|
2bee448a00 |
No files matched your search
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'eas-cli': patch
|
||||
---
|
||||
|
||||
Stop the simulator session when `eas simulator:start` receives SIGTERM, as it already does for Ctrl+C. Package runners such as `pnpm dlx` can send SIGTERM, which left the session running on EAS.
|
||||
@@ -1340,6 +1340,33 @@ describe(Simulator, () => {
|
||||
}
|
||||
});
|
||||
|
||||
it('cancels the create request and exits with 143 on SIGTERM', async () => {
|
||||
useFakeTimersWithRealSleep();
|
||||
const processExitSpy = jest.spyOn(process, 'exit').mockImplementation(code => {
|
||||
throw new Error(`process.exit(${code})`);
|
||||
});
|
||||
try {
|
||||
const createStarted = mockCreateThatNeverAnswers();
|
||||
const existingSigtermListeners = [...process.listeners('SIGTERM')];
|
||||
|
||||
const { command } = createCommand(['--platform', 'ios', '--non-interactive']);
|
||||
const commandPromise = command.runAsync();
|
||||
const exited = expect(commandPromise).rejects.toThrow('process.exit(143)');
|
||||
await createStarted;
|
||||
process.emit('SIGTERM');
|
||||
await jest.advanceTimersByTimeAsync(5_000);
|
||||
await exited;
|
||||
|
||||
expect(Log.warn).toHaveBeenCalledWith(
|
||||
expect.stringContaining('Run `eas simulator:list` to check for a running session')
|
||||
);
|
||||
expect(process.listeners('SIGTERM')).toEqual(existingSigtermListeners);
|
||||
} finally {
|
||||
processExitSpy.mockRestore();
|
||||
jest.useRealTimers();
|
||||
}
|
||||
});
|
||||
|
||||
it('warns the same way, and logs no "request failed", when the create request fails after Ctrl+C', async () => {
|
||||
mockLogEvent.mockClear();
|
||||
useFakeTimersWithRealSleep();
|
||||
@@ -2503,6 +2530,40 @@ describe(Simulator, () => {
|
||||
processExitSpy.mockRestore();
|
||||
});
|
||||
|
||||
it('stops the simulator session and exits with 143 on SIGTERM before the session is ready', async () => {
|
||||
const processExitSpy = jest.spyOn(process, 'exit').mockImplementation(code => {
|
||||
throw new Error(`process.exit(${code})`);
|
||||
});
|
||||
let notifyQueryStarted: () => void = () => {};
|
||||
const queryStarted = new Promise<void>(resolve => {
|
||||
notifyQueryStarted = resolve;
|
||||
});
|
||||
mockByIdAsync.mockImplementationOnce(
|
||||
() =>
|
||||
new Promise(() => {
|
||||
notifyQueryStarted();
|
||||
})
|
||||
);
|
||||
const existingSigintListeners = [...process.listeners('SIGINT')];
|
||||
const existingSigtermListeners = [...process.listeners('SIGTERM')];
|
||||
|
||||
const { command } = createCommand(['--platform', 'ios', '--non-interactive']);
|
||||
const commandPromise = command.runAsync();
|
||||
await queryStarted;
|
||||
|
||||
process.emit('SIGTERM');
|
||||
await expect(commandPromise).rejects.toThrow('process.exit(143)');
|
||||
|
||||
expect(mockEnsureDeviceRunSessionStoppedAsync).toHaveBeenCalledWith(
|
||||
graphqlClient,
|
||||
'session-123'
|
||||
);
|
||||
expect(mockResetSimulatorEnvAsync).toHaveBeenCalledWith(projectDir, 'session-123');
|
||||
expect(process.listeners('SIGINT')).toEqual(existingSigintListeners);
|
||||
expect(process.listeners('SIGTERM')).toEqual(existingSigtermListeners);
|
||||
processExitSpy.mockRestore();
|
||||
});
|
||||
|
||||
const localEgressSessions: [
|
||||
string,
|
||||
DeviceRunSessionType,
|
||||
@@ -3511,6 +3572,50 @@ describe(Simulator, () => {
|
||||
expect(mockResetSimulatorEnvAsync).toHaveBeenCalledWith(projectDir, 'session-123');
|
||||
});
|
||||
|
||||
it('stops the session on SIGTERM while it is running, without waiting for a stalled poll', async () => {
|
||||
const existingSigtermListeners = [...process.listeners('SIGTERM')];
|
||||
let notifyPollStarted: () => void = () => {};
|
||||
const pollStarted = new Promise<void>(resolve => {
|
||||
notifyPollStarted = resolve;
|
||||
});
|
||||
let releasePoll: (session: ReturnType<typeof makeDeviceRunSession>) => void = () => {};
|
||||
mockByIdAsync.mockResolvedValueOnce(makeDeviceRunSession()).mockImplementationOnce(
|
||||
() =>
|
||||
new Promise(resolve => {
|
||||
releasePoll = resolve;
|
||||
notifyPollStarted();
|
||||
})
|
||||
);
|
||||
|
||||
const { command } = createCommand(['--platform', 'ios']);
|
||||
const commandPromise = command.runAsync();
|
||||
await pollStarted;
|
||||
process.emit('SIGTERM');
|
||||
await commandPromise;
|
||||
|
||||
expect(mockEnsureDeviceRunSessionStoppedAsync).toHaveBeenCalledWith(
|
||||
graphqlClient,
|
||||
'session-123'
|
||||
);
|
||||
expect(mockResetSimulatorEnvAsync).toHaveBeenCalledWith(projectDir, 'session-123');
|
||||
expect(process.listeners('SIGTERM')).toEqual(existingSigtermListeners);
|
||||
releasePoll(makeDeviceRunSession());
|
||||
});
|
||||
|
||||
it('removes its signal listeners when printing the session instructions fails', async () => {
|
||||
const existingSigintListeners = [...process.listeners('SIGINT')];
|
||||
const existingSigtermListeners = [...process.listeners('SIGTERM')];
|
||||
jest.mocked(Log.log).mockImplementationOnce(() => {
|
||||
throw new Error('print failed');
|
||||
});
|
||||
|
||||
const { command } = createCommand(['--platform', 'ios']);
|
||||
await expect(command.runAsync()).rejects.toThrow('print failed');
|
||||
|
||||
expect(process.listeners('SIGINT')).toEqual(existingSigintListeners);
|
||||
expect(process.listeners('SIGTERM')).toEqual(existingSigtermListeners);
|
||||
});
|
||||
|
||||
it('stops the session and reports when the stop cannot be confirmed after an interrupt before ready', async () => {
|
||||
jest.spyOn(process, 'exit').mockImplementation(code => {
|
||||
throw new Error(`process.exit(${code})`);
|
||||
|
||||
@@ -875,6 +875,9 @@ With local egress, actions wait for the connection and require interactive mode.
|
||||
}
|
||||
} finally {
|
||||
sessionInterrupt.signal.removeEventListener('abort', onSessionAbort);
|
||||
// Paths that already disposed it are unaffected. An unexpected error, for example while
|
||||
// printing the session instructions, must not leave the signal listeners installed.
|
||||
sessionInterrupt.dispose();
|
||||
egressAbortController.abort();
|
||||
await egressPromise;
|
||||
}
|
||||
@@ -959,7 +962,12 @@ async function waitForSessionEndOrInterruptAsync({
|
||||
while (!signal.aborted) {
|
||||
let session;
|
||||
try {
|
||||
session = await DeviceRunSessionQuery.byIdAsync(graphqlClient, deviceRunSessionId);
|
||||
// Race the abort so a stalled poll cannot delay the stop after Ctrl+C or SIGTERM. A runner
|
||||
// can follow SIGTERM with SIGKILL soon after.
|
||||
session = await Promise.race([
|
||||
DeviceRunSessionQuery.byIdAsync(graphqlClient, deviceRunSessionId),
|
||||
sessionInterrupt.abortPromise,
|
||||
]);
|
||||
} catch (err) {
|
||||
Log.debug(
|
||||
`Failed to poll simulator session: ${err instanceof Error ? err.message : String(err)}`
|
||||
@@ -967,6 +975,9 @@ async function waitForSessionEndOrInterruptAsync({
|
||||
await sleepAsync(POLL_INTERVAL_MS, signal);
|
||||
continue;
|
||||
}
|
||||
if (!session) {
|
||||
break;
|
||||
}
|
||||
|
||||
const jobRunStatus = session.turtleJobRun?.status;
|
||||
if (
|
||||
@@ -1009,11 +1020,43 @@ async function waitForSessionEndOrInterruptAsync({
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Signals that stop the session. Ctrl+C sends SIGINT. Package runners such as `pnpm dlx` can
|
||||
* forward SIGTERM to the CLI instead, and without a handler Node exits at once and leaves the
|
||||
* session running on EAS.
|
||||
*/
|
||||
const INTERRUPT_SIGNALS = ['SIGINT', 'SIGTERM'] as const;
|
||||
type InterruptSignal = (typeof INTERRUPT_SIGNALS)[number];
|
||||
|
||||
/** Shell convention: 128 + the signal number. */
|
||||
const EXIT_CODE_BY_INTERRUPT_SIGNAL: Record<InterruptSignal, number> = {
|
||||
SIGINT: 130,
|
||||
SIGTERM: 143,
|
||||
};
|
||||
|
||||
/** Listens for every interrupt signal and returns a function that removes the listeners. */
|
||||
function addInterruptListener(listener: (signal: InterruptSignal) => void): () => void {
|
||||
const listeners = INTERRUPT_SIGNALS.map(signal => {
|
||||
const onSignal = (): void => {
|
||||
listener(signal);
|
||||
};
|
||||
process.on(signal, onSignal);
|
||||
return { signal, onSignal };
|
||||
});
|
||||
return () => {
|
||||
for (const { signal, onSignal } of listeners) {
|
||||
process.removeListener(signal, onSignal);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
type SessionInterrupt = {
|
||||
signal: AbortSignal;
|
||||
abortPromise: Promise<void>;
|
||||
/** Stop the session as if the user had pressed Ctrl+C. */
|
||||
abort: () => void;
|
||||
/** The exit code for the signal that interrupted the session. */
|
||||
readonly exitCode: number;
|
||||
dispose: () => void;
|
||||
};
|
||||
|
||||
@@ -1029,18 +1072,20 @@ function registerSessionInterrupt(deviceRunSessionId: string): SessionInterrupt
|
||||
{ once: true }
|
||||
);
|
||||
});
|
||||
const sigintHandler = (): void => {
|
||||
let exitCode = EXIT_CODE_BY_INTERRUPT_SIGNAL.SIGINT;
|
||||
const interruptHandler = (interruptSignal: InterruptSignal): void => {
|
||||
if (signal.aborted) {
|
||||
// Force exit on a second Ctrl+C in case cleanup is hanging. The session may still be
|
||||
// Force exit on a second signal in case cleanup is hanging. The session may still be
|
||||
// running on EAS, so tell the user how to make sure it gets terminated.
|
||||
Log.error(
|
||||
`Aborted before the simulator session could be stopped. Run \`eas simulator:stop --id ${deviceRunSessionId}\` to terminate it and avoid unexpected charges.`
|
||||
);
|
||||
process.exit(130);
|
||||
process.exit(EXIT_CODE_BY_INTERRUPT_SIGNAL[interruptSignal]);
|
||||
}
|
||||
exitCode = EXIT_CODE_BY_INTERRUPT_SIGNAL[interruptSignal];
|
||||
abortController.abort();
|
||||
};
|
||||
process.on('SIGINT', sigintHandler);
|
||||
const removeInterruptListener = addInterruptListener(interruptHandler);
|
||||
|
||||
return {
|
||||
signal,
|
||||
@@ -1048,7 +1093,10 @@ function registerSessionInterrupt(deviceRunSessionId: string): SessionInterrupt
|
||||
abort: () => {
|
||||
abortController.abort();
|
||||
},
|
||||
dispose: () => process.removeListener('SIGINT', sigintHandler),
|
||||
get exitCode() {
|
||||
return exitCode;
|
||||
},
|
||||
dispose: removeInterruptListener,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1082,7 +1130,7 @@ async function stopDeviceRunSessionAfterInterruptAsync({
|
||||
} finally {
|
||||
sessionInterrupt.dispose();
|
||||
}
|
||||
process.exit(130);
|
||||
process.exit(sessionInterrupt.exitCode);
|
||||
}
|
||||
|
||||
async function stopDeviceRunSessionCreatedAfterCancelAsync(
|
||||
@@ -1158,9 +1206,9 @@ class SimulatorRequestCancelledError extends Error {}
|
||||
* arrives. "Request cancelled" means the client stopped waiting; the server can still create the
|
||||
* session. So Ctrl+C stops the spinner, flushes analytics, and waits a bounded time for the
|
||||
* request: a session that comes back goes to `stopCreatedAsync`, and otherwise the user is told how
|
||||
* to check for one. Then it exits with 130, like the session's own Ctrl+C handler: going through
|
||||
* the command's error handling would print the exit as an error and report it to Sentry. A second
|
||||
* Ctrl+C exits at once.
|
||||
* to check for one. Then it exits with 130 (143 for SIGTERM), like the session's own Ctrl+C
|
||||
* handler: going through the command's error handling would print the exit as an error and report it
|
||||
* to Sentry. A second Ctrl+C or SIGTERM exits at once.
|
||||
*/
|
||||
async function withSimulatorRequestAnalyticsAsync<T>(
|
||||
analytics: AnalyticsWithOrchestration,
|
||||
@@ -1170,18 +1218,20 @@ async function withSimulatorRequestAnalyticsAsync<T>(
|
||||
stopCreatedAsync: (created: T) => Promise<void>
|
||||
): Promise<T> {
|
||||
let cancelRequested = false;
|
||||
let onSigint: (() => void) | undefined;
|
||||
let exitCode = EXIT_CODE_BY_INTERRUPT_SIGNAL.SIGINT;
|
||||
let onInterrupt: ((signal: InterruptSignal) => void) | undefined;
|
||||
const cancelled = new Promise<never>((_, reject) => {
|
||||
onSigint = () => {
|
||||
onInterrupt = signal => {
|
||||
if (cancelRequested) {
|
||||
Log.warn(SESSION_MAY_BE_RUNNING_WARNING);
|
||||
process.exit(130);
|
||||
process.exit(EXIT_CODE_BY_INTERRUPT_SIGNAL[signal]);
|
||||
}
|
||||
cancelRequested = true;
|
||||
exitCode = EXIT_CODE_BY_INTERRUPT_SIGNAL[signal];
|
||||
reject(new SimulatorRequestCancelledError());
|
||||
};
|
||||
});
|
||||
process.on('SIGINT', onSigint!);
|
||||
const removeInterruptListener = addInterruptListener(onInterrupt!);
|
||||
analytics.logEvent(SimulatorEvent.REQUEST_SENT, properties);
|
||||
let createPromise: Promise<T> | undefined;
|
||||
try {
|
||||
@@ -1198,7 +1248,7 @@ async function withSimulatorRequestAnalyticsAsync<T>(
|
||||
]);
|
||||
flushTimeout.abort();
|
||||
await stopSessionCreatedAfterCancelAsync(createPromise, stopCreatedAsync);
|
||||
process.exit(130);
|
||||
process.exit(exitCode);
|
||||
}
|
||||
const reason = simulatorRequestFailureReason(error);
|
||||
if (reason) {
|
||||
@@ -1206,7 +1256,7 @@ async function withSimulatorRequestAnalyticsAsync<T>(
|
||||
}
|
||||
throw error;
|
||||
} finally {
|
||||
process.removeListener('SIGINT', onSigint!);
|
||||
removeInterruptListener();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -63,6 +63,22 @@ describe(createAppArchiveAsync, () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('leaves SIGINT and SIGTERM to a caller that handles interrupts', async () => {
|
||||
const sigintListeners = process.listeners('SIGINT');
|
||||
const sigtermListeners = process.listeners('SIGTERM');
|
||||
const exitListenerCount = process.listenerCount('exit');
|
||||
const archive = await createAppArchiveAsync(bundle, { handleInterrupt: false });
|
||||
try {
|
||||
expect(process.listeners('SIGINT')).toEqual(sigintListeners);
|
||||
expect(process.listeners('SIGTERM')).toEqual(sigtermListeners);
|
||||
// The exit handler still removes the archive when the caller exits.
|
||||
expect(process.listenerCount('exit')).toBe(exitListenerCount + 1);
|
||||
} finally {
|
||||
await archive.cleanupAsync();
|
||||
}
|
||||
expect(process.listenerCount('exit')).toBe(exitListenerCount);
|
||||
});
|
||||
|
||||
it('removes its signal and exit handlers after normal cleanup', async () => {
|
||||
const before = [
|
||||
process.listeners('SIGINT'),
|
||||
|
||||
@@ -28,10 +28,12 @@ export async function createAppArchiveAsync(
|
||||
process.exit(143);
|
||||
};
|
||||
process.once('exit', cleanupOnExit);
|
||||
// A caller that handles interrupts itself (simulator:start stops its session first) needs both
|
||||
// signals: exiting here would skip that cleanup. The exit handler still removes the archive.
|
||||
if (handleInterrupt) {
|
||||
process.once('SIGINT', interrupt);
|
||||
process.once('SIGTERM', terminate);
|
||||
}
|
||||
process.once('SIGTERM', terminate);
|
||||
const cleanupAsync = async (): Promise<void> => {
|
||||
try {
|
||||
await fs.remove(directory);
|
||||
|
||||
@@ -21,7 +21,7 @@ export async function applyStartConfigAsync(
|
||||
const catalog = await fetchHostActionCatalogAsync(connection);
|
||||
const prepared = await prepareControlConfigAsync(catalog, config.path, connection.udid, config);
|
||||
signal.throwIfAborted();
|
||||
// Startup must stop its newly created session before exiting on Ctrl+C.
|
||||
// Startup must stop its newly created session before exiting on Ctrl+C or SIGTERM.
|
||||
return await executeControlConfigAsync(connection, prepared, {
|
||||
onProgress,
|
||||
handleInterrupt: false,
|
||||
|
||||
Reference in new issue
Block a user