When a socket timeouts via socket.setTimeout(), it is automatically destroyed, even though the documentation states that it should not be.
When an idle timeout is triggered the socket will receive a 'timeout' event but the connection will not be severed. The user must manually call socket.end() or socket.destroy() to end the connection.
Code to reproduce:
const http = require('http');
const server = http.createServer(function (req, res) {
});
server.on("connection", socket => {
console.log('on connection');
socket.setTimeout(2000, () => {
console.log('on socket timeout', socket.destroyed);
});
});
server.listen(3000);
The above comment is incorrect. This has nothing to do with the timers subsystem itself.
@underflow00 The issue is you are looking at the documentation for net. The http module overloads this behavior (by default).
This behavior is documented in http.server.setTimeout([msecs][, callback]), although it could perhaps be more clear. E.g. it could say that destroy() will be called on the socket.
I think It is very clear: ..., and sockets are destroyed automatically if they time out.; personally I would not change that part, I think that connection event docs is not very precise, docs says is typically an object of type net.Socket, so you'll jump to read net.Socket docs instead of http docs.
Let me know what do you thing
...and sockets are destroyed automatically if they time out.;
Actually I don't think that's strictly true, I think its more like:
...and sockets are destroyed automatically if they timeout and there is no
'timeout'listener on either the request, response nor server object
We should probably update that.
See, https://github.com/nodejs/node/blob/master/lib/_http_server.js#L470
@juanarbol: Actually that paragraph was recently changed. No update required.
@ronag Yes, sorry, I really did a mess by misunderstanding this issue; I'll be more careful by reading docs (maybe making quotes of MD files), and making traces of code reproductions before making a comment.
Thank you! I learned a lot.
The most important documentation change has got to be the one on socket.setTimeout(), especially since server.setTimeout() is automatically called under the hood and users may not even come across that method.
Perhaps mention something like 'if the socket is created by an external component (e.g. http.Server) then it may be automatically destroyed by that component'.
Most helpful comment
The above comment is incorrect. This has nothing to do with the timers subsystem itself.
@underflow00 The issue is you are looking at the documentation for
net. Thehttpmodule overloads this behavior (by default).This behavior is documented in
http.server.setTimeout([msecs][, callback]), although it could perhaps be more clear. E.g. it could say thatdestroy()will be called on thesocket.