From 4822636fd36bb7bff3e2d330cb3b62e9727ee67d Mon Sep 17 00:00:00 2001 From: Dennis-SEG Date: Sat, 24 Jan 2026 23:20:44 +0100 Subject: [PATCH] fix: prevent stale error handler references in file node append mode Use write callback's error argument instead of adding/removing error handlers. This ensures the correct msg/done scope is used for each write operation. The stream-level error handler now only handles errors for dynamic filenames, while static filename writes handle errors via the write callback. Addresses review feedback from @knolleary. Fixes #5453 --- .../@node-red/nodes/core/storage/10-file.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/packages/node_modules/@node-red/nodes/core/storage/10-file.js b/packages/node_modules/@node-red/nodes/core/storage/10-file.js index 5cff2b631..147cce690 100644 --- a/packages/node_modules/@node-red/nodes/core/storage/10-file.js +++ b/packages/node_modules/@node-red/nodes/core/storage/10-file.js @@ -183,15 +183,25 @@ module.exports = function(RED) { } catch(err) { } }); + // Note: For static filenames, write errors are handled in the write callback. + // This handler catches stream-level errors for dynamic filenames. node.wstream.on("error", function(err) { - node.error(RED._("file.errors.appendfail",{error:err.toString()}),msg); - done(); + // Only handle if not a static filename (those use write callback) + if (node.filenameType !== "str" && node.filenameType !== "env") { + node.error(RED._("file.errors.appendfail",{error:err.toString()}),msg); + done(); + } }); } if (node.filenameType === "str" || node.filenameType === "env") { // Static filename - write and reuse the stream next time - node.wstream.write(buf, function() { - nodeSend(msg); + // Use write callback's error argument for proper scoping of msg/done + node.wstream.write(buf, function(err) { + if (err) { + node.error(RED._("file.errors.appendfail",{error:err.toString()}),msg); + } else { + nodeSend(msg); + } done(); }); }