Skip to content

Commit 55cdf11

Browse files
authored
Release the surface-ids test's lock in one rename so the waiting allocator cannot land mid-removal (#1249)
2 parents e915cd5 + bbc021f commit 55cdf11

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

‎vscode-ext/test/surface-ids.test.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { spawnSync } from 'node:child_process';
2-
import { chmod, mkdir, readFile, rm, utimes, writeFile } from 'node:fs/promises';
2+
import { chmod, mkdir, readFile, rename, rm, utimes, writeFile } from 'node:fs/promises';
33
import * as path from 'node:path';
44
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
55

@@ -33,6 +33,13 @@ async function holdLock(pid: number, token = 'theirs') {
3333
await mkdir(lock());
3434
await writeFile(path.join(lock(), 'owner.json'), JSON.stringify({ pid, token }));
3535
}
36+
/** Release that lock as a holder does: moved aside in one step, so a waiter
37+
* never renames its own lock onto the emptied directory mid-removal. */
38+
async function releaseLock() {
39+
const aside = `${lock()}.released`;
40+
await rename(lock(), aside);
41+
await rm(aside, { recursive: true });
42+
}
3643
const lockToken = async () => (JSON.parse(await readFile(path.join(lock(), 'owner.json'), 'utf8')) as { token: string }).token;
3744
/** A pid no process has: one that just exited. */
3845
const deadPid = () => spawnSync(process.execPath, ['-e', '']).pid!;
@@ -86,7 +93,7 @@ describe('createSurfaceIdAllocator', () => {
8693
void waiting.then(() => { settled = true; });
8794
await new Promise((resolve) => setTimeout(resolve, 50));
8895
expect(settled).toBe(false);
89-
await rm(lock(), { recursive: true });
96+
await releaseLock();
9097
expect(await waiting).toEqual(['surface:1']);
9198
await expect(readFile(path.join(lock(), 'owner.json'))).rejects.toThrow();
9299
});

0 commit comments

Comments
 (0)