fix(artifacts): honor cancellation at the output publication boundary

This commit is contained in:
morluto
2026-10-13 15:12:17 +08:00
parent 9049dbaf0c
commit e7882b34f4
7 changed files with 103 additions and 16 deletions
@@ -10,6 +10,8 @@ const injectedFilesystemFailures = vi.hoisted(() => ({
closeNext: false,
blockCloseRetries: false,
chmodNext: false,
syncAbortAfter: undefined as AbortController | undefined,
syncCount: 0,
rootIdentityPath: undefined as string | undefined,
}));
@@ -44,6 +46,15 @@ vi.mock("node:fs/promises", async (importOriginal) => {
await close();
};
}
if (injectedFilesystemFailures.syncAbortAfter !== undefined) {
const sync = handle.sync.bind(handle);
handle.sync = async () => {
await sync();
injectedFilesystemFailures.syncCount += 1;
if (injectedFilesystemFailures.syncCount === 2)
injectedFilesystemFailures.syncAbortAfter?.abort();
};
}
return handle;
},
};
@@ -82,6 +93,34 @@ describe("safe artifact output close failures", () => {
},
);
it.skipIf(process.platform === "win32")(
"keeps the tree unpublished when cancellation arrives during commit durability",
async () => {
const parent = await createTestTempDirectory("rea-safe-output-cancel-");
const output = join(parent, "unpublished");
const tree = await SafeOutputTree.create(output);
const bytes = Buffer.from("pending output");
await tree.write("file.txt", Readable.from(bytes), {
sha256: createHash("sha256").update(bytes).digest("hex"),
bytes: bytes.byteLength,
});
const controller = new AbortController();
injectedFilesystemFailures.syncCount = 0;
injectedFilesystemFailures.syncAbortAfter = controller;
try {
await expect(tree.commit(controller.signal)).rejects.toMatchObject({
reason: "cancelled",
});
expect(tree.published).toBe(false);
} finally {
injectedFilesystemFailures.syncAbortAfter = undefined;
injectedFilesystemFailures.syncCount = 0;
}
expect(await tree.rollback()).toMatchObject({ status: "complete" });
await expect(access(output)).rejects.toMatchObject({ code: "ENOENT" });
},
);
it.skipIf(process.platform === "win32")(
"preserves setup failure when closing the staging handle also fails",
async () => {