Skip to content

Commit 7bb3909

Browse files
authored
fix(workers): clean stale bundle install staging (#124057)
Serialize node worker installs per Gateway namespace so a later bundle hash reclaims interrupted staging from older hashes instead of leaking it indefinitely.
1 parent ab4557d commit 7bb3909

2 files changed

Lines changed: 8 additions & 7 deletions

File tree

‎src/node-host/node-worker-bundle-installer.test.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,14 @@ describe("node worker bundle installer", () => {
9595
return { gatewayUrl: `ws://127.0.0.1:${address.port}`, requests };
9696
}
9797

98-
it("atomically installs and reuses an exact namespaced bundle", async () => {
98+
it("atomically installs, reuses, and cleans prior-hash crash staging", async () => {
9999
const fixture = await bundleFixture();
100+
const staleBundleHash = "f".repeat(64);
100101
const staleStaging = path.join(
101102
root,
102103
fixture.input.gatewayNamespace,
103104
"bundles",
104-
`.staging-${fixture.input.build.bundleHash}-crashed`,
105+
`.staging-${staleBundleHash}-crashed`,
105106
);
106107
await fs.mkdir(staleStaging, { recursive: true });
107108
const served = await serve(fixture.archive, fixture.input.archive.token);

‎src/node-host/node-worker-bundle-installer.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,11 @@ async function validateInstalledBundle(
165165
}
166166
}
167167

168-
async function removeStaleInstallStaging(bundlesRoot: string, bundleHash: string): Promise<void> {
169-
const prefix = `.staging-${bundleHash}-`;
168+
async function removeStaleInstallStaging(bundlesRoot: string): Promise<void> {
170169
const entries = await fsp.readdir(bundlesRoot, { withFileTypes: true });
171170
await Promise.all(
172171
entries.map(async (entry) => {
173-
if (entry.name.startsWith(prefix) && entry.isDirectory() && !entry.isSymbolicLink()) {
172+
if (entry.name.startsWith(".staging-") && entry.isDirectory() && !entry.isSymbolicLink()) {
174173
await fsp.rm(path.join(bundlesRoot, entry.name), { recursive: true, force: true });
175174
}
176175
}),
@@ -227,7 +226,8 @@ export class NodeWorkerBundleInstaller {
227226
signal?: AbortSignal;
228227
}): Promise<WorkerAdmissionHandshake> {
229228
const { input } = params;
230-
const key = `${input.gatewayNamespace}\0${input.build.bundleHash}`;
229+
// One namespace owns every staging sibling, so serialize it before sweeping crash residue.
230+
const key = input.gatewayNamespace;
231231
return await this.#operations.enqueue(key, async () => {
232232
try {
233233
params.signal?.throwIfAborted();
@@ -237,7 +237,7 @@ export class NodeWorkerBundleInstaller {
237237
return structuredClone(input.build);
238238
}
239239
await fsp.mkdir(bundlesRoot, { recursive: true, mode: 0o700 });
240-
await removeStaleInstallStaging(bundlesRoot, input.build.bundleHash);
240+
await removeStaleInstallStaging(bundlesRoot);
241241
const operationRoot = await fsp.mkdtemp(
242242
path.join(bundlesRoot, `.staging-${input.build.bundleHash}-`),
243243
);

0 commit comments

Comments
 (0)