Skip to content

Stop sqlite calling into moved Ruby objects - #723

Open
djmb wants to merge 1 commit into
sparklemotion:mainfrom
djmb:fix/pin-callback-values-against-gc-compaction
Open

Stop sqlite calling into moved Ruby objects#723
djmb wants to merge 1 commit into
sparklemotion:mainfrom
djmb:fix/pin-callback-values-against-gc-compaction

Conversation

@djmb

@djmb djmb commented Jul 31, 2026

Copy link
Copy Markdown

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.

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.
@djmb

djmb commented Jul 31, 2026

Copy link
Copy Markdown
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant