Skip to content

Commit c7722ff

Browse files
authored
fix(ios): reap orphaned simulator recorders with a graceful finalize window (callstack#2457)
* fix(ios): hint the busy simctl recorder and detach recorders gracefully Classify `simctl recordVideo` exit 16 (POSIX EBUSY) as a typed COMMAND_FAILED with an actionable hint and the recorder's stderr instead of an unclassified UNKNOWN, so a caller learns another recording holds the one host-wide CoreSimulator slot or a prior recorder died without detaching. Give a recorder that is rolled back or reaped the same graceful SIGINT -> SIGTERM -> SIGKILL escalation the live stop path already uses, and widen the daemon-startup reaper's recorder grace to match it, so finalize-and-detach releases the host-wide recording lock instead of a mid-detach SIGKILL dangling it for every later recording. Closes callstack#2170 * refactor(ios): inline the busy-recorder classifier and drop the rollback helper Simplify callstack#2170. The exit-code classification lives beside `startError` that is its only caller, so drop the separate module and its test and assert through the real start path instead. Test through `normalizeError` to keep the wire-shape (hint lifted, reason and exit code retained) guarantee. Revert the start-rollback SIGKILL sites: the identity arm fires only when the recorder already exited, and the acquisition rollback discards an explicitly canceled recording, so fast-kill there is fine. The dangling-host path is the startup reaper, which keeps the wider finalize grace added earlier. * refactor(ios): scope this change to the daemon-startup recorder reap grace The exit-16 classifier and graceful start rollback are owned by callstack#2447 with the DEVICE_IN_USE contract; drop the overlapping COMMAND_FAILED classifier here to keep one start-path error contract. Keep only the non-overlapping daemon-startup fix: an orphaned simctl recorder is reaped with the same finalize grace the live stop path allows, plus a startup-wiring test. Refs callstack#2170.
1 parent fd4cee8 commit c7722ff

2 files changed

Lines changed: 85 additions & 0 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import fs from 'node:fs';
2+
import { afterEach, expect, test, vi } from 'vitest';
3+
import { mkdtempForTestSync } from '../../__tests__/test-utils/tmp-dir.ts';
4+
5+
const reapCalls = vi.hoisted(() => [] as Array<Record<string, unknown>>);
6+
7+
vi.mock('@agent-device/host-kit/process', async (importOriginal) => {
8+
const actual = await importOriginal<Record<string, unknown>>();
9+
return {
10+
...actual,
11+
reapOwnedProcessRecordsAtStartup: (_store: unknown, options?: Record<string, unknown>) => {
12+
reapCalls.push(options ?? {});
13+
return Promise.resolve({ terminated: [], failed: [] });
14+
},
15+
};
16+
});
17+
18+
vi.mock('../../platform-runtime.ts', () => ({
19+
androidObservation: {},
20+
createRequestPlatformProviders: () => ({
21+
run: async (_context: unknown, task: () => Promise<unknown>) => await task(),
22+
}),
23+
createPlatformRuntimeGateway: () => ({
24+
applicationLifecycle: {
25+
recoverStartupResources: async () => {},
26+
detachForDaemonShutdown: async () => {},
27+
finalizeDaemonShutdown: async () => {},
28+
},
29+
inspectFacts: async () => {
30+
throw new Error('unused');
31+
},
32+
bind: async () => {
33+
throw new Error('unused');
34+
},
35+
shutdown: async () => {},
36+
}),
37+
createPlatformDeviceInventoryGateways: () => ({}),
38+
}));
39+
40+
vi.mock('../../provider-device-runtimes.ts', () => ({
41+
DEFAULT_PROVIDER_RUNTIME_REQUIRED_IDS: [],
42+
createDefaultProviderRuntimeComposition: async () => ({ runtimes: [], platformModules: [] }),
43+
}));
44+
45+
import { startDaemonRuntime } from './daemon-runtime.ts';
46+
47+
afterEach(() => {
48+
reapCalls.length = 0;
49+
});
50+
51+
test('daemon startup reaps orphaned simctl recorders with a graceful finalize window', async () => {
52+
const stateDir = mkdtempForTestSync('agent-device-daemon-recording-reaper-');
53+
try {
54+
const runtime = await startDaemonRuntime({
55+
env: {
56+
...process.env,
57+
AGENT_DEVICE_STATE_DIR: stateDir,
58+
AGENT_DEVICE_DAEMON_IDLE_TIMEOUT_MS: '0',
59+
AGENT_DEVICE_DAEMON_SERVER_MODE: 'http',
60+
},
61+
exit: () => {},
62+
registerProcessHandlers: false,
63+
stderr: { write: () => {} },
64+
stdout: { write: () => {} },
65+
});
66+
expect(runtime).not.toBeNull();
67+
68+
const recordingReap = reapCalls.find(
69+
(call) => JSON.stringify(call.purposes) === JSON.stringify(['simctl-screen-recording']),
70+
);
71+
expect(recordingReap, 'startup should reap the simctl recorder purpose').toBeDefined();
72+
// An orphaned recorder must get the same graceful finalize window the live stop path allows,
73+
// so it releases the host recording lock instead of leaving it dangling for the next session.
74+
expect(Number(recordingReap!.termTimeoutMs)).toBeGreaterThanOrEqual(5_000);
75+
76+
await runtime?.shutdown();
77+
} finally {
78+
fs.rmSync(stateDir, { recursive: true, force: true });
79+
}
80+
});

‎src/daemon/server/daemon-runtime.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ import { createScreenRecordingAdmissionLedger } from '../screen-recording-admiss
8383
const DAEMON_SESSION_LEASE_RELEASE_TIMEOUT_MS = 1_000;
8484
const DAEMON_PNG_WORKER_TERMINATE_TIMEOUT_MS = 1_000;
8585
const DAEMON_PROVIDER_RELEASE_DRAIN_TIMEOUT_MS = 2_000;
86+
// An orphaned `simctl recordVideo` releases the host-wide recording lock only after it finishes
87+
// finalizing on SIGINT; force-killing it sooner leaves every later recording failing with EBUSY
88+
// (#2170). Bound the grace to the recorder purpose, so daemon startup stays under the client budget.
89+
const DAEMON_RECORDING_REAP_TERM_TIMEOUT_MS = 5_000;
8690

8791
type WritableOutput = {
8892
write: (chunk: string) => unknown;
@@ -515,6 +519,7 @@ export async function startDaemonRuntime(
515519
await reapOwnedProcessRecordsAtStartup(ownedProcessRecords, {
516520
openWebSessionNames: openWebSessionNames(sessionStore),
517521
purposes: ['simctl-screen-recording'],
522+
termTimeoutMs: DAEMON_RECORDING_REAP_TERM_TIMEOUT_MS,
518523
});
519524
await cleanupWebBrowserOrphansForDaemonStartup({
520525
stateDir: baseDir,

0 commit comments

Comments
 (0)