Skip to content

Commit 5751fc1

Browse files
cixelfengmk2
authored andcommitted
fix: sockLen being miscalculated when removing sockets (#60)
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.
1 parent 82ff0e8 commit 5751fc1

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

lib/_http_agent.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ Agent.prototype.removeSocket = function removeSocket(s, options) {
380380

381381
// [patch start]
382382
var freeLen = this.freeSockets[name] ? this.freeSockets[name].length : 0;
383-
var sockLen = freeLen + this.sockets[name] ? this.sockets[name].length : 0;
383+
var sockLen = freeLen + (this.sockets[name] ? this.sockets[name].length : 0);
384384
// [patch end]
385385

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

0 commit comments

Comments
 (0)