Skip to content

Race condition in file node - stale error handler references in append mode #5453

@Dennis-SEG

Description

@Dennis-SEG

Summary

The file node's append mode can have stale error handler references when the write stream is reused across multiple messages.

Problem

In append mode with static filenames, the write stream is kept open and reused. The error handler is set up once but references the msg and done from the first write. If an error occurs during a later write, the wrong message context is used.

Severity

MEDIUM - Can cause incorrect error reporting or double done() calls when write errors occur.

Proposed Fix

Add per-write error handling with a writeDone flag:

var writeDone = false;
var handleWriteError = function(err) {
    if (!writeDone) {
        writeDone = true;
        node.wstream.removeListener('error', handleWriteError);
        node.error(RED._("file.errors.appendfail",{error:err.toString()}),msg);
        done();
    }
};
node.wstream.on('error', handleWriteError);
node.wstream.write(buf, function() {
    if (!writeDone) {
        writeDone = true;
        node.wstream.removeListener('error', handleWriteError);
        nodeSend(msg);
        done();
    }
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions