-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
http: remove stale timeout listeners #9440
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
65790af
http: remove stale timeout listeners
5c37b4a
replace catch with unhandledRejection-listener
274cb28
use common.localhostIPv4
c32ee77
Increase timeout, output rejection reason
2c4ebae
use common.platformTimeout
fa6ec18
early exit on rejection
516ba6f
use EventEmitter#once
60ff0fb
test in callback-style
3039b1d
use socket.listeners, add common.mustCall
6cc3f0b
add common.mustCall
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
test/parallel/test-http-client-timeout-option-listeners.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const http = require('http'); | ||
const assert = require('assert'); | ||
|
||
const agent = new http.Agent({ keepAlive: true }); | ||
|
||
const server = http.createServer((req, res) => { | ||
res.end(''); | ||
}); | ||
|
||
const options = { | ||
agent, | ||
method: 'GET', | ||
port: undefined, | ||
host: common.localhostIPv4, | ||
path: '/', | ||
timeout: common.platformTimeout(100) | ||
}; | ||
|
||
server.listen(0, options.host, common.mustCall(() => { | ||
options.port = server.address().port; | ||
doRequest(common.mustCall((numListeners) => { | ||
assert.strictEqual(numListeners, 1); | ||
doRequest(common.mustCall((numListeners) => { | ||
assert.strictEqual(numListeners, 1); | ||
server.close(); | ||
agent.destroy(); | ||
})); | ||
})); | ||
})); | ||
|
||
function doRequest(cb) { | ||
http.request(options, common.mustCall((response) => { | ||
const sockets = agent.sockets[`${options.host}:${options.port}:`]; | ||
assert.strictEqual(sockets.length, 1); | ||
const socket = sockets[0]; | ||
const numListeners = socket.listeners('timeout').length; | ||
response.resume(); | ||
response.once('end', common.mustCall(() => { | ||
process.nextTick(cb, numListeners); | ||
})); | ||
})).end(); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd write the test in simple callback style. Less chance of accidentally swallowing errors and easier to debug because you can simply |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not to derail this PR which is desperately needed, but isn't this section very similar to the existing
ClientRequest.prototype.setTimeout
method? Would just callingreq.setTimeout(req.timeout)
at this point achieve almost the same behavior?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought along the same lines when I found this bug. The code implementing this feature didn't look like I expected, but I just wanted to make a small, conceptually simple fix to get it merged as quickly as possible.
It think your suggestion would work. What does @evanlucas say?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I reused your unit test code and took a stab at the approach described above in #9831
The primary difference is that any timeout set applies from the time a socket is assigned to a request, i.e. it includes socket pre-connect timeouts. In existing code, if a timeout is set via
ClientRequest.prototype.setTimeout
, it is "reapplied" and starts counting only from the time the socket connects. Perhaps this would count as an API change.In other HTTP libraries, the connect-timeout and the transaction-timeout are two separate parameters, e.g. in curl https://curl.haxx.se/libcurl/c/CURLOPT_CONNECTTIMEOUT.html and https://curl.haxx.se/libcurl/c/CURLOPT_TIMEOUT.html