Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: remove a failed ChatGPT ZIP asset's partial file once its handle closes

[._.]/ Adam Eivy380b7b8d0917
diff --git a/server/services/chatgptZipImport.js b/server/services/chatgptZipImport.js
index 47641680b1..47166ca31d 100644
--- a/server/services/chatgptZipImport.js
+++ b/server/services/chatgptZipImport.js
@@ -154,21 +154,29 @@ const UNSAFE_MEMBER_WARNING = '⚠️ ChatGPT import: skipped a ZIP member with
// recognized — the caller then falls back to the friendly name / `.bin`).
const streamAssetToFile = (entry, filePath, max, budget) => new Promise((resolve, reject) => {
const outPromise = createWriteStreamGuarded(filePath);
- let out = null;
const head = [];
let headLen = 0;
let size = 0;
let done = false;
- const fail = (err) => { if (done) return; done = true; out?.destroy(); reject(err); };
+ // Reject only after the write stream has CLOSED. The caller unlinks the
+ // `.part` once this task settles; rejecting first lets the async open land
+ // after that unlink (re-creating the file) or leaves the fd open so Windows
+ // still lists the file (#12701).
+ const fail = (err) => {
+ if (done) return;
+ done = true;
+ outPromise
+ .then((stream) => {
+ if (stream.closed) return undefined;
+ return new Promise((closed) => { stream.once('close', closed); stream.destroy(); });
+ })
+ .catch(() => {})
+ .then(() => reject(err));
+ };
outPromise.then((stream) => {
- // `sink` can already have failed (and destroyed `out`, which was still
- // null at that point) before this resolves — the fd it just opened would
- // otherwise never be closed. Destroy it immediately rather than wiring it
- // into a promise that already settled.
- if (done) { stream.destroy(); return; }
- out = stream;
- out.on('error', fail);
- out.on('finish', () => { if (done) return; done = true; resolve(sniffExtension(Buffer.concat(head))); });
+ if (done) return;
+ stream.on('error', fail);
+ stream.on('finish', () => { if (done) return; done = true; resolve(sniffExtension(Buffer.concat(head))); });
}).catch(fail);
const sink = new Writable({
async write(chunk, _enc, cb) {
@@ -260,6 +268,9 @@ export async function extractChatgptZip(zipPath, { assetDir = PATHS.brainImportA
.on('error', onErr)
.pipe(parser)
.on('entry', (entry) => {
+ // An entry the parser emits after the failure would start a task the
+ // cleanup's `allSettled(inFlight)` snapshot never waits for.
+ if (settled) { entry.autodrain(); return; }
const { path } = entry;
if (IS_ASSET_NAME_MAP(path) || IS_CONVO_JSON(path) || (IS_DAT(path) && isSafeDatMember(path))) {
try { archiveBudget.addEntry(); } catch (err) { onErr(err); return; }
diff --git a/server/services/chatgptZipImport.test.js b/server/services/chatgptZipImport.test.js
index 16b2c5ddf8..e411cfaca1 100644
--- a/server/services/chatgptZipImport.test.js
+++ b/server/services/chatgptZipImport.test.js
@@ -1,4 +1,4 @@
-import { describe, it, expect, beforeEach, afterEach } from 'vitest';
+import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import { mkdtemp, rm, writeFile, readdir, readFile, mkdir } from 'fs/promises';
import { tmpdir } from 'os';
import { join } from 'path';
@@ -72,6 +72,41 @@ describe('chatgptZipImport service', () => {
expect(await readdir(assetDir)).toEqual([]);
});
+ // Windows CI opens/closes `.part` handles slowly enough that cleanup ran
+ // before the failed member's stream settled (#12701). A delayed open makes
+ // that ordering deterministic.
+ it('removes a failed member\'s partial file even when its open lands late', async () => {
+ // Every stream the import opens, so the assertion waits for each handle
+ // to close instead of racing whatever open/close is still pending.
+ const streamsClosed = [];
+ vi.resetModules();
+ vi.doMock('../lib/fileUtils.js', async (importOriginal) => {
+ const real = await importOriginal();
+ return {
+ ...real,
+ createWriteStreamGuarded: async (p, o) => {
+ await new Promise((r) => setTimeout(r, 50));
+ const stream = await real.createWriteStreamGuarded(p, o);
+ streamsClosed.push(new Promise((r) => stream.once('close', r)));
+ return stream;
+ },
+ };
+ });
+ try {
+ const { extractChatgptZip: extract } = await import('./chatgptZipImport.js');
+ const zipPath = await writeZip([['file-first.dat', PNG], ['file-second.dat', PNG]]);
+ const assetDir = join(TMP, 'assets');
+ await expect(extract(zipPath, { assetDir, maxAssetBytes: PNG.length + 1 }))
+ .rejects.toMatchObject({ status: 413 });
+ await vi.waitFor(() => expect(streamsClosed).toHaveLength(2));
+ await Promise.all(streamsClosed);
+ expect(await readdir(assetDir)).toEqual([]);
+ } finally {
+ vi.doUnmock('../lib/fileUtils.js');
+ vi.resetModules();
+ }
+ });
+
describe('sniffExtension', () => {
it('detects png/jpeg/wav/pdf from magic bytes', () => {
expect(sniffExtension(PNG)).toBe('.png');