Merged
Conversation
ueno
approved these changes
Feb 9, 2026
Owner
ueno
left a comment
There was a problem hiding this comment.
Looks very reasonable, thank you!
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.
Improve thread safety of ruby-gpgme. Hopefully addresses #115.
The ruby-gpgme gem has no thread safety mechanisms, which can cause "Bad file descriptor" errors when multiple threads perform GPG operations concurrently (e.g. in Rails/Sidekiq environments). The root causes include unsafe GC behavior with the C extension, lack of synchronization, and fragile fd handling in the passphrase callback.
Changes:
Removed
RUBY_TYPED_FREE_IMMEDIATELYfrom all four typed data structs (Data, Ctx, Key, TrustItem). This flag allowed Ruby's GC to free the underlying C structs outside the GVL, which is unsafe when another thread may still be using them. Setting flags to 0 defers freeing to a safe point.Added
RB_GC_GUARDcalls after blocking GPGME operations in 7 functions: gpgme_op_decrypt, gpgme_op_decrypt_verify, gpgme_op_sign, gpgme_op_encrypt, gpgme_op_encrypt_sign, gpgme_op_verify, and gpgme_op_keylist_next. This prevents the GC from collecting Ruby-wrapped objects (contexts, data buffers) while the C library is still using their underlying pointers.At the ruby layer:
Added opt-out thread-safe mode via GPGME.thread_safe = true. When enabled, all block-form
Ctx.newcalls are serialized through a global Monitor (reentrant mutex). A Monitor is used rather than a Mutex because GPGME operations are reentrant — e.g. Crypto#sign → Ctx.new → Key.find → Ctx.new.Improved
Ctx.pass_functionfd handling: uses explicit io.write instead of io.puts, setsautoclose = falseto prevent Ruby from closing the fd that belongs to GPGME/gpg-agent, and wraps the write in error handling.Usage
Thread-safe mode is on by default but can be altered
GPGME.thread_safe = falseif it's proving problematic for some reason.