Search code examples
c++openssl

OpenSSL BIO thread safety


OpenSSL's FAQ about thread safety says the following:

Yes but with some limitations; for example, an SSL connection cannot be used concurrently by multiple threads. This is true for most OpenSSL objects.

It does not allow me to understand whether the following will be safe:

  1. I call a blocking read on a BIO
  2. If my application should be terminated before the response is received, I call close on that BIO

Solution

  • OpenSSL does not guarantee thread safety if you call 2 functions using the same BIO object at the same time from 2 different threads. However that is not what you are doing.

    You are sharing an fd between two different threads. One copy of the fd is being used by the OpenSSL library in the BIO_read() call in one thread, and you are calling close() on the same fd in a different thread.

    Sharing an fd in this way is allowed on Linux so there should be no problems in your case. I can't speak for Windows.