Subject: Re: Thread safety issues

Re: Thread safety issues

From: Simon Josefsson <>
Date: Tue, 31 Aug 2010 22:54:44 +0200

jmk <> writes:

> On Tue, 2010-08-31 at 21:54 +0200, Simon Josefsson wrote:
>> jmk <> writes:
>> > On Tue, 2010-08-31 at 09:53 +0200, Simon Josefsson wrote:
>> >> You shouldn't need any mutexes except the ones needed for the crypto
>> >> library. Maybe you could remove your own mutex and see if things
>> >> continue to work?
>> >
>> > It's certainly possible I've messed stuff up, but without the mutex for
>> > libssh2_session_init_ex(), things still occasionally blow up. With a
>> > mutex for that one call and the crypto callback functions, things *seem*
>> > completely stable.
>> Do you call libssh2_init? The libssh2_session_init_ex function
>> initializes the library when needed, maybe you cause two threads to
>> initialize the library at the same time. By doing an explicit
>> libssh2_init at the start of your program, that should be avoided.
>> It may also be that I've missed something, and libssh2_session_init_ex
>> is intended to be thread-unsafe too. But then we have a documentation
>> problem, since this is not stated in the function man page.
> I wasn't calling libssh2_init(). I guess I misread a previous statement
> about libssh2_init() being non-thread safe as libssh2_session_init().
> Adding a call to libssh2_init() with a mutex and removing the mutex for
> libssh2_session_init_ex() seems to work just fine, so
> libssh2_session_init_ex should indeed be thread-safe. For my particular
> use, it probably doesn't matter much and both approaches would work.

It may be simpler to call libssh2_init globally before firing up any
threads. Then you only need to supply mutexes to the crypto library.

Received on 2010-08-31