Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/platforms/ios/runner-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
type ExecBackgroundResult,
} from '../../utils/exec.ts';
import { withKeyedLock } from '../../utils/keyed-lock.ts';
import { isProcessAlive } from '../../utils/process-identity.ts';
import { isProcessAlive, isProcessGroupAlive } from '../../utils/process-identity.ts';
import type { DeviceInfo } from '../../utils/device.ts';
import { buildSimctlArgsForDevice } from './simctl.ts';
import {
Expand Down Expand Up @@ -192,7 +192,9 @@ async function stopRunnerSessionInternal(
new Promise<void>((resolve) => setTimeout(resolve, RUNNER_STOP_WAIT_TIMEOUT_MS)),
]);
} catch {}
await killRunnerProcessTree(session.child.pid, 'SIGKILL');
if (isRunnerProcessTreeAlive(session.child.pid)) {
await killRunnerProcessTree(session.child.pid, 'SIGKILL');
}
cleanupTempFile(session.xctestrunPath);
cleanupTempFile(session.jsonPath);
await session.simulatorSetRedirect?.release();
Expand Down Expand Up @@ -274,6 +276,11 @@ function isRunnerProcessAlive(pid: number | undefined): boolean {
return isProcessAlive(pid);
}

function isRunnerProcessTreeAlive(pid: number | undefined): boolean {
if (!pid) return false;
return isRunnerProcessAlive(pid) || isProcessGroupAlive(pid);
}

async function killRunnerProcessTree(
pid: number | undefined,
signal: 'SIGINT' | 'SIGTERM' | 'SIGKILL',
Expand Down
5 changes: 5 additions & 0 deletions src/utils/__tests__/process-identity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import assert from 'node:assert/strict';
import {
isAgentDeviceDaemonCommand,
isProcessAlive,
isProcessGroupAlive,
readProcessStartTime,
readProcessCommand,
} from '../process-identity.ts';
Expand All @@ -11,6 +12,10 @@ test('isProcessAlive returns false for invalid pid', () => {
assert.equal(isProcessAlive(-1), false);
});

test('isProcessGroupAlive returns false for invalid pid', () => {
assert.equal(isProcessGroupAlive(-1), false);
});

test('readProcessStartTime returns value for current process', () => {
const startTime = readProcessStartTime(process.pid);
if (startTime === null) {
Expand Down
10 changes: 10 additions & 0 deletions src/utils/process-identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ export function isProcessAlive(pid: number): boolean {
}
}

export function isProcessGroupAlive(pid: number): boolean {
if (!Number.isInteger(pid) || pid <= 0) return false;
try {
process.kill(-pid, 0);
return true;
} catch (err) {
return (err as NodeJS.ErrnoException).code === 'EPERM';
}
}

export function readProcessStartTime(pid: number): string | null {
if (!Number.isInteger(pid) || pid <= 0) return null;
try {
Expand Down
Loading