Skip to content

Commit

Permalink
fix: sockLen being miscalculated when removing sockets
Browse files Browse the repository at this point in the history
Order of operations issue with + and ternary statements:

    console.log(0 + '' ? 'a' : 'b') // a

The code:

    freeLen + this.sockets[name] ? this.sockets[name].length : 0;

Is equivalent to:

    (freeLen + this.sockets[name]) ? this.sockets[name].length : 0;

When `this.sockets[name]` exists, it is an array, `(freeLen + this.sockets[name])` evaluates to a string, which evaluates to a string (true). When it does not, `(freeLen + this.sockets[name])` evaluates to `NaN` (false).
Either way, `freeLen` is ignored.
  • Loading branch information
cixel committed Apr 10, 2018
1 parent 7cbca8e commit 6ee9e21
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion lib/_http_agent.js
Expand Up @@ -380,7 +380,7 @@ Agent.prototype.removeSocket = function removeSocket(s, options) {

// [patch start]
var freeLen = this.freeSockets[name] ? this.freeSockets[name].length : 0;
var sockLen = freeLen + this.sockets[name] ? this.sockets[name].length : 0;
var sockLen = freeLen + (this.sockets[name] ? this.sockets[name].length : 0);
// [patch end]

if (this.requests[name] && this.requests[name].length && sockLen < this.maxSockets) {
Expand Down

0 comments on commit 6ee9e21

Please sign in to comment.