Node: `socket.setTimeout` destroys connection automatically

Created on 21 Aug 2019  路  7Comments  路  Source: nodejs/node

  • Version: v10.15.3
  • Platform: Windows 10 Pro 64-bit

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);
doc http question

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. 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.

All 7 comments

I think you're right, that is happening right here, but that was called while "enrolling" that timer here

I understand and could fix this specific issue, but I do not know why _destroyed is set true when unenrrolling' a timer

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'.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

srl295 picture srl295  路  3Comments

fanjunzhi picture fanjunzhi  路  3Comments

Brekmister picture Brekmister  路  3Comments

jmichae3 picture jmichae3  路  3Comments

stevenvachon picture stevenvachon  路  3Comments