Skip to content

Commit

Permalink
fix: sockLen being miscalculated when removing sockets (#60)
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 authored and fengmk2 committed Oct 18, 2018
1 parent 82ff0e8 commit 5751fc1
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion lib/_http_agent.js
Original file line number Diff line number Diff line change
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 5751fc1

Please sign in to comment.