Title: ssl.SSLSocket.read() / write() missing ERR_clear_error() before SSL_read_ex() / SSL_write_ex() causes spurious errors with cooperative threading · Issue #148594 · python/cpython · GitHub
Open Graph Title: ssl.SSLSocket.read() / write() missing ERR_clear_error() before SSL_read_ex() / SSL_write_ex() causes spurious errors with cooperative threading · Issue #148594 · python/cpython
X Title: ssl.SSLSocket.read() / write() missing ERR_clear_error() before SSL_read_ex() / SSL_write_ex() causes spurious errors with cooperative threading · Issue #148594 · python/cpython
Description: Bug report Bug description: Summary _ssl__SSLSocket_read_impl and _ssl__SSLSocket_write_impl in Modules/_ssl.c do not call ERR_clear_error() before SSL_read_ex() / SSL_write_ex(). This allows stale entries on the per-thread OpenSSL error...
Open Graph Description: Bug report Bug description: Summary _ssl__SSLSocket_read_impl and _ssl__SSLSocket_write_impl in Modules/_ssl.c do not call ERR_clear_error() before SSL_read_ex() / SSL_write_ex(). This allows stale...
X Description: Bug report Bug description: Summary _ssl__SSLSocket_read_impl and _ssl__SSLSocket_write_impl in Modules/_ssl.c do not call ERR_clear_error() before SSL_read_ex() / SSL_write_ex(). This allows stale...
Opengraph URL: https://github.com/python/cpython/issues/148594
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"ssl.SSLSocket.read() / write() missing ERR_clear_error() before SSL_read_ex() / SSL_write_ex() causes spurious errors with cooperative threading","articleBody":"# Bug report\n\n### Bug description:\n\n### Summary\n\n`_ssl__SSLSocket_read_impl` and `_ssl__SSLSocket_write_impl` in `Modules/_ssl.c` do not call `ERR_clear_error()` before `SSL_read_ex()` / `SSL_write_ex()`. This allows stale entries on the per-thread OpenSSL error queue to corrupt the result of `SSL_get_error()`, causing spurious `BrokenPipeError` or `OSError` on healthy SSL connections.\n\n### Affected versions\n\nAll current CPython versions. Confirmed in `3.12` branch (line 2544) and `main` / 3.15-dev (line 2941).\n\n### Root cause\n\nThe `do { ... } while()` retry loop in `_ssl__SSLSocket_read_impl` ([`Modules/_ssl.c` L2939-2942 on main](https://github.com/python/cpython/blob/main/Modules/_ssl.c#L2939-L2942)):\n\n```c\ndo {\n Py_BEGIN_ALLOW_THREADS;\n retval = SSL_read_ex(self->ssl, mem, (size_t)len, &count);\n err = _PySSL_errno(retval == 0, self->ssl, retval);\n Py_END_ALLOW_THREADS;\n // ...\n} while (err.ssl == SSL_ERROR_WANT_READ || err.ssl == SSL_ERROR_WANT_WRITE);\n```\n\n`_PySSL_errno()` calls `SSL_get_error(ssl, retcode)`, which internally calls `ERR_peek_last_error()`. Per the [OpenSSL documentation](https://www.openssl.org/docs/man3.0/man3/SSL_get_error.html):\n\n> In addition to `ssl` and `ret`, **`SSL_get_error()` inspects the current thread's OpenSSL error queue**. Thus, `SSL_get_error()` must be called in the same thread that performed the TLS/SSL I/O operation, and **no other OpenSSL function calls should appear in between**. 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.\n\nIf stale error entries are present on the queue from a prior SSL operation (on the same thread but a different SSL object), `SSL_get_error()` misattributes them and returns `SSL_ERROR_SYSCALL` instead of the correct `SSL_ERROR_WANT_READ`.\n\nThe same issue exists in `_ssl__SSLSocket_write_impl`.\n\n### When this manifests\n\nThis bug is invisible in multi-threaded programs because each OS thread has its own OpenSSL error queue. It becomes **critical** in cooperative multitasking frameworks (**gevent**, **eventlet**, **asyncio** with SSL) where multiple coroutines/greenlets share a single OS thread and thus a single OpenSSL error queue.\n\n**Concrete scenario** (gevent):\n1. Greenlet A performs an SSL write on an HTTPS connection. The remote client has disconnected, so `SSL_write_ex()` → `send()` fails with `EPIPE`. OpenSSL pushes an error entry onto the (per-thread) error queue. The greenlet handles the exception, but the error queue is **not cleared**.\n2. The gevent hub switches to Greenlet B, which is an AMQP consumer doing `SSL_read_ex()` on a healthy RabbitMQ connection.\n3. `SSL_read_ex()` → `recv()` returns `EAGAIN` (no data available — normal for a non-blocking socket).\n4. `SSL_get_error()` finds the stale error from step 1 via `ERR_peek_last_error()` and returns `SSL_ERROR_SYSCALL` instead of `SSL_ERROR_WANT_READ`.\n5. `_PySSL_errno()` captures `errno = 32` (stale EPIPE from step 1).\n6. CPython exits the retry loop, enters `PySSL_SetError()`, and raises `BrokenPipeError(errno=32, \"Broken pipe\")` on a **perfectly healthy** connection.\n\n### Evidence\n\n- **Disassembly**: The compiled `_ssl.cpython-312-x86_64-linux-gnu.so` confirms no `ERR_clear_error` (PLT `0x9050`) before `SSL_read_ex` (PLT `0x93b0`) at the call site.\n- **Production telemetry**: At the moment of every `BrokenPipeError`, `getsockopt(SO_ERROR)` returns 0 (no kernel-level error), and `tcpdump` shows no FIN/RST from the remote side — the TCP connection is healthy.\n- **Workaround validation**: Calling `ERR_clear_error()` (via ctypes) before every `_sslobj.read()` in a monkey-patched `ssl.SSLSocket.read()` **completely eliminates** the spurious errors. Tested for 15+ minutes under production load with zero errors, after months of constant failures every ~90 seconds.\n\n### Proposed fix\n\nAdd `ERR_clear_error()` before `SSL_read_ex()` and `SSL_write_ex()` in their respective retry loops:\n\n```c\ndo {\n Py_BEGIN_ALLOW_THREADS;\n ERR_clear_error(); /* Prevent stale errors from affecting SSL_get_error() */\n retval = SSL_read_ex(self->ssl, mem, (size_t)len, &count);\n err = _PySSL_errno(retval == 0, self->ssl, retval);\n Py_END_ALLOW_THREADS;\n // ...\n```\n\nThis matches OpenSSL's documented requirement and is consistent with how CPython already calls `ERR_clear_error()` in other SSL functions (e.g., `_ssl__SSLSocket_do_handshake_impl`, `_ssl_ctx_new`).\n\n### Related\n\n- gh-115627 (commit ea9a296fce2) — Improved `PySSL_SetError` handling for `SSL_ERROR_SYSCALL`, but only in `main`; does **not** add `ERR_clear_error()` before read/write calls.\n- gh-127257 (commit 7f707fa6c67) — `ERR_LIB_SYS` handling improvement, backported to 3.12; does not address this issue.\n\n### Reproducer\n\nA minimal reproducer requires two SSL connections on the same OS thread. In pseudocode:\n\n```python\nimport ssl, socket, gevent\n\ndef writer_greenlet():\n \"\"\"SSL connection that will fail, leaving stale error on queue\"\"\"\n ctx = ssl.create_default_context()\n sock = ctx.wrap_socket(socket.socket(), server_hostname=\"...\")\n sock.connect(...)\n # Remote side disconnects\n sock.write(b\"data\") # raises BrokenPipeError — leaves stale OpenSSL error\n\ndef reader_greenlet():\n \"\"\"Healthy SSL connection that reads — gets spurious BrokenPipeError\"\"\"\n ctx = ssl.create_default_context()\n sock = ctx.wrap_socket(socket.socket(), server_hostname=\"...\")\n sock.connect(...)\n # This should block waiting for data, but instead raises BrokenPipeError\n sock.read(4096) # BrokenPipeError on a HEALTHY connection\n\ngevent.joinall([\n gevent.spawn(writer_greenlet),\n gevent.spawn(reader_greenlet),\n])\n```\n\n### Versions\n\n- CPython: 3.12.12, also present in `main` (3.15-dev, commit d14e31ed683)\n- OpenSSL: 3.5.1 (also reproducible with 3.0.x, 3.2.x)\n- OS: RHEL 10.1\n- gevent: 25.4.1 / 25.8.2\n\n---\n\nThe pseudocode reproducer is schematic — in practice, the trigger requires precise greenlet switching timing. The production scenario (AMQP consumers + HTTPS server in gevent) triggers it reliably every ~90 seconds.\n\n### CPython versions tested on:\n\n3.12\n\n### Operating systems tested on:\n\nLinux\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-148597\n* gh-159641\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/kswia","@type":"Person","name":"kswia"},"datePublished":"2026-04-15T05:55:11Z","url":"https://github.com/python/cpython/issues/148594"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:9b9970ac-91c3-868b-c557-29513084d796 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DB58:3F8AB1:49D03A5:5D84D01:6B023348 |
| html-safe-nonce | 4c8ef56f6e1261aefb11595c28470852dd7f4ce4f102d6ca26145fd1b7794c72 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEQjU4OjNGOEFCMTo0OUQwM0E1OjVEODREMDE6NkIwMjMzNDgiLCJ2aXNpdG9yX2lkIjoiNTk2MjM0NDA5NTQwMDczNTU2MCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 687ce6d285eca322053dd497b9b81f08c39a343e50005bfa533bdb4e27cb4d5d |
| hovercard-subject-tag | issue:4266584759 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/148594/issue_layout |
| twitter:image | https://opengraph.githubassets.com/840ecf87a3826612109184b1e74791f48e1ffcc9300ad45175c8deb68fb8d220/python/cpython/issues/148594 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/840ecf87a3826612109184b1e74791f48e1ffcc9300ad45175c8deb68fb8d220/python/cpython/issues/148594 |
| og:image:alt | Bug report Bug description: Summary _ssl__SSLSocket_read_impl and _ssl__SSLSocket_write_impl in Modules/_ssl.c do not call ERR_clear_error() before SSL_read_ex() / SSL_write_ex(). This allows stale... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | kswia |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width