From 178784c22f67a9c36bffc1f963ec754864d5dc31 Mon Sep 17 00:00:00 2001 From: guoyangzhen Date: Mon, 16 Mar 2026 22:46:59 +0800 Subject: [PATCH] fix: prevent file handle leak when maxFiles is exceeded Fixes #987 When maxFiles limit is reached, the fileBegin event handler calls _error(), but _handlePart continues and opens a write stream for the new file. These file handles are never closed. Fix: check this.error after emitting fileBegin and before file.open(). If an error occurred (e.g., maxFiles exceeded), decrement _flushing and return early to prevent the file stream from being opened. --- src/Formidable.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Formidable.js b/src/Formidable.js index 5ea964e0..2ade16e0 100644 --- a/src/Formidable.js +++ b/src/Formidable.js @@ -403,6 +403,12 @@ class IncomingForm extends EventEmitter { }); this.emit('fileBegin', part.name, file); + // Check for error after fileBegin (e.g., maxFiles exceeded) to avoid leaking file handles + if (this.error) { + this._flushing -= 1; + return; + } + file.open(); this.openedFiles.push(file);