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/reliability/zombie-cleanup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
const LOCK_RETRY_MS = 50;
const LOCK_TIMEOUT_MS = 5000;

export function sleepWithoutBusySpin(ms: number): void {
const duration = Math.max(0, Math.floor(ms));
if (duration === 0) return;
const buffer = new SharedArrayBuffer(4);
const view = new Int32Array(buffer);
Atomics.wait(view, 0, 0, duration);
}

function acquireLock(): boolean {
const deadline = Date.now() + LOCK_TIMEOUT_MS;
while (Date.now() < deadline) {
Expand All @@ -34,8 +42,7 @@
releaseLock();
continue;
}
const waitUntil = Date.now() + LOCK_RETRY_MS;
while (Date.now() < waitUntil) { /* spin */ }
sleepWithoutBusySpin(LOCK_RETRY_MS);
continue;
}
return false;
Expand Down Expand Up @@ -331,7 +338,7 @@
const { orphanedIds, liveIds } = getOrphanedAndLiveDeviceIds();
const protectedIds = new Set([...knownDeviceIds, ...liveIds]);

for (const runtime of Object.values(data.devices) as any[]) {

Check warning on line 341 in src/reliability/zombie-cleanup.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
for (const device of runtime) {
if (device.state === 'Booted'
&& orphanedIds.has(device.udid)
Expand Down Expand Up @@ -399,7 +406,7 @@
}, resolvedInterval);
cleanupInterval.unref();
}, resolvedGrace);
(cleanupGraceTimeout as any).unref?.();

Check warning on line 409 in src/reliability/zombie-cleanup.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
}

/**
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/zombie-cleanup-lock.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { sleepWithoutBusySpin } from '../../src/reliability/zombie-cleanup';

describe('zombie cleanup registry lock wait helper', () => {
it('returns immediately for non-positive durations', () => {
const start = Date.now();
sleepWithoutBusySpin(0);
sleepWithoutBusySpin(-10);
expect(Date.now() - start).toBeLessThan(25);
});

it('waits for approximately the requested duration without a JavaScript spin loop', () => {
const start = Date.now();
sleepWithoutBusySpin(15);
expect(Date.now() - start).toBeGreaterThanOrEqual(10);
});
});
Loading