Discussion:
libssh2 thread safety
Jasmeet Bagga
2012-04-14 01:00:00 UTC
Permalink
Hi,

Is libssh2 thread safe. Compiling on Linux I don't see D_REENTRANT or
-pthreads being passed to gcc ?
Please advise.

Thanks,
Jasmeet

_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
Peter Stuge
2012-04-14 01:37:28 UTC
Permalink
Post by Jasmeet Bagga
Is libssh2 thread safe.
No.


//Peter
_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
Jasmeet Bagga
2012-04-14 02:07:31 UTC
Permalink
Post by Peter Stuge
Post by Jasmeet Bagga
Is libssh2 thread safe.
No.
Sorry, I didn't see this response before posting a follow up question.
Allow me to ask a more specific question. Is there a sane way to use
libssh2 from a multi-threaded application ? In my case all ssh sessions
are contained within a single thread. However looking at libssh2's use
of errno, I was wondering if I need to compile libssh2 with -D_REENTRANT
to be able to use it correctly here ?

Jasmeet
Post by Peter Stuge
//Peter
_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
Daniel Stenberg
2012-04-15 09:50:39 UTC
Permalink
Post by Jasmeet Bagga
Is libssh2 thread safe.
No.
I would not say it like that.

I claim libssh2 _is_ thread safe, as long as you don't share libssh2's structs
and handles between threads and assuming you use the crypto engine's mutex
callbacks properly.
--
/ daniel.haxx.se
_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
Neil Gierman
2012-04-15 14:46:55 UTC
Permalink
Post by Daniel Stenberg
Post by Jasmeet Bagga
Is libssh2 thread safe.
No.
I would not say it like that.
I claim libssh2 _is_ thread safe, as long as you don't share libssh2's
structs and handles between threads and assuming you use the crypto
engine's mutex callbacks properly.
I agree. We have been using libssh2 in a multithreaded application for a
few years. Just don't share between threads like Daniel says.
Post by Daniel Stenberg
--
/ daniel.haxx.se
_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
Jasmeet Bagga
2012-04-14 01:47:44 UTC
Permalink
Asked another way, is it safe to use libssh2 from a multi-threaded
application. From this mail thread -
http://www.libssh2.org/mail/libssh2-devel-archive-2010-08/0088.shtml -
it appears that as long as a libssh2_init is not called concurrently we
should be fine. In our application, ssh sessions are initiated only from
a single thread, is this ok ?. Do I need to recompile libssh2 with
-D_REENTRANT so that access to errno is sane ?

Thanks,
Post by Jasmeet Bagga
Hi,
Is libssh2 thread safe. Compiling on Linux I don't see D_REENTRANT or
-pthreads being passed to gcc ?
Please advise.
Thanks,
Jasmeet
_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
Peter Stuge
2012-04-14 02:09:09 UTC
Permalink
Post by Jasmeet Bagga
Asked another way, is it safe to use libssh2 from a multi-threaded
application.
This is the same question, and the answer is also the same: No.
Post by Jasmeet Bagga
In our application, ssh sessions are initiated only from a single
thread, is this ok ?
Initiating sessions is not relevant. All use of libssh2 must be
synchronized.
Post by Jasmeet Bagga
Do I need to recompile libssh2 with -D_REENTRANT so that access to
errno is sane ?
I don't think so. You need to make sure that only one thread calls
libssh2 at a time.


//Peter
_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
Jasmeet Bagga
2012-04-14 02:27:17 UTC
Permalink
Post by Peter Stuge
Post by Jasmeet Bagga
Asked another way, is it safe to use libssh2 from a multi-threaded
application.
This is the same question, and the answer is also the same: No.
Post by Jasmeet Bagga
In our application, ssh sessions are initiated only from a single
thread, is this ok ?
Initiating sessions is not relevant. All use of libssh2 must be
synchronized.
s/initiated/contained/ i.e. all sessions are contained within a thread.
The other thread does not do anything with libssh2.
Post by Peter Stuge
Post by Jasmeet Bagga
Do I need to recompile libssh2 with -D_REENTRANT so that access to
errno is sane ?
I don't think so. You need to make sure that only one thread calls
libssh2 at a time.
Only one thread does. However I am getting login failures with error
message "Failed getting banner". Looking at another mail thread
(http://www.libssh2.org/mail/libssh2-devel-archive-2010-06/0243.shtml)
it seems the problem went away after compiling with -D_REENTRANT. I
tried this but it didn't help. Unfortunately I can't access the
associated ticket as libssh2 trac is down.

Jasmeet
Post by Peter Stuge
//Peter
_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
Peter Stuge
2012-04-14 02:45:54 UTC
Permalink
Post by Jasmeet Bagga
Post by Peter Stuge
Initiating sessions is not relevant. All use of libssh2 must be
synchronized.
s/initiated/contained/ i.e. all sessions are contained within a
thread. The other thread does not do anything with libssh2.
Sounds good.
Post by Jasmeet Bagga
Only one thread does. However I am getting login failures with
error message "Failed getting banner".
You need to provide COMPLETE details of your problem. I suggest
enabling tracing and sending the full log to the list.
Post by Jasmeet Bagga
it seems the problem went away after compiling with -D_REENTRANT. I
tried this but it didn't help. Unfortunately I can't access the
associated ticket as libssh2 trac is down.
Try now. http://trac.libssh2.org/ticket/99


//Peter
_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
Jasmeet Bagga
2012-04-16 19:03:33 UTC
Permalink
Post by Peter Stuge
Post by Jasmeet Bagga
Only one thread does. However I am getting login failures with
error message "Failed getting banner".
You need to provide COMPLETE details of your problem. I suggest
enabling tracing and sending the full log to the list.
The log is attached. I did further debugging and found that ssh via open
ssh succeeds, however when I enabled verbose option on it, I see some
lines that are interesting
DUKEOSTPDZ01:/var/tmp/jasmeet#ssh -v packet-design-***@public.gmane.org
OpenSSH_4.3p2, OpenSSL 0.9.8e-fips-rhel5 01 Jul 2008
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
debug1: Connecting to 172.17.248.203 [172.17.248.203] port 22.
debug1: Connection established.
debug1: permanently_set_uid: 0/0
debug1: identity file /root/.ssh/identity type -1
debug1: identity file /root/.ssh/id_rsa type -1
debug1: identity file /root/.ssh/id_dsa type -1
debug1: Remote protocol version 1.99, remote software version
OpenSSH_4.4 ---> Remote version is 1.99
debug1: match: OpenSSH_4.4 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_4.3

This shows that the remote protocol version is 1.99. Does libssh2 work
with that ? I would assume no, but the folks using this device claim
that it used to work and then suddenly started failing without any
change to the device.

Thanks,
Jasmeet
Post by Peter Stuge
Post by Jasmeet Bagga
it seems the problem went away after compiling with -D_REENTRANT. I
tried this but it didn't help. Unfortunately I can't access the
associated ticket as libssh2 trac is down.
Try now. http://trac.libssh2.org/ticket/99
//Peter
_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
Peter Stuge
2012-04-16 19:10:16 UTC
Permalink
Post by Jasmeet Bagga
This shows that the remote protocol version is 1.99. Does libssh2
work with that ?
As the RFC specifies, 1.99 means that the server can do both 1.0
and 2.0 protocols. Since you have a product based on SSH you really
should take a few hours to study the protocol RFCs. They are very
simple.
Post by Jasmeet Bagga
[libssh2] 0.956528 Conn: Setting blocking mode OFF
[libssh2] 0.956546 Transport: session_startup for socket 13
[libssh2] 0.956554 Transport: Sending Banner: SSH-2.0-libssh2_1.4.1
[libssh2] 0.956561 Socket: Sent 23/23 bytes at 0xbb9345+0
[libssh2] 0.956992 Socket: Recved 0 bytes banner
[libssh2] 0.956996 Failure Event: -43 - Failed getting banner
[libssh2] 0.957045 Transport: Disconnecting: reason=11, desc=, lang=
=> libssh2_transport_write plain (13 bytes)
The log shows that the problem is very very early. The problem is not
likely with libssh2 at all but in your application, and the root
problem will be something you do or do not do to the socket between
start of your program and calling libssh2_session_handshake().


//Peter
_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
Jasmeet Bagga
2012-04-17 02:06:30 UTC
Permalink
Post by Peter Stuge
Post by Jasmeet Bagga
[libssh2] 0.956528 Conn: Setting blocking mode OFF
[libssh2] 0.956546 Transport: session_startup for socket 13
[libssh2] 0.956554 Transport: Sending Banner: SSH-2.0-libssh2_1.4.1
[libssh2] 0.956561 Socket: Sent 23/23 bytes at 0xbb9345+0
[libssh2] 0.956992 Socket: Recved 0 bytes banner
[libssh2] 0.956996 Failure Event: -43 - Failed getting banner
[libssh2] 0.957045 Transport: Disconnecting: reason=11, desc=, lang=
=> libssh2_transport_write plain (13 bytes)
The log shows that the problem is very very early. The problem is not
likely with libssh2 at all but in your application, and the root
problem will be something you do or do not do to the socket between
start of your program and calling libssh2_session_handshake().
JFYI, the problem was that the remote device has a very low rate limit for ssh connections and it was abruptly terminating ssh connections (after the TCP connection succeeded), leading to the above seen errors.
Post by Peter Stuge
//Peter
_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
Peter Stuge
2012-04-17 03:00:10 UTC
Permalink
Post by Jasmeet Bagga
Post by Peter Stuge
Post by Jasmeet Bagga
[libssh2] 0.956992 Socket: Recved 0 bytes banner
[libssh2] 0.956996 Failure Event: -43 - Failed getting banner
[libssh2] 0.957045 Transport: Disconnecting: reason=11, desc=, lang=
The log shows that the problem is very very early.
JFYI, the problem was that the remote device has a very low rate
limit for ssh connections and it was abruptly terminating ssh
connections (after the TCP connection succeeded), leading to the
above seen errors.
Sure.

So one thing that could be improved about libssh2 here is the
reported error. Currently you get LIBSSH2_ERROR_SOCKET_RECV but it
could be made more clear that the connection was unexpectedly closed.

Do you have some suggestion for a more useful error code?


//Peter
_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
Peter Stuge
2012-04-17 03:06:57 UTC
Permalink
Post by Peter Stuge
Post by Jasmeet Bagga
Post by Peter Stuge
Post by Jasmeet Bagga
[libssh2] 0.956992 Socket: Recved 0 bytes banner
[libssh2] 0.956996 Failure Event: -43 - Failed getting banner
[libssh2] 0.957045 Transport: Disconnecting: reason=11, desc=, lang=
The log shows that the problem is very very early.
JFYI, the problem was that the remote device has a very low rate
limit for ssh connections and it was abruptly terminating ssh
connections (after the TCP connection succeeded), leading to the
above seen errors.
Sure.
So one thing that could be improved about libssh2 here is the
reported error. Currently you get LIBSSH2_ERROR_SOCKET_RECV but it
could be made more clear that the connection was unexpectedly closed.
Do you have some suggestion for a more useful error code?
How about the attached?


//Peter
Jasmeet Bagga
2012-04-17 04:04:04 UTC
Permalink
----- Original Message -----
From: "Peter Stuge" <peter-Y+***@public.gmane.org>
To: libssh2-devel-***@public.gmane.org
Sent: Monday, April 16, 2012 8:06:57 PM
Subject: Re: libssh2 thread safety
Post by Peter Stuge
Post by Jasmeet Bagga
Post by Peter Stuge
Post by Jasmeet Bagga
[libssh2] 0.956992 Socket: Recved 0 bytes banner
[libssh2] 0.956996 Failure Event: -43 - Failed getting banner
[libssh2] 0.957045 Transport: Disconnecting: reason=11, desc=, lang=
The log shows that the problem is very very early.
JFYI, the problem was that the remote device has a very low rate
limit for ssh connections and it was abruptly terminating ssh
connections (after the TCP connection succeeded), leading to the
above seen errors.
Sure.
So one thing that could be improved about libssh2 here is the
reported error. Currently you get LIBSSH2_ERROR_SOCKET_RECV but it
could be made more clear that the connection was unexpectedly closed.
Do you have some suggestion for a more useful error code?
How about the attached?

I think this is better


//Peter

_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
Jasmeet Bagga
2012-04-16 19:08:08 UTC
Permalink
Post by Peter Stuge
Try now. http://trac.libssh2.org/ticket/99
This is down again. I tried on Friday after your mail, it was up then
but now its failing again.


Jasmeet
Post by Peter Stuge
//Peter
_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
Peter Stuge
2012-04-16 19:14:06 UTC
Permalink
Post by Jasmeet Bagga
Post by Peter Stuge
Try now. http://trac.libssh2.org/ticket/99
This is down again. I tried on Friday after your mail, it was up
then but now its failing again.
Reload and it should be there.


//Peter
_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
Loading...