From 54cb9451126bd5629134988b357a2fb7912878b8 Mon Sep 17 00:00:00 2001 From: Misha Skvortsov Date: Fri, 13 Nov 2026 16:26:52 +0300 Subject: [PATCH 1/2] test(cli): make the lost-launch race take the waiting path every time The race test let its fake launcher publish a ready owner record and exit. attachToOwner races waitForPublishedOwner against the launcher's exit, so on some machines the published record won, the path that waits for the winner (src/cli/owner.ts 210-218) never ran, and owner.ts fell under its 98% statement floor: the v0.23.5 release gate failed on macOS that way. The launcher now writes the winner as still starting and exits; the test makes the record ready only once the launcher is gone, so only its exit can end the first wait. Co-Authored-By: Claude Opus 5.5 --- src/cli/owner.test.ts | 51 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 44 insertions(+), 7 deletions(-) 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) }) From a1647f4a9fac32b1cfd1294514cddbd00f38f9c3 Mon Sep 17 00:00:00 2001 From: Misha Skvortsov Date: Fri, 13 Nov 2026 16:39:45 +0300 Subject: [PATCH 2/2] test: retry removing temp folders a stopped child may still touch Two gates failed while removing a test's temporary folder rather than on a test: the v0.23.5 release gate on macOS with ENOTEMPTY after the diffusion owner test shut its core down, and PR CI on Windows with EPERM after the FastFlowLM runtime test. fs.rm retries exactly those errors when asked to; the managed-environment tests already do. The shared tmp-data-folder helper and the FastFlowLM test now retry five times, 100 ms apart. Co-Authored-By: Claude Opus 5.5 --- src/runtime/fastflowlm/runtime.test.ts | 3 ++- test/helpers/tmp-data-folder.ts | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) 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 }), } }