http-shutdown icon indicating copy to clipboard operation
http-shutdown copied to clipboard

_isIdle is not updated properly

Open slavafomin opened this issue 7 years ago • 0 comments

Hello!

I'm trying to use this module with Express, but I'm getting some weird behavior with this peace of code:

server.on('request', function(req, res) {
  req.socket._isIdle = false;

  res.on('finish', function() {
    req.socket._isIdle = true;
    destroy(req.socket);
  });
});

For some reason, req.socket._isIdle = false; takes no effect, because when I examine req.socket._isIdle in res.on('finish') function I can see it's still set to true.

But, the real problem happens here:

function destroy(socket, force) {
  if (force || (socket._isIdle && isShuttingDown)) {
    socket.destroy();
    delete connections[socket._connectionId];
  }
};

Because socket._isIdle has a incorrect value of false and the socket is not destroyed when shutting down.

This implementation helped me to fix the problem:

server.on('request', function(req, res) {
  var socket = req.socket;
  socket._isIdle = false;

  res.on('finish', function() {
    socket._isIdle = true;
    destroy(socket);
  });
});

Do you have ideas why is this happening? It looks like req.socket references different objects, but it just makes no sense.

slavafomin avatar May 31 '17 00:05 slavafomin