Repository navigation
Conversation
Under the M:N thread scheduler, a Ruby thread in a non-main Ractor may
be resumed on a different native thread after rb_thread_call_without_gvl()
returns. The OpenSSL error queue is thread-local, so errors pushed by
the function without the GVL are left on the previous native thread:
the exception loses its reason, and the stray errors appear in an
unrelated exception raised later on that native thread.
8.times.map {
Ractor.new {
4.times.map {
Thread.new {
2000.times.map do
OpenSSL::KDF.pbkdf2_hmac("", salt: "", iterations: 0, length: 1, hash: "sha256")
rescue OpenSSL::KDF::KDFError => e
e.message
end.uniq
}
}.flat_map(&:value).uniq
}
}.flat_map(&:value).uniq
#=> ["PKCS5_PBKDF2_HMAC: invalid iteration count", "PKCS5_PBKDF2_HMAC"]
Save the error queue at the end of the function and restore it after
the GVL is reacquired, using OSSL_ERR_STATE (OpenSSL 3.2+).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
I hadn't thought about this. Thanks for catching it!
I think it would also be acceptable for these methods to clear the queue, since I expect they fail only because of invalid argument values. I suspect this might not be the only place ruby/openssl breaks if the native thread changes. In particular, some |
|
My initial thought was whether we could get away with simply discarding the OpenSSL error queue contents, assuming doing so would only affect the quality of error messages. However, it turns out that some parts of ruby/openssl do need to preserve error queue contents, so we need to handle this properly. In particular, it seems pretty bad that a Ruby proc set to I'm taking this over in #1168 to address all the related issues together. Thanks for spotting this! |
Under the M:N thread scheduler, a Ruby thread in a non-main Ractor may be resumed on a different native thread after rb_thread_call_without_gvl() returns. The OpenSSL error queue is thread-local, so errors pushed by the function without the GVL are left on the previous native thread: the exception loses its reason, and the stray errors appear in an unrelated exception raised later on that native thread.
Save the error queue at the end of the function and restore it after the GVL is reacquired, using OSSL_ERR_STATE (OpenSSL 3.2+).
What do you think? @ko1 @rhenium