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
pull/5460/head
Dennis-SEG 2026-01-24 23:20:44 +01:00 committed by Dennis Bosmans
parent 1019d52f78
commit 4822636fd3
1 changed files with 14 additions and 4 deletions

View File

@ -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();
});
}