-
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.request timeout doesn't take effect #12005
Comments
Setting a timeout on the underlying socket might possibly work for you.
|
@neroux As document says: it specifies the socket timeout. Socket timeouts after a duration of inactivity on the socket. It should cover both connect timeout and read timeout. |
The term socket timeout does not specify as to what times out. In this case it refers to the connection. |
What @neroux said. To set the read timeout, use |
It makes me confusing. @lpinca gives me comments in nock/nock#754 (comment) that it checks if there is socket activity for the whole duration of the request. If it's as what @neroux said: it refers to the connection only, how to explain this code section which was committed in PR #8101 by @reneweb and PR #9440 by @italoacasas? if (req.timeout) { // req.timeout = options.timeout
const emitRequestTimeout = () => req.emit('timeout');
socket.once('timeout', emitRequestTimeout);
req.once('response', (res) => {
res.once('end', () => {
socket.removeListener('timeout', emitRequestTimeout);
});
});
}
req.emit('socket', socket); |
To make sure, I have read through the source code net.js, _http_client.js and _http_agent.js. First, when you call The |
Okay, I'll reopen. Asked the reviewers to chime in: #8101 (comment) I'll note that the pull request has some red flags (trailing whitespace, long lines) that suggest it could have been scrutinized more. |
I think this is a documentation problem more than a code problem. The PR does what is supposed to be doing. If we check https://nodejs.org/api/net.html#net_socket_settimeout_timeout_callback, the connection is not severed, but a This is what https://github.com/nodejs/node/blob/master/test/parallel/test-http-client-timeout-option.js#L25-L29 proves. |
@mcollina I'm not a native English speaker. Maybe I don't really understand this sentence:
It doesn't apear to me that after connection is established the idle timeout will be disabled. Let's confirm with code: const net = require('net');
const PORT = 5001;
const server = net.createServer( (socket) => {
console.log(Date.now(), 'SERVER connected');
setTimeout(() => { // delay 10 seconds
console.log(Date.now(), 'SERVER send data');
socket.write('hello\r\n');
}, 10000);
});
server.on('error', (err) => {
console.log(Date.now(), 'SERVER error', err);
});
server.listen(PORT);
console.log(Date.now(), 'CLIENT create connection');
const client = net.createConnection({
port: PORT,
timeout: 100
}, () => {
console.log(Date.now(), 'CLIENT connected');
});
client.on('timeout', () => {
console.log(Date.now(), 'CLIENT timeout');
process.exit();
});
client.on('data', (data) => {
console.log(Date.now(), 'CLIENT received data', data.toString());
process.exit();
}) After connection established, socket server delays 10 second to send data. Client sets timeout to 100ms and it gets timeout event.
From this test result on |
@lpinca Thanks for the explaination. However, I don't think I get an answer about whether my issue is valid or not. My test case shows that Is it an issue on Anyway, I believe when someone uses |
@aleung I think it works as expected. The 'use strict';
const http = require('http');
const server = http.createServer((req, res) => {
console.log(Date.now(), 'Server get incoming request');
setTimeout(() => {
console.log(Date.now(), 'Server send response');
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('Hello');
res.end();
}, 1000);
});
server.listen(8080, () => {
const req = http.get({ port: 8080, timeout: 100 });
req.on('response', () => { throw new Error('Test invalidation'); });
req.on('timeout', () => req.abort());
req.on('error', (err) => {
if (req.aborted) return;
console.error(err.stack);
});
}); Documentation should be updated to reflect the actual behavior though. |
Sorry everybody, I make a really stupid error in my test case: I want to listen on the @lpinca thank you very much for your kindness answer. |
Here is an working example: |
@manoharreddyporeddy in that example I see |
I don't exactly recall what I wrote at that time. However I see that I mentioned the links in the code in link mentioned, which points to nodejs github source and its tests. I recall that I tried to compare just like you did, (since we both did same, it's possible that it is correct, but I din't test it), but nodejs docs din't exactly mentioned that, at that time. So, all I can say is, |
Since node.js 6.8.0,
http.request
supportstimeout
option. (api-doc)I test this feature with below code:
Output I got:
The response was delayed 10 second to send and I expected a ETIMEOUT on http client. But it didn't.
What's more, I read the file change in #8101 which is PR adding timeout to
http.request
, the test case seems not testinghttp.request
'stimeout
option.The text was updated successfully, but these errors were encountered: