Repository navigation
[fix] serialize register_function against connection Open (UDF map data race) - #19
Merged
Merged
Conversation
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…ta race) modernc's Driver.Open reads its process-global user-defined-function map (d.udfs) with no lock, while RegisterScalarFunction writes it. register_function already serializes writes under funcMutex, but a connection's Open — called lazily by database/sql, and from another host machine in a parallel runtime — reads d.udfs unlocked, so a concurrent register_function + connect is a data race (potentially a fatal concurrent-map access). The README "register before connect" convention doesn't hold once machines run in parallel. connect now opens through a funcMutex-serialized wrapper driver (ensureLocalDriver / udfSerializedDriver): every Open holds funcMutex for reading while modernc reads the UDF map, mutually exclusive with register_function's write. Concurrent Opens still run in parallel (a read lock). The remote (libSQL) path is unaffected — it has no UDF map. Test: hammering register_function and memory-DB Open concurrently under -race reports no race (and DATA RACE without the wrapper — verified by temporarily opening the unwrapped "sqlite" driver). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 4 |
| Duplication | 0 |
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
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.
Problem (PKG-29, high)
modernc's
Driver.Openreads its process-global user-defined-function map (d.udfs) with no lock (for _, udf := range d.udfs), whileRegisterScalarFunctionwrites it.register_functionalready serializes writes underfuncMutex, but a connection'sOpen— called lazily bydatabase/sql(first query, pool growth, connection replacement), and from another host machine in a parallel runtime — readsd.udfsunlocked. So a concurrentregister_function+connectis a data race, potentially a fatal concurrent-map access. The README "register before connect" convention doesn't hold once machines run in parallel (the starbox parallel season).Fix
connectnow opens through afuncMutex-serialized wrapper driver (ensureLocalDriver/udfSerializedDriver): everyOpenholdsfuncMutex.RLockwhile modernc reads the UDF map, mutually exclusive withregister_function'sLock/write. Concurrent Opens still run in parallel (a read lock). The wrapper covers all lazy pool Opens because it is the registered driver. The remote libSQL path is unaffected (no UDF map).Scope / limits (documented)
This serializes this module's
register_functionagainst its own connections. It does not protect a different process-wide component opening the raw"sqlite"driver, nor concurrent collation/connection-hook registration — this module registers neither, and those are modernc's own unlocked globals. (A truly process-wide fix would have to live in modernc.)Test
TestConcurrentRegisterAndConnectNoRacehammersregister_functionand memory-DBOpenconcurrently under-raceand reports no race. Test-first verified: temporarily opening the unwrapped"sqlite"driver makes-racereportDATA RACE.Verification
go test -race(local + go1.20 Docker-race), go1.25go mod tidyno-op,go vet,gofmt, doccov (29/29 builtins + 16/16 config accessors), coverage 78.6% (min 69). Touched functions gocyclo ≤8 (the pre-existingconnect/starlarkToDriverValueare untouched). Also bumps base to v0.1.3 for ecosystem convergence.🤖 Generated with Claude Code