Skip to content

Race condition in Node.js - double resolve in close callback #5455

Closed

Description

@Dennis-SEG

Summary

The node close callback wrapper can call resolve() twice if a node's close handler both calls done() and throws an error.

Problem

In the close callback handling:

args.push(() => {
    resolve();
});
try {
    r = nodeCloseHandler.apply(node, args);
} catch(err) {
    resolve();
}

If the close handler calls done() (which calls resolve()) and then throws an error, resolve() is called twice.

Severity

LOW - While calling resolve twice on a Promise is a no-op, it indicates a logic issue that could cause confusion or issues in future refactoring.

Proposed Fix

Add a resolved flag:

var resolved = false;
args.push(() => {
    if (!resolved) {
        resolved = true;
        resolve();
    }
});
try {
    r = nodeCloseHandler.apply(node, args);
} catch(err) {
    if (!resolved) {
        resolved = true;
        resolve();
    }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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