Repository navigation
Keep the OpenSSL error queue across GVL acquisitions and callbacks - #1168
Merged
Merged
Conversation
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
force-pushed
the
ky/ssl-callbacks-error-queue
branch
from
November 27, 2026 19:19
84e27ab to
f06b250
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.