2

Suppose I establish ssh connection, then create several channels with libssh2_channel_open_session() - all in one main thread.

Then I create a thread for each channel, and these threads start to write and read from their channels. Each thread uses only its LIBSSH2_CHANNEL* pointer and never accesses session handle (LIBSSH2_SESSION*).

Then main thread joins all threads and close the session.

Is this safe? The only thing about thread-safety that I have found in LibSSH2 documentation is "just don't share handles simultaneously". However, I don't understand how this applies to my case. I don't share handles directly, but channel handles are likely to share session handle.

Dmitry J
  • 867
  • 7
  • 20

1 Answers1

0

No, this is not thread safe for the reason you mentioned: the channels share the session handle. Furthermore, channels are multiplexed over a single TCP connection for the session, so in a non-blocking context it is important that operations on a channel complete before an operation on another channel begins.

You probably want to protect the session with a mutex if you're planning to use any handles to a session (channel, sftp, etc) from multiple threads.

Evan
  • 603
  • 1
  • 11
  • 18