Subject: Re: multithread calls libssh2_session_handshake,error,return -13

Re: multithread calls libssh2_session_handshake,error,return -13

From: Alfred Gebert <>
Date: Thu, 13 Feb 2014 09:14:03 +0100

On Wed, Feb 12, 2014 at 4:10 AM, xingzhiming <> wrote:
> hi,all
> When using multiple threads, in libssh2_session_handshake abnormal.
> If you do not multi-threaded, no problem. How to use multi-thread calls libssh2?

I had a similar problem. Perhaps my solution can you help as well.

If I created a session I got the error:
  Unable to exchange encryption keys

I enabled verbose logging and did run the application with truss
(similar to strace on Linux) on Solaris.

  /13: recv(11, 0xFFFFFD7FFBA05B17, 1, 0) Err#11 EAGAIN
  _libssh2_recv: rc=-1 errno=125
  [libssh2] 0.758411 Socket: Error recving 1 bytes: 125

On the first line above a recv() system call returned with -1 and
errno was 11 (EAGAIN). But on the second and third line errno was 125.
The second line is a debug output which I added to the source directly
after the recv() statement.

The source of the problem was that I didn't compile libssh2 with
correct compiler flags for multi-threading. If you do not pay
attention on Solaris then errno is a global variable. Using gcc
compiler you can add "-pthread" as compile and link flag to enable
thread local storage of errno. Using -D_REENTRANT on Solaris as
compile flag also works. My configure call is now is e.g.
  CFLAGS="-m64 -pthread" LDFLAGS="-m64 -pthread" ./configure
--with-openssl --with-libssl-prefix=$HOME/local --with-libz
--with-libz-prefix=$HOME/local --prefix=$HOME/local
Received on 2014-02-13