title: How to manage the error queue in openssl (SSL_get_error and ERR_get_error)
body: <p>In OpenSSl, The man pages for The majority of SSL_* calls indicate an error by returning a value &lt;= 0 and suggest calling SSL_get_error() to get the extended error.</p>
  
  <p>But within the man pages for these calls as well as for other OpenSSL library calls, there are vague references to using the "error queue" in OpenSSL - Such is the case in the man page for <a href="https://www.openssl.org/docs/ssl/SSL_get_error.html#DESCRIPTION">SSL_get_error</a>:</p>
  
  <pre><code>   The current thread's error queue must be empty before the TLS/SSL I/O
     operation is attempted, or  SSL_get_error() will not work reliably.
  </code></pre>
  
  <p>And in that very same man page, the description for SSL_ERROR_SSL says this:</p>
  
  <pre><code>   SSL_ERROR_SSL
         A failure in the SSL library occurred, usually a protocol error.
         The OpenSSL error queue contains more information on the error.
  </code></pre>
  
  <p>This kind of implies that there is something in the error queue worth reading.  And failure to read it makes a subsequent call to SSL_get_error unreliable.  Presumably, the call to make is <a href="https://www.openssl.org/docs/crypto/ERR_get_error.html">ERR_get_error</a>.</p>
  
  <p>I plan to use non-blocking sockets in my code. As such, it's important that I reliably discover when the error condition is SSL_ERROR_WANT_READ or SSL_ERROR_WANT_WRITE so I can put the socket in the correct polling mode.</p>
  
  <p>So my questions are this:</p>
  
  <ul>
  <li><p>Does SSL_get_error() call ERR_get_error() implicitly for me?  Or do I need to use both?</p></li>
  <li><p>Should I be calling <a href="https://www.openssl.org/docs/crypto/ERR_clear_error.html">ERR_clear_error</a> prior to every OpenSSL library call?</p></li>
  <li><p>Is it possible that more than one error could be in the queue after an OpenSSL library call completes?  Hence, are there circumstances where the first error in the queue is more relevant than the last error?</p></li>
  </ul>