Skip to content

Commit fcb2f35

Browse files
committed
fix(test): tolerate provider temp cleanup races
1 parent 719d52a commit fcb2f35

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

‎test/integration/provider-scenarios/harness.ts‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import assert from 'node:assert/strict';
22
import fs from 'node:fs';
33
import os from 'node:os';
44
import path from 'node:path';
5+
import { setTimeout as sleep } from 'node:timers/promises';
56
import { createAgentDeviceClient } from '../../../src/agent-device-client.ts';
67
import type { AgentDeviceDaemonTransport } from '@agent-device/contracts/client';
78
import type { AgentDeviceClient } from '../../../src/client/client-types.ts';
@@ -130,7 +131,7 @@ export async function createProviderScenarioHarness(
130131
setSession: (name, session) => sessionStore.set(name, session),
131132
close: async () => {
132133
await deviceRuntimeGateway.shutdown();
133-
removeProviderScenarioTempDir(sessionDir);
134+
await removeProviderScenarioTempDir(sessionDir);
134135
},
135136
};
136137
}
@@ -164,12 +165,21 @@ export async function withProviderScenarioTempDir<TResult>(
164165
try {
165166
return await run(dir);
166167
} finally {
167-
removeProviderScenarioTempDir(dir);
168+
await removeProviderScenarioTempDir(dir);
168169
}
169170
}
170171

171-
function removeProviderScenarioTempDir(dir: string): void {
172-
fs.rmSync(dir, PROVIDER_SCENARIO_TEMP_REMOVE_OPTIONS);
172+
async function removeProviderScenarioTempDir(dir: string): Promise<void> {
173+
for (let attempt = 0; ; attempt += 1) {
174+
try {
175+
fs.rmSync(dir, PROVIDER_SCENARIO_TEMP_REMOVE_OPTIONS);
176+
return;
177+
} catch (error) {
178+
const code = (error as NodeJS.ErrnoException).code;
179+
if (attempt >= 5 || !['EBUSY', 'ENOTEMPTY', 'EPERM'].includes(code ?? '')) throw error;
180+
await sleep(50 * (attempt + 1));
181+
}
182+
}
173183
}
174184

175185
export function restoreEnv(key: string, previous: string | undefined): void {

0 commit comments

Comments
 (0)