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([issue-1527]): tear down in-flight entry stream on parser destroy

Adam Eivy578d1a3551eb
diff --git a/server/lib/zipStream.js b/server/lib/zipStream.js
index 8a8c34e1fb..bda24083ec 100644
--- a/server/lib/zipStream.js
+++ b/server/lib/zipStream.js
@@ -41,6 +41,19 @@ export function parseZip() {
final(cb) {
if (!closed) { closed = true; emitter.emit('close'); }
cb();
+ },
+ destroy(err, cb) {
+ // On abort (a consumer calls `parser.destroy()` to stop ingestion mid-ZIP),
+ // the entry currently streaming would otherwise be stranded: its
+ // `passThrough` is never fed again and never `.end()`-ed, so anything
+ // piping it (e.g. a write-to-disk or collect consumer awaiting the entry's
+ // completion) hangs forever. End it so EOF flows downstream and those
+ // consumers settle. `end()` (not `destroy(err)`) avoids an unhandled error
+ // on the passThrough, since `.pipe()` doesn't forward errors.
+ if (currentEntry?.passThrough && !currentEntry.passThrough.writableEnded) {
+ currentEntry.passThrough.end();
+ }
+ cb(err);
}
});
diff --git a/server/lib/zipStream.test.js b/server/lib/zipStream.test.js
index b538f539dc..0a460216a6 100644
--- a/server/lib/zipStream.test.js
+++ b/server/lib/zipStream.test.js
@@ -141,6 +141,39 @@ describe('parseZip', () => {
const entries = await collectEntries(Buffer.concat(pieces));
expect(entries[0].data.toString()).toBe(data.toString());
});
+
+ it('ends the in-flight entry stream when the parser is destroyed mid-entry (no hang)', async () => {
+ // A consumer that aborts ingestion (parser.destroy()) while an entry is
+ // still streaming must not strand that entry's stream: anything piping it
+ // and awaiting completion would otherwise hang forever. Feed a stored entry
+ // whose declared size (1000) exceeds the bytes we actually supply (100), so
+ // the entry is emitted and piped but left mid-stream when we abort.
+ const payload = Buffer.alloc(1000, 0x41);
+ const fullEntry = buildEntry('big.dat', payload);
+ const headerLen = fullEntry.length - payload.length;
+ const partial = fullEntry.subarray(0, headerLen + 100);
+
+ const parser = parseZip();
+ const sinkFinished = new Promise((resolve) => {
+ parser.on('entry', (entry) => {
+ const chunks = [];
+ const sink = new Writable({ write(c, _e, cb) { chunks.push(c); cb(); } });
+ // Resolves only if the entry stream is allowed to finish — i.e. the
+ // parser's teardown ended it rather than abandoning it mid-flight.
+ sink.on('finish', () => resolve(Buffer.concat(chunks)));
+ entry.pipe(sink);
+ });
+ });
+
+ // Await the write callback so processBuffer has emitted+piped the entry and
+ // buffered its 100 bytes before we abort.
+ await new Promise((res) => parser.write(partial, res));
+ parser.destroy();
+
+ // Without the destroy→end teardown this never resolves and the test times out.
+ const data = await sinkFinished;
+ expect(data.length).toBe(100);
+ });
});
describe('extractZipEntryToBuffer', () => {
diff --git a/server/services/chatgptZipImport.js b/server/services/chatgptZipImport.js
index ece699b08a..d87ee2a536 100644
--- a/server/services/chatgptZipImport.js
+++ b/server/services/chatgptZipImport.js
@@ -244,13 +244,16 @@ export async function extractChatgptZip(zipPath, { assetDir = PATHS.brainImportA
// temp files would otherwise be orphaned on disk. Remove them before
// re-throwing so a bad upload can't accumulate `.part` files.
//
- // Wait for every in-flight asset write to settle FIRST. `settle(reject)`
- // rejects this Promise immediately and tears down the source/parser, but a
- // `streamAssetToFile` write already in progress keeps flushing to its
- // `.part` file as its stream is destroyed. Unlinking before those streams
- // close races them: the write can re-create the `.part` file after the
- // unlink and leave it orphaned (the intermittent full-suite failure). Once
- // settled, every write handle is closed, so the unlink is final.
+ // Wait for every in-flight member task to settle FIRST. `settle(reject)`
+ // rejects this Promise immediately and destroys the source/parser, but a
+ // `streamAssetToFile` whose write stream hasn't finished is still mid-flight:
+ // `createWriteStream` opens its `.part` file asynchronously, so unlinking now
+ // can race the open — the unlink no-ops (file not created yet), then the open
+ // lands and orphans the `.part` (the intermittent full-suite failure). The
+ // parser's teardown ends the in-flight entry stream (see zipStream.js
+ // `destroy`), so each task settles deterministically — once `allSettled`
+ // resolves, every write handle is closed and the file's on-disk existence is
+ // settled, making the unlink below final rather than racing a pending open.
await Promise.allSettled(inFlight);
await cleanupTempFiles(tempPaths);
throw err;