Subject: PATCH - libssh2 disconnection

PATCH - libssh2 disconnection

From: Maxime Larocque <>
Date: Tue, 10 Jul 2012 10:56:29 -0400


We found a problem with the disconnect process of libssh2. When used
with Apache Mina SSHd server it aborts the connection too fast, causing
exceptions in the SSHd server.

An OpenSSH client connection to Apache Mina SSHd does not cause the
problem. A libssh2 connection to an OpenSSH server does not cause any
logs in the server.

It seems that the libssh2 server does not wait for all the messages to
be received before closing the connection, once the first message is
received ( SSH_MSG_CHANNEL_EOF), it aborts the connection instead of

It was tested with libssh2 1.4.2 (which is not yet a valid version in Trac).

The following patch resolves the problem on our system:

--- libssh2-1.4.2/src/channel.c 2012-07-03 14:41:27.010562955 -0400
+++ libssh2-1.4.2/src/channel2.c 2012-07-03 14:41:19.098562719 -0400
@@ -2295,7 +2295,7 @@
      if (channel->close_state == libssh2_NB_state_sent) {
          /* We must wait for the remote SSH_MSG_CHANNEL_CLOSE message */

- while (!channel->remote.close && !rc &&
+ while (!channel->remote.close && (rc >= 0) &&
                 (session->socket_state != LIBSSH2_SOCKET_DISCONNECTED))
              rc = _libssh2_transport_read(session);


_libssh2_transport_read can return:
- a negative value on error
- the message type on success

It should stay in the while loop as long as there are messages
processed; rc>=0 is then required instead of !rc.

I tried to add it to Trac, but it said a message about being routed to
moderation; I am not sure if it has been accepted.


Maxime Larocque
Orthogone Technologies
Currently under contract for Accedian Networks

Received on 2012-07-10