Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

net/tls KeepAlive does not work as expected #6194

Closed
FGRibreau opened this issue Sep 7, 2013 · 2 comments
Closed

net/tls KeepAlive does not work as expected #6194

FGRibreau opened this issue Sep 7, 2013 · 2 comments

Comments

@FGRibreau
Copy link

Quoted from TCP keepalive HowTo:

In order to understand what TCP keepalive (which we will just call keepalive) does, you need do nothing more than read the name: keep TCP alive. This means that you will be able to check your connected socket (also known as TCP sockets), and determine whether the connection is still up and running or if it has broken.

So the following code SHOULD throw something when the internet connection is broken:

var tls = require('tls');

var socket = tls.connect(443, "google.com", function connected() {
  console.log('connected');
});

socket.setNoDelay(true);
socket.setKeepAlive(true, 0);
socket.setTimeout(0, function(){
  console.log('timeout');
});
socket.on('data', function(data) {
  console.log(data);
});
socket.on('close', function() {
  console.error("close");
});
socket.on('error', function(err) {
  console.error("error", err);
});

Tested on MacOS/Debian, with NodeJS v0.10.17

@bnoordhuis
Copy link
Member

It works as expected - though maybe not as you expect it to. :-)

Quoting man 7 tcp:

tcp_keepalive_time (integer; default: 7200; since Linux 2.2)

The number of seconds a connection needs to be idle before TCP begins sending out keep-alive probes. Keep-alives are only sent when the SO_KEEPALIVE socket option is enabled. The default value is 7200 seconds (2 hours). An idle connection is terminated after approximately an additional 11 minutes (9 probes an interval of 75 seconds apart) when keep-alive is enabled.

That is, TCP keep-alive is basically a timer that, once it expires, tells you the connection is dead.

Node.js could perhaps make keep-alive a little more configurable. Linux supports TCP_KEEPCNT, TCP_KEEPIDLE and TCP_KEEPINTVL, the BSDs have TCP_KEEPALIVE, Solaris has TCP_KEEPALIVE, TCP_KEEPALIVE_THRESHOLD and TCP_KEEPALIVE_ABORT_THRESHOLD, Windows has... well, who knows?

Still, that won't let you magically detect a severed connection at the time of the disconnect itself because that's simply not how TCP works.

@FGRibreau
Copy link
Author

That is, TCP keep-alive is basically a timer that, once it expires, tells you the connection is dead.

Ok, so indeed, after ~10 minutes, Node emits an error:
`error { [Error: read ETIMEDOUT] code: 'ETIMEDOUT', errno: 'ETIMEDOUT', syscall: 'read' }``

Thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants