Ticket #99 (closed defect)
Opened 3 years ago
Last modified 3 years ago
libssh2 library is not thread safe
| Reported by: | anonymous | Owned by: | bagder |
|---|---|---|---|
| Priority: | normal | Milestone: | |
| Component: | misc | Version: | |
| Keywords: | Cc: | bagder, p82420 | |
| Blocked By: | Blocks: |
Description
By default, there is no "-D_REENTRANT" flag passed to the C preprocessor (nor any -mt (Sun Studio) or -pthreads (gcc) option to the C compiler).
Note: I'm using libssh2 on Solaris 9/10, using Sun Studio 12.
Referencing errno in many pieces of code makes the whole library not thread safe (libssh2_session_startup() for example). There are many EAGAIN errors that are now thrown internally (because of new non-blocking sockets in 1.1), but unfortunately, the errno value is not correctly accessed, leading to undefined behaviour. This can be "easily" reproduced using something like (compiled in a thread safe manner):
Th.1: set errno
Th.1: detach thread 2
Th.2: establish an SSH connection. libssh2_session_startup() will usually fail.
I've passed CPPFLAGS="-D_REENTRANT" to the configure script, and things seem now to be OK.
Miscellaneous defects of the "configure" script:
- Specifying LDFLAGS="-mt -pthreads" does not pass these options to the ld linker.
- Specifying --disable-debug still compiles with "-g".
Change History
comment:1 Changed 3 years ago by bagder
comment:2 Changed 3 years ago by p82420
Hello,
Sorry, but I'm not really confident with configure.in, m4 macros and all that stuff.
comment:3 Changed 3 years ago by bagder
I've now committed a fix for this in the git repo. It'd be great if you tried it.
The fix will be included in the June 5 and later snapshots from => http://libssh2.haxx.se/snapshots.html
comment:4 Changed 3 years ago by bagder
Thanks for your report and help in improving libssh2!
A fix has just been committed to the source code repo, this case is now considered fixed and closed!

The configure script obviously needs to check for _REENTRANT. I'm confident we can get some inspiration and code from here:
http://curl.haxx.se/lxr/source/m4/curl-reentrant.m4#L390
You able to provide a patch?