rb_trilogy_connect: add GC guard for all string configs - #313
Merged
Conversation
Fix: trilogy-libraries#312 Since we're initializing `connopt` with pointers inside the user provided strings, we must ensure that they are pinned until we return from `rb_trilogy_connect`, otherwise if compaction happens concurrently, or another thread clears the `opts` hash, they could be guarbage collected or moved. To achieve that, every string option has an associated local variables that is guarded to ensure it isn't optimized out by the compiler.
Methods that take a string and release the GVL should handle concurrent modification of the string and concurrent compaction. For compaction the solution is to ensure they are pinned by virtue of being present on the stack, which is normally the case but compilers may optimize that out, so we should explictly use `RB_GC_GUARD`. As for concurrent modification, the intended API for that is `rb_str_locktmp` but it's quite incovenient to use when the method may raise, so a more ergonomic API is to acquire our own frozen copy with `rb_str_new_frozen`. That means an extra allocation but it's an acceptable tradeoff.
jeremy
reviewed
Aug 5, 2026
jeremy
left a comment
Contributor
There was a problem hiding this comment.
Also noting that handle_trilogy_error is NORETURN and the guards sit after it… in principle the compiler could treat those VALUEs as dead on a branch that never reaches the guard. Kept live on the clang I checked, but leans on compiler behavior rather than a guarantee.
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.
Fix: #312
Since we're initializing
connoptwith pointers inside the user provided strings, we must ensure that they are pinned until we return fromrb_trilogy_connect, otherwise if compaction happens concurrently, or another thread clears theoptshash, they could be guarbage collected or moved.To achieve that, every string option has an associated local variables that is guarded to ensure it isn't optimized out by the compiler.