diff --git a/src/cli/owner.test.ts b/src/cli/owner.test.ts index 61b746c8..bde52e10 100644 --- a/src/cli/owner.test.ts +++ b/src/cli/owner.test.ts @@ -1,5 +1,6 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { readFile, rm, writeFile } from 'node:fs/promises' +import { join } from 'node:path' import { makeTmpDataFolder } from '../../test/helpers/tmp-data-folder.js' import type { TmpDataFolder } from '../../test/helpers/tmp-data-folder.js' import { AtomicCore } from '../core/index.js' @@ -16,6 +17,25 @@ afterEach(async () => { await data.cleanup() }) +const alive = (pid: number): boolean => { + try { + process.kill(pid, 0) + return true + } catch { + return false + } +} + +async function until(probe: () => Promise, timeoutMs = 5_000): Promise { + const deadline = Date.now() + timeoutMs + for (;;) { + const value = await probe() + if (value !== undefined) return value + if (Date.now() > deadline) throw new Error('timed out') + await new Promise((resolve) => setTimeout(resolve, 10)) + } +} + async function runningCore(): Promise { const core = await AtomicCore.create({ dataFolder: data.root, controlPort: 0 }) cores.push(core) @@ -233,19 +253,36 @@ describe('attachToOwner', () => { it('attaches to the winning owner when its own launcher exits after losing the race', async () => { const core = await runningCore() - const ready = await readFile(data.layout.core.instanceLock, 'utf8') - await rm(data.layout.core.instanceLock) - const script = `require('node:fs').writeFileSync(${JSON.stringify( - data.layout.core.instanceLock - )}, ${JSON.stringify(ready)}); process.exit(3)` - const owner = await attachToOwner({ + const lock = data.layout.core.instanceLock + const ready = await readFile(lock, 'utf8') + // The winner still starts when the loser exits, so only the loser's exit can end the first wait + // and the launcher always takes the path that waits for the winner. With a ready record in place + // before the exit, either side could win that race, and the path ran on some machines only. + const starting = JSON.stringify({ ...(JSON.parse(ready) as Record), state: 'starting' }) + const loserPidFile = join(data.root, 'loser.pid') + await rm(lock) + const script = [ + `const fs = require('node:fs')`, + `fs.writeFileSync(${JSON.stringify(lock)}, ${JSON.stringify(starting)})`, + `fs.writeFileSync(${JSON.stringify(loserPidFile)}, String(process.pid))`, + `process.exit(3)`, + ].join('; ') + const attaching = attachToOwner({ layout: data.layout, clientName: 'race-loser', launch: true, selfCommand: [process.execPath, '-e', script], - timeoutMs: 2000, + timeoutMs: 5000, }) + // The winner becomes ready only once the loser is gone: reaped, so its exit has been seen here. + const loser = await until( + async () => Number(await readFile(loserPidFile, 'utf8').catch(() => '')) || undefined + ) + await until(async () => (alive(loser) ? undefined : true)) + await writeFile(lock, ready) + const owner = await attaching expect(owner.record.instance_id).toBe(core.instanceId) + expect(owner.record.state).toBe('ready') expect(owner.launched).toBe(false) }) diff --git a/src/runtime/fastflowlm/runtime.test.ts b/src/runtime/fastflowlm/runtime.test.ts index 5de70eb5..9f1bd2a4 100644 --- a/src/runtime/fastflowlm/runtime.test.ts +++ b/src/runtime/fastflowlm/runtime.test.ts @@ -75,7 +75,8 @@ beforeEach(async () => { }) afterEach(async () => { await runtime.shutdown() - await rm(root, { recursive: true, force: true }) + // Windows keeps a stopped child's files locked for a moment (EPERM on rmdir): retried. + await rm(root, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 }) }) async function load(modelId = id, overrides: Record = {}) { const pending = runtime.load(modelId, { timeoutSecs: 10, overrides }) diff --git a/test/helpers/tmp-data-folder.ts b/test/helpers/tmp-data-folder.ts index a136a880..e9da7cb7 100644 --- a/test/helpers/tmp-data-folder.ts +++ b/test/helpers/tmp-data-folder.ts @@ -73,6 +73,8 @@ export async function makeTmpDataFolder(prefix = 'atomic-core-data-'): Promise rm(root, { recursive: true, force: true }), + // A child the test just stopped may still be closing a file in the folder: retried, as rm does on + // ENOTEMPTY / EBUSY / EPERM (a macOS gate failed on ENOTEMPTY after a diffusion owner shut down). + cleanup: () => rm(root, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 }), } }