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(build): retry Windows plugin bundle renames (#2370)

Michael D'Angelo206a9c8106c3
diff --git a/sdk/typescript/scripts/build-plugin.mjs b/sdk/typescript/scripts/build-plugin.mjs
index d426e06ba..40ec5d8ce 100644
--- a/sdk/typescript/scripts/build-plugin.mjs
+++ b/sdk/typescript/scripts/build-plugin.mjs
@@ -12,12 +12,32 @@ import {
rm,
} from "node:fs/promises";
import { dirname, join, posix, resolve } from "node:path";
+import { setTimeout as delay } from "node:timers/promises";
import { isMain } from "./is-main.mjs";
import { pluginContractFiles } from "./plugin-contract.mjs";
const packageRoot = resolve(import.meta.dirname, "..");
const repositoryRoot = resolve(packageRoot, "../..");
+async function retryWindowsRename(operation, rollback) {
+ // Windows can temporarily deny a directory rename while a file inside is open.
+ for (let attempt = 0; ; attempt++) {
+ try {
+ return await operation();
+ } catch (error) {
+ await rollback?.();
+ if (
+ process.platform !== "win32" ||
+ !["EPERM", "EACCES", "EBUSY"].includes(error.code) ||
+ attempt === 5
+ ) {
+ throw error;
+ }
+ await delay(100 * (attempt + 1));
+ }
+ }
+}
+
function validatePath(path) {
const parts = path.split("/");
if (
@@ -119,21 +139,24 @@ export async function buildBundledPlugin({
) {
throw new Error("Bundled plugin generated files outside its contract.");
}
- try {
- await rename(destination, previous);
- preservePrevious = true;
- } catch (error) {
- if (error.code !== "ENOENT") throw error;
- }
- try {
- await rename(staged, destination);
- } catch (error) {
- if (preservePrevious) {
- await rename(previous, destination);
- preservePrevious = false;
- }
- throw error;
- }
+ await retryWindowsRename(
+ async () => {
+ try {
+ await rename(destination, previous);
+ preservePrevious = true;
+ } catch (error) {
+ if (error.code !== "ENOENT") throw error;
+ }
+ await rename(staged, destination);
+ },
+ async () => {
+ // Keep the previous bundle available to readers during publication backoff.
+ if (preservePrevious) {
+ await retryWindowsRename(() => rename(previous, destination));
+ preservePrevious = false;
+ }
+ },
+ );
preservePrevious = false;
} finally {
// Leave the previous bundle available if restoring it fails.
diff --git a/sdk/typescript/tests-ts/build-plugin.test.ts b/sdk/typescript/tests-ts/build-plugin.test.ts
index 8a8130ff7..e02c08fde 100644
--- a/sdk/typescript/tests-ts/build-plugin.test.ts
+++ b/sdk/typescript/tests-ts/build-plugin.test.ts
@@ -375,6 +375,65 @@ describe("bundled plugin build", () => {
expect(preflight.stderr).toBe("");
});
+ test
+ .skipIf(process.platform !== "win32")
+ .each(["released", "persistent", "unrestorable"])(
+ "handles Windows file locks during bundle publication (%s)",
+ async (lock) => {
+ const root = await temporaryDirectory();
+ const source = join(root, "source");
+ const destination = join(root, "_bundled_plugin");
+ const manifest = ".codex-plugin/plugin.json";
+ await writeFixture(source, manifest, "new");
+ await writeFixture(destination, manifest, "previous");
+ await writeFixture(
+ source,
+ "contract.json",
+ JSON.stringify({ externalOwnedExact: [manifest], shippedExact: [] }),
+ );
+ const fixture = fileURLToPath(
+ new URL("./fixtures/locked-bundle-build.mjs", import.meta.url),
+ );
+ const { stdout } = await execFileAsync("node", [fixture, root, lock]);
+ const outcome = JSON.parse(stdout);
+ if (lock === "released") {
+ expect(outcome).toEqual({
+ failed: ["_bundled_plugin", "bundle", "previous"],
+ });
+ expect(await readFile(join(destination, manifest), "utf8")).toBe("new");
+ } else {
+ expect(["EPERM", "EACCES", "EBUSY"]).toContain(outcome.code);
+ expect(outcome.failed).toEqual([
+ "_bundled_plugin",
+ "bundle",
+ "previous",
+ ]);
+ if (lock === "unrestorable") {
+ const remaining = await readdir(root);
+ const backups = remaining.filter((name) =>
+ name.startsWith(".plugin-build-"),
+ );
+ expect(backups).toHaveLength(1);
+ expect(remaining).not.toContain("_bundled_plugin");
+ expect(
+ await readFile(
+ join(root, backups[0]!, "previous", manifest),
+ "utf8",
+ ),
+ ).toBe("previous");
+ return;
+ }
+ expect(await readFile(join(destination, manifest), "utf8")).toBe(
+ "previous",
+ );
+ }
+ expect((await readdir(root)).sort()).toEqual([
+ "_bundled_plugin",
+ "source",
+ ]);
+ },
+ );
+
test.each(["compiler", "file contract"])(
"preserves the previous bundle when the %s fails",
async (failure) => {
diff --git a/sdk/typescript/tests-ts/fixtures/locked-bundle-build.mjs b/sdk/typescript/tests-ts/fixtures/locked-bundle-build.mjs
new file mode 100644
index 000000000..139191043
--- /dev/null
+++ b/sdk/typescript/tests-ts/fixtures/locked-bundle-build.mjs
@@ -0,0 +1,64 @@
+import assert from "node:assert/strict";
+import fs from "node:fs/promises";
+import { syncBuiltinESMExports } from "node:module";
+import { basename, join } from "node:path";
+import timers from "node:timers/promises";
+import { buildBundledPlugin } from "../../scripts/build-plugin.mjs";
+
+const [root, mode] = process.argv.slice(2);
+const manifest = ".codex-plugin/plugin.json";
+const source = join(root, "source");
+const destination = join(root, "_bundled_plugin");
+const rename = fs.rename;
+const delay = timers.setTimeout;
+const failed = new Set();
+let waitingToRestore = false;
+
+// Lock a real descendant during the first rename of each directory. Persistent
+// cases also hold a file during every subsequent publication or restore attempt.
+fs.rename = async (from, to) => {
+ const step = basename(from);
+ const locked =
+ !failed.has(step) ||
+ (step === "bundle" && mode !== "released") ||
+ (step === "previous" && mode === "unrestorable");
+ const handle = locked ? await fs.open(join(from, manifest), "r") : undefined;
+ try {
+ await rename(from, to);
+ if (step === "previous") waitingToRestore = false;
+ } catch (error) {
+ failed.add(step);
+ waitingToRestore = step === "previous";
+ throw error;
+ } finally {
+ await handle?.close();
+ }
+};
+timers.setTimeout = async (...args) => {
+ if (!waitingToRestore) {
+ assert.equal(
+ await fs.readFile(join(destination, manifest), "utf8"),
+ "previous",
+ );
+ }
+ return delay(...args);
+};
+syncBuiltinESMExports();
+
+let code;
+try {
+ await buildBundledPlugin({
+ source,
+ destination,
+ contractPath: join(source, "contract.json"),
+ });
+} catch (error) {
+ if (
+ mode === "released" ||
+ !["EPERM", "EACCES", "EBUSY"].includes(error.code)
+ ) {
+ throw error;
+ }
+ code = error.code;
+}
+console.log(JSON.stringify({ code, failed: [...failed] }));