Subject: Re: Trying to understand keepalive

Re: Trying to understand keepalive

From: Fritz Elfert <>
Date: Sun, 25 Dec 2011 14:48:12 +0100

On 12/25/2011 12:58 PM, Henrik Nordström wrote:
> sön 2011-12-25 klockan 03:09 +0100 skrev Fritz Elfert:
>> Studying the source of libssh2 (current git), I noticed, that while
>> libssh2 supports sending keepalive messages as well as replying to
>> keepalives sent from the server, it apparently does *not* act on the
>> responses from the server even when want_reply is set while invoking
>> libssh2_keepalive_send().
> The keep-alive implementation in libssh2 is very rudimentary, and not
> without problems. Currently it helps keeping an idle connection alive,
> but not good for detecting communication issues, and may even cause
> additional communication issues if EAGAIN is seen when sending the
> keep-alive message.

EAGAIN is silently ignored in libssh2_keepalive_send(). Isn't that correct?

>> So am I missing something here?
>> Or: How am I (as a user of libssh2) supposed to find out, if the server
>> has properly responded to the keepalive that I just sent?
> From what I can tell you can't.
> There really should be timestamps recording time of last sent, last sent
> with want_reply set and last received packet, and keepalive adjusted to
> monitor these both for determining if keep-alive is needed and for
> detecting session timeout. Technically just one of the last sent
> timestamps is sufficient, but to keep the logics of which time to track
> for keep-alive isolated within keepalive it's easier if both are
> recorded in the session.

I see.
Well if you take a look at openssh, they simply maintain a counter
(server_alive_timeouts) which gets incremented when sending a keepalive
and reset to 0 whenever a SSH_MSG_REQUEST_SUCCESS or
SSH_MSG_REQUEST_FAILURE is received. Before sending the keepalive
request, they check this counter against their configurable
server_alive_count_max. Also: They *always* set want_reply to 1 -
Wouldn't make much sense otherwise.

From that, my understanding is that the current implementation of
libssh2 (regrding maintaining a single timestamp of last sent) is
sufficient. The only thing that's missing is the timeouts-counter. Of
course - since want_reply is optional in libssh2 - the counter should
only be incremented if want_reply is set.

> It's not really important to track if the received (or sent) packets are
> in reply to keep-alive or not. It's sufficient to note that there is
> traffic.



> I don't know why ticket #183 was closed (Daniel?). Does not look
> outdated to me. May even work to some degree. But needs to be improved
> slightly to handle want_reply == 0 without timing out the session, and
> do not solve a couple of other keep-alive issues.
> Regards
> Henrik
> _______________________________________________
> libssh2-devel

Received on 2011-12-25