Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sockLen was being miscalculated when removing sockets #60

Merged
merged 2 commits into from Oct 18, 2018

Conversation

cixel
Copy link
Contributor

@cixel cixel commented Apr 10, 2018

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, which does not seem intended.

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.
@codecov-io
Copy link

codecov-io commented Apr 10, 2018

Codecov Report

Merging #60 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master      #60   +/-   ##
=======================================
  Coverage   91.51%   91.51%           
=======================================
  Files           4        4           
  Lines         283      283           
=======================================
  Hits          259      259           
  Misses         24       24
Impacted Files Coverage Δ
lib/_http_agent.js 89.85% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 82ff0e8...723bb23. Read the comment docs.

@tony-gutierrez
Copy link
Contributor

This seems like a pretty important fix for potential bugs? Why no movement here?

@fengmk2
Copy link
Member

fengmk2 commented Jul 31, 2018

can you add a test case for this fix?

@cixel
Copy link
Contributor Author

cixel commented Oct 18, 2018

Sorry for sleeping on this, I must've missed the notifications back in July. I took another look at this today to see how I could go about adding a test.

I'll admit I'm not very familiar with the code; I found this issue doing static analysis with an AST parser on a project dependent on this one.

I made the following change to see if I could better understand the effects this issue has:

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

  if (correctSockLen !== sockLen) {
	console.log(correctSockLen, sockLen, this.maxSockets);
  }

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

During testing, there's never a situation where correctSocklen !== sockLen and !(correctSockLen < this.maxSockets). This is the situation my patch would correct; however, I never see that, and I'm not sure how to cause that situation.

Any suggestions would be appreciated.

@fengmk2 fengmk2 added the bug label Oct 18, 2018
@fengmk2 fengmk2 self-assigned this Oct 18, 2018
@fengmk2
Copy link
Member

fengmk2 commented Oct 18, 2018

@cixel I see where is the bug now:

 node
> 1 + true ? 1 : 0
1
> 1 + (true ? 1 : 0)
2

@fengmk2 fengmk2 merged commit 5751fc1 into node-modules:master Oct 18, 2018
@fengmk2
Copy link
Member

fengmk2 commented Oct 18, 2018

3.5.2 released

@cixel Thanks.

@cixel
Copy link
Contributor Author

cixel commented Oct 18, 2018

Thank you! Sorry for the delayed follow-up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants