Skip to content

Keep the OpenSSL error queue across GVL acquisitions and callbacks - #1168

Merged
rhenium merged 4 commits into
ruby:masterfrom
rhenium:ky/ssl-callbacks-error-queue
Nov 30, 2026
Merged

rhenium merged 4 commits into
ruby:masterfrom
rhenium:ky/ssl-callbacks-error-queue

Conversation

@rhenium

@rhenium rhenium commented Nov 27, 2026

Copy link
Copy Markdown
Member

Fixes #1145


ossl.c: add ossl_err_state_*()

Add a helper that wraps OSSL_ERR_STATE_*() in OpenSSL >= 3.2, and
emulates the behavior with older OpenSSL versions and LibreSSL/AWS-LC.


kdf, pkey: keep the OpenSSL error queue across GVL acquisition

Under the M:N thread scheduler, a Ruby thread may be resumed on a
different native thread after rb_thread_call_without_gvl() or
rb_thread_call_with_gvl() re-acquires the GVL.

Save the error queue before re-acquiring the GVL and restore it
afterward.

This patch also fixes a potential memory leak in
OpenSSL::PKey.generate_* when rb_thread_call_without_gvl() is
interrupted.

Co-authored-by: Yusuke Endoh mame@ruby-lang.org


x509store: keep the OpenSSL error queue across verify_callback

The verify_callback may be called with a non-empty error queue in some
cases. Save the error queue before executing any Ruby code and restore
it afterward.


ssl: keep the OpenSSL error queue across callbacks

OpenSSL may call some callbacks in OpenSSL::SSL after adding entries
to the error queue. These callbacks must leave the queue intact, as
SSL_get_error() relies on its contents to determine the cause of the
error. Otherwise, we may end up raising the wrong exception.

I have confirmed that verify_callback and session_remove_cb can be
called with a non-empty error queue, but there may be other cases.

As in the previous patches, save the error queue before executing any
Ruby code and restore it afterward.

rhenium and others added 4 commits November 28, 2026 03:15
Add a helper that wraps OSSL_ERR_STATE_*() in OpenSSL >= 3.2, and
emulates the behavior with older OpenSSL versions and LibreSSL/AWS-LC.
Under the M:N thread scheduler, a Ruby thread may be resumed on a
different native thread after rb_thread_call_without_gvl() or
rb_thread_call_with_gvl() re-acquires the GVL.

Save the error queue before re-acquiring the GVL and restore it
afterward.

This patch also fixes a potential memory leak in
OpenSSL::PKey.generate_* when rb_thread_call_without_gvl() is
interrupted.

Co-authored-by: Yusuke Endoh <mame@ruby-lang.org>
The verify_callback may be called with a non-empty error queue in some
cases. Save the error queue before executing any Ruby code and restore
it afterward.
OpenSSL may call some callbacks in OpenSSL::SSL after adding entries
to the error queue. These callbacks must leave the queue intact, as
SSL_get_error() relies on its contents to determine the cause of the
error. Otherwise, we may end up raising the wrong exception.

I have confirmed that verify_callback and session_remove_cb can be
called with a non-empty error queue, but there may be other cases.

As in the previous patches, save the error queue before executing any
Ruby code and restore it afterward.
@rhenium
rhenium force-pushed the ky/ssl-callbacks-error-queue branch from 84e27ab to f06b250 Compare November 27, 2026 19:19
@rhenium
rhenium merged commit 05365ad into ruby:master Nov 30, 2026
48 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant