Skip to content

Commit 3d1e4ec

Browse files
committed
fix(tests): await the lock holder before removing its temp root
The contention test released its holder and dropped the exit promise on the floor, so afterEach could remove the temp root while that child still had the coordinator database open. Windows refuses to unlink a file another process holds, so teardown threw EBUSY and the failure was attributed to a test that had already proved its assertion. POSIX unlinks an open file regardless, which is why this only ever appeared on Windows, and only under full-suite load where the child exits slower. Await the holder, and let teardown retry briefly before giving the directory back to the OS: `force` covers a missing path, not a locked one, and a temp directory left behind is a smaller lie than a green test reported red.
1 parent 4309560 commit 3d1e4ec

1 file changed

Lines changed: 24 additions & 3 deletions

File tree

‎tests/codex-inject-write-lock.test.ts‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,23 @@ beforeEach(() => {
5050
});
5151

5252
afterEach(() => {
53-
while (cleanup.length) rmSync(cleanup.pop()!, { recursive: true, force: true });
53+
while (cleanup.length) {
54+
const dir = cleanup.pop()!;
55+
// `force` covers a missing path, not a locked one: a child that is still exiting
56+
// can hold a coordinator file open for a few milliseconds, and Windows answers
57+
// EBUSY rather than unlinking underneath it. Retry briefly, then leave the temp
58+
// directory to the OS -- failing teardown would blame whichever test ran here.
59+
for (let attempt = 0; attempt < 5; attempt++) {
60+
try {
61+
rmSync(dir, { recursive: true, force: true });
62+
break;
63+
} catch (err) {
64+
const code = (err as NodeJS.ErrnoException).code;
65+
if (code !== "EBUSY" && code !== "EPERM" && code !== "ENOTEMPTY") throw err;
66+
if (attempt < 4) Bun.sleepSync(50 * (attempt + 1));
67+
}
68+
}
69+
}
5470
});
5571

5672
describe("the lock is on the production path", () => {
@@ -98,7 +114,7 @@ describe("the lock is on the production path", () => {
98114
* lock module while a real injection runs; the injection must report busy and
99115
* must not have written its candidate bytes.
100116
*/
101-
test("a held lock makes real injection report busy and write nothing", () => {
117+
test("a held lock makes real injection report busy and write nothing", async () => {
102118
seedNative();
103119
// Establish the coordinator first: a clean home has no row, and the holder
104120
// needs one to contend over.
@@ -130,7 +146,12 @@ describe("the lock is on the production path", () => {
130146
const contender = runInject(20200);
131147

132148
writeFileSync(releaseMarker, "go");
133-
holder.exited.then(() => undefined);
149+
// AWAIT the holder. Dropping its exit on the floor left a live child owning the
150+
// coordinator database while afterEach removed the temp root, and Windows refuses
151+
// to unlink a file another process still has open -- so teardown failed with EBUSY
152+
// and blamed this test for a race it had already won. POSIX unlinks regardless,
153+
// which is why only Windows ever saw it, and only under full-suite load.
154+
await holder.exited;
134155

135156
expect(contender.success).toBeFalse();
136157
expect(contender.retryable).toBeTrue();

0 commit comments

Comments
 (0)