Skip to content

Clear the OpenSSL error queue before SSL_read/SSL_write - #1390

Open
r3wretrhy wants to merge 1 commit into
redis:masterfrom
r3wretrhy:fix/ssl-clear-error-queue-1389
Open

r3wretrhy wants to merge 1 commit into
redis:masterfrom
r3wretrhy:fix/ssl-clear-error-queue-1389

Conversation

@r3wretrhy

@r3wretrhy r3wretrhy commented Oct 24, 2026 •

Copy link
Copy Markdown

Fixes #1389.

redisSSLRead() and redisSSLWrite() call SSL_get_error() after SSL_read()/SSL_write() without clearing the thread's OpenSSL error queue first. If something is already on the queue (for example the "unexpected eof while reading" left behind when another connection was dropped without close_notify), SSL_get_error() returns SSL_ERROR_SSL/SSL_ERROR_SYSCALL instead of SSL_ERROR_WANT_READ, and a non-blocking read that just has no data yet fails with REDIS_ERR_IO.

This adds ERR_clear_error() before both calls, the same way redisSSLConnect() already does before SSL_connect().

There's a new SSL test that does a round trip on a connection, switches it to non-blocking, puts an error on the queue and calls redisBufferRead(). CMake needs OpenSSL::Crypto on hiredis-test for that, since the test calls libcrypto directly.

Testing (Debian, OpenSSL 3.5.7, redis-server 8.0.2):

  • make USE_SSL=1 check: all tests pass; with the ssl.c change reverted the new test fails.
  • CMake build with -DENABLE_SSL=ON -DENABLE_SSL_TESTS=ON plus TEST_SSL=1 ./test.sh: all tests pass.
  • The repro.c from the issue: planted and drop now return REDIS_OK for connection B (before: "Resource temporarily unavailable"); control and drop-clear unchanged.

Note

Medium Risk
Changes TLS I/O error handling on every SSL read/write; behavior is narrowly scoped but affects connection reliability for non-blocking and multi-connection workloads.

Overview
Fixes incorrect I/O errors on non-blocking TLS reads/writes when the thread’s OpenSSL error queue already holds stale entries (e.g. from a prior connection). redisSSLRead() and redisSSLWrite() now call ERR_clear_error() before SSL_read/SSL_write, matching redisSSLConnect(), so SSL_get_error() can return WANT_READ/WANT_WRITE instead of SSL_ERROR_SSL.

Adds a non-Windows SSL regression test that plants a fake queue entry and asserts redisBufferRead() stays REDIS_OK on an idle non-blocking connection. CMake links OpenSSL::Crypto into hiredis-test when SSL tests are enabled so the test can use libcrypto APIs directly.

Reviewed by Cursor Bugbot for commit a1d3b8f. Bugbot is set up for automated code reviews on this repo. Configure here.

SSL_get_error() only reports SSL_ERROR_WANT_READ/WANT_WRITE reliably if
the thread's error queue is empty before the I/O call. A stale entry,
for example the 'unexpected eof while reading' left behind when a server
drops another connection without close_notify, made a non-blocking read
that simply had no data yet fail with REDIS_ERR_IO.

Clear the queue before SSL_read() and SSL_write(), as redisSSLConnect()
already does before SSL_connect(), and add a regression test.

Fixes redis#1389
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-24T06:54:25.519469Z a1d3b8f PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TLS: a stale OpenSSL error makes non-blocking reads fail with "Resource temporarily unavailable"

1 participant