Stop sqlite calling into moved Ruby objects - #723
Open
djmb wants to merge 1 commit into
Open
Conversation
When you register a scalar function, aggregator, collation, trace handler or authorizer, we hand sqlite a pointer to a Ruby object. The garbage collector can move that object without updating sqlite's copy of the address. The database's C struct now points at the collections holding those callbacks, so the mark function can pin them. Trace and authorizer passed the database and read the callback out of an instance variable. They now pass the struct instead, which lives in malloc'd memory the collector never relocates. The busy handler already worked this way. Writes to the new fields go through RB_OBJ_WRITE so the collector keeps them alive. There is a test for each of the five that forces every movable object to move. None of them hold the object under test in a local variable, which would prevent that.
Author
|
@flavorjones - I found this issue because I was getting random errors and crashes from a Rails app that used custom functions running parallel tests against SQLite. I got Claude + Codex to then track down the other instances of unpinned objects that the GC could move. |
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.
When you register a scalar function, aggregator, collation, trace handler or authorizer, we hand sqlite a pointer to a Ruby object. The garbage collector can move that object without updating sqlite's copy of the address.
The database's C struct now points at the collections holding those callbacks, so the mark function can pin them.
Trace and authorizer passed the database and read the callback out of an instance variable. They now pass the struct instead, which lives in malloc'd memory the collector never relocates. The busy handler already worked this way.
Writes to the new fields go through RB_OBJ_WRITE so the collector keeps them alive.
There is a test for each of the five that forces every movable object to move. None of them hold the object under test in a local variable, which would prevent that.