@devries-cameron20 / composio Mirror · read-only / commits / 3f43cb5b1
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 09:45 UTC

fix(core): remove a claimed download file when closing it fails

jkomyno committed Sep 30, 2026 at 12:45 UTC 3f43cb5b118752ca9780e55dcfaa40dbe6c0f5d3
2 files changed +35 -9
ts/packages/core/src/platform/node.ts
+10 -8
@@ -111,15 +111,17 @@ export const platform = {
111
112 writeFileExclusiveSync(filePath: string, content: Uint8Array): void {
113 const fd = fs.openSync(filePath, 'wx');
114 - let written = false;
114 try {
116 - fs.writeFileSync(fd, content);
117 - written = true;
118 - } finally {
119 - fs.closeSync(fd);
120 - // The path was created here, so a failed write must not leave a
121 - // partial file that a retry would treat as an existing download.
122 - if (!written) fs.rmSync(filePath, { force: true });
115 + try {
116 + fs.writeFileSync(fd, content);
117 + } finally {
118 + fs.closeSync(fd);
119 + }
120 + } catch (error) {
121 + // The path was created here, so a failed write or close must not leave
122 + // a partial file that a retry would treat as an existing download.
123 + fs.rmSync(filePath, { force: true });
124 + throw error;
125 }
126 },
127 } as Platform;
ts/packages/core/test/platform/node.test.ts
+25 -1
@@ -7,7 +7,11 @@ import { platform } from '../../src/platform/node';
7
8 vi.mock('node:fs', async importOriginal => {
9 const actual = await importOriginal<typeof import('node:fs')>();
10 - return { ...actual, writeFileSync: vi.fn(actual.writeFileSync) };
10 + return {
11 + ...actual,
12 + closeSync: vi.fn(actual.closeSync),
13 + writeFileSync: vi.fn(actual.writeFileSync),
14 + };
15 });
16
17 const invertAsciiCase = (value: string): string =>
@@ -63,6 +67,26 @@ describe('node platform exclusive writes', () => {
67 }
68 });
69
70 + it('removes the file it created when closing it fails', () => {
71 + const root = mkdtempSync(path.join(os.tmpdir(), 'composio-exclusive-write-'));
72 + try {
73 + const filePath = path.join(root, 'report.pdf');
74 + const actual = vi.mocked(fs.closeSync).getMockImplementation()!;
75 + // Some filesystems report a deferred write error only on close.
76 + vi.mocked(fs.closeSync).mockImplementationOnce(fd => {
77 + actual(fd);
78 + throw Object.assign(new Error('disk quota exceeded'), { code: 'EDQUOT' });
79 + });
80 +
81 + expect(() => platform.writeFileExclusiveSync(filePath, new Uint8Array([1, 2]))).toThrow(
82 + 'disk quota exceeded'
83 + );
84 + expect(existsSync(filePath)).toBe(false);
85 + } finally {
86 + rmSync(root, { recursive: true, force: true });
87 + }
88 + });
89 +
90 it('leaves an existing file untouched', () => {
91 const root = mkdtempSync(path.join(os.tmpdir(), 'composio-exclusive-write-'));
92 try {