Skip to content

Commit 34e9a9b

Browse files
StiensWoutcodexclaude
committed
fix(server): remove theme staging files after write failures
Co-authored-by: GPT 6.1 Sol <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 1e30f6a commit 34e9a9b

2 files changed

Lines changed: 31 additions & 4 deletions

File tree

‎apps/server/src/environmentTheme.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,28 @@ it.layer(Layer.merge(NodeServices.layer, NodeCrypto.layer))("environment theme",
329329
),
330330
);
331331

332+
it.effect("preserves the theme and removes staging files when writing fails", () =>
333+
withEnvironmentThemes(
334+
{ "nightfall.json": encodeThemeFile(NIGHTFALL_THEME) },
335+
Effect.gen(function* () {
336+
const service = yield* EnvironmentTheme.EnvironmentThemeService;
337+
{
338+
using _write = vi.spyOn(NodeFS, "writeFileSync").mockImplementation(() => {
339+
throw Object.assign(new Error("Disk write failed"), { code: "EIO" });
340+
});
341+
const error = yield* Effect.flip(
342+
service.save({ id: "nightfall", theme: SHARED_THEME, overwrite: true }),
343+
);
344+
assert.equal(error.reason, "storage");
345+
}
346+
assert.deepEqual(yield* service.read("nightfall"), { id: "nightfall", ...NIGHTFALL_THEME });
347+
const fs = yield* FileSystem.FileSystem;
348+
const { environmentThemesDir } = yield* ServerConfig.ServerConfig;
349+
assert.deepEqual(yield* fs.readDirectory(environmentThemesDir), ["nightfall.json"]);
350+
}),
351+
),
352+
);
353+
332354
it.effect("never replaces another publisher during create", () =>
333355
withEnvironmentThemes(
334356
{ "nightfall.json": encodeThemeFile(NIGHTFALL_THEME) },

‎apps/server/src/environmentTheme.ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,10 +339,15 @@ export const publishThemeContents = Effect.fn("EnvironmentTheme.publish")(functi
339339
0o644,
340340
);
341341
try {
342-
NodeFS.writeFileSync(fd, raw);
343-
return NodeFS.fstatSync(fd).ino;
344-
} finally {
345-
NodeFS.closeSync(fd);
342+
try {
343+
NodeFS.writeFileSync(fd, raw);
344+
return NodeFS.fstatSync(fd).ino;
345+
} finally {
346+
NodeFS.closeSync(fd);
347+
}
348+
} catch (cause) {
349+
NodeFS.unlinkSync(stagingPath);
350+
throw cause;
346351
}
347352
},
348353
catch: publishFailure,

0 commit comments

Comments
 (0)