Remove partial downloads when the stream is cut mid-write

When the connection drops while the body is being written, the pipe
destroys the file stream with an error before it has closed.
events.once then rejects on that error, so the temporary file was
never unlinked. A save upload cancelled from the web UI left a
<name>.tmp.zip in the instance saves folder, listed as a save.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
bbassieandClaude Opus 5.5 committed 2026-11-21 02:50:22 +00:00
1 parent 8692ea3941
commit faf214e2a1
2 files changed
+16 -2

No files matched your search

+2 -2
View File
@@ -2,7 +2,6 @@
* @module lib/file_ops
*/
import crypto from "crypto";
import events from "events";
import fs from "node:fs/promises";
import path from "path";
import stream from "stream";
@@ -258,7 +257,8 @@ export async function createTempWriteStream(filePath: string) {
async function discardPartialDownload(writeStream: stream.Writable, tempFilePath: string) {
if (!writeStream.closed) {
writeStream.destroy();
await events.once(writeStream, "close");
// Not events.once, it rejects if the stream is still erroring from the aborted pipe.
await new Promise(resolve => writeStream.once("close", resolve));
}
try {
await fs.unlink(tempFilePath);