title: SSL_write returns SSL_ERR_SYSCALL while ERR_get_error and errno are both 0
body: <p>I'm facing a strange problem and the openssl error handling and documentation didn't help.
  I use <code>SSL_dup</code>(after an <code>SSL_new</code>), <code>SSL_set_fd</code> and <code>SSL_connect</code> to start an SSL session.
  But then when I use <code>SSL_write</code>, it returns 0. The openssl <a href="https://www.openssl.org/docs/man1.1.1/man3/SSL_write.html" rel="nofollow noreferrer">documentation</a> says to check <code>SSL_get_error</code>. I did and it gave me <code>SSL_ERROR_SYSCALL</code>. 
  The <a href="https://www.openssl.org/docs/man1.1.1/man3/SSL_get_error.html" rel="nofollow noreferrer">docs</a> also suggest checking error queue and errno. But they are both zero. Now things get intereseting. The documentation says</p>
  
  <blockquote>
    <p>SSL_ERROR_SYSCALL</p>
    
    <p>Some non-recoverable, fatal I/O error occurred. The OpenSSL error queue may contain more information on the error. For socket I/O on Unix systems, consult errno for details. If this error occurs then no further I/O operations should be performed on the connection and SSL_shutdown() must not be called.</p>
    
    <p>This value can also be returned for other errors, check the error queue for details.</p>
  </blockquote>
  
  <p>It says it's non recoverable but when I call <code>SSL_write</code> again with the same parameters, it works successfully. But obviously I cannot leave the code like this ignoring the error code of the first call. Here is what I have tried to troubleshoot the problem from what I found on the Internet. </p>
  
  <ul>
  <li>I used <code>select</code> to wait for the underlying socket to become ready for writing. </li>
  <li>I checked for EOF on <code>SSL_get_rbio</code> and <code>SSL_get_wbio</code>. <code>BIO_eof</code> returns 0 for both of them.</li>
  <li>I tried <code>SSL_do_handshake</code> before calling <code>SSL_write</code>. This may be unnecessary anyway because according the <a href="https://github.com/openssl/openssl/blob/30a4cda5e0c67b4e77da4f21b7c5f27991d3367a/ssl/ssl_lib.c#L1710" rel="nofollow noreferrer">openssl source</a> it is called when I do <code>SSL_connect</code>.</li>
  <li>I tried checking <code>SSL_is_init_finished</code> but it returns 1 and doesn't indicate an issue here.</li>
  </ul>
  
  <p>My openssl version is 1.1.1 and I'm on Ubuntu 18.04.3</p>
  
  <p>Why does this problem occurs? How can I find the reason and fix it?</p>