Skip to content

OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security

fix: retry Windows artifact replacement while readers close (#2373)

Michael D'Angelo38ee133f2660
diff --git a/plugins/codex-security/mcp-app/src/artifact-io.ts b/plugins/codex-security/mcp-app/src/artifact-io.ts
index e76cf2692..f58be4d8a 100644
--- a/plugins/codex-security/mcp-app/src/artifact-io.ts
+++ b/plugins/codex-security/mcp-app/src/artifact-io.ts
@@ -3,6 +3,7 @@ import type { ZodType, ZodError } from "zod/v4";
import { randomUUID } from "node:crypto";
import { promises as fs } from "node:fs";
import { dirname, isAbsolute, join, sep } from "node:path";
+import { setTimeout as sleep } from "node:timers/promises";
export interface DiscoveryBoardContext {
root: string;
@@ -229,6 +230,33 @@ export async function artifactDestination(
return destination;
}
+const WINDOWS_RENAME_RETRY_MS = [10, 25, 50, 100, 200, 400];
+
+async function publishArtifact(
+ temporary: string,
+ destination: string,
+): Promise<void> {
+ // Windows can deny replacement while a reader or another publisher still
+ // holds the previous file. Retry the same rename, keeping the old file visible.
+ for (let attempt = 0; ; attempt++) {
+ try {
+ await fs.rename(temporary, destination);
+ return;
+ } catch (error) {
+ const delay = WINDOWS_RENAME_RETRY_MS[attempt];
+ if (
+ process.platform !== "win32" ||
+ delay === undefined ||
+ !isNodeError(error) ||
+ !["EPERM", "EBUSY", "EACCES"].includes(error.code ?? "")
+ ) {
+ throw error;
+ }
+ await sleep(delay);
+ }
+ }
+}
+
export async function replaceArtifactText(
path: string,
content: string,
@@ -250,7 +278,7 @@ export async function replaceArtifactText(
throw writeError;
},
);
- await fs.rename(temporary, path);
+ await publishArtifact(temporary, path);
} catch (error) {
await fs.rm(temporary, { force: true });
throw error;
diff --git a/plugins/codex-security/mcp-app/tests/test_helper_files.ts b/plugins/codex-security/mcp-app/tests/test_helper_files.ts
index 8b7a9930f..a328dd4c5 100644
--- a/plugins/codex-security/mcp-app/tests/test_helper_files.ts
+++ b/plugins/codex-security/mcp-app/tests/test_helper_files.ts
@@ -1,4 +1,5 @@
import assert from "node:assert/strict";
+import { promises as fs } from "node:fs";
import { mkdir, rm, symlink } from "node:fs/promises";
import { join, win32 } from "node:path";
import { after, test } from "node:test";
@@ -27,6 +28,66 @@ const { windowsFileSystem } = (await importModule({
const directories = createTemporaryDirectories(true);
after(() => directories.cleanup());
+const { replaceArtifactText } = (await importSource(
+ join(import.meta.dirname, "../src/artifact-io.ts"),
+)) as typeof import("../src/artifact-io.js");
+
+test(
+ "Windows publishes the staged artifact after a reader releases the old file",
+ { skip: process.platform !== "win32" },
+ async (t) => {
+ const directory = await directories.create("held-artifact-");
+ const destination = join(directory, "result.jsonl");
+ await fs.writeFile(destination, "old");
+ const reader = await fs.open(destination, "r");
+ t.after(() => reader.close());
+ const rename = fs.rename.bind(fs);
+ let conflicts = 0;
+ t.mock.method(
+ fs,
+ "rename",
+ async (...args: Parameters<typeof fs.rename>) => {
+ try {
+ return await rename(...args);
+ } catch (error) {
+ if (args[1] === destination && conflicts === 0) {
+ conflicts++;
+ assert.equal(await fs.readFile(destination, "utf8"), "old");
+ await reader.close();
+ }
+ throw error;
+ }
+ },
+ );
+ await replaceArtifactText(destination, "new");
+ assert.equal(conflicts, 1);
+ assert.equal(await fs.readFile(destination, "utf8"), "new");
+ assert.deepEqual(await fs.readdir(directory), ["result.jsonl"]);
+ },
+);
+
+test(
+ "Windows retains the old artifact and cleans the temporary file when a reader stays open",
+ { skip: process.platform !== "win32" },
+ async () => {
+ const directory = await directories.create("locked-artifact-");
+ const destination = join(directory, "result.jsonl");
+ await fs.writeFile(destination, "old");
+ const reader = await fs.open(destination, "r");
+ try {
+ await assert.rejects(
+ replaceArtifactText(destination, "new"),
+ (error: NodeJS.ErrnoException) =>
+ ["EPERM", "EBUSY", "EACCES"].includes(error.code ?? ""),
+ );
+ assert.equal(await fs.readFile(destination, "utf8"), "old");
+ assert.deepEqual(await fs.readdir(directory), ["result.jsonl"]);
+ } finally {
+ await reader.close();
+ }
+ },
+);
+
test(
"POSIX home expansion removes boundary separators while retaining literal suffixes",
{ skip: process.platform === "win32" },