Skip to content

[fix] serialize register_function against connection Open (UDF map data race) - #19

Merged
vt128 merged 2 commits into
masterfrom
fix/serialize-udf-register-connect
Jul 14, 2026
Merged

vt128 merged 2 commits into
masterfrom
fix/serialize-udf-register-connect

Conversation

@vt128

@vt128 vt128 commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Problem (PKG-29, high)

modernc's Driver.Open reads its process-global user-defined-function map (d.udfs) with no lock (for _, udf := range d.udfs), while RegisterScalarFunction writes it. register_function already serializes writes under funcMutex, but a connection's Open — called lazily by database/sql (first query, pool growth, connection replacement), 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 (the starbox parallel season).

Fix

connect now opens through a funcMutex-serialized wrapper driver (ensureLocalDriver / udfSerializedDriver): every Open holds funcMutex.RLock while modernc reads the UDF map, mutually exclusive with register_function's Lock/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_function against 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

TestConcurrentRegisterAndConnectNoRace hammers register_function and memory-DB Open concurrently under -race and reports no race. Test-first verified: temporarily opening the unwrapped "sqlite" driver makes -race report DATA RACE.

Verification

go test -race (local + go1.20 Docker -race), go1.25 go mod tidy no-op, go vet, gofmt, doccov (29/29 builtins + 16/16 config accessors), coverage 78.6% (min 69). Touched functions gocyclo ≤8 (the pre-existing connect/starlarkToDriverValue are untouched). Also bumps base to v0.1.3 for ecosystem convergence.

🤖 Generated with Claude Code

vt128 and others added 2 commits July 15, 2026 01:41
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>
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 4 complexity · 0 duplication

Metric Results
Complexity 4
Duplication 0

View in Codacy

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.

@vt128
vt128 merged commit 13bc262 into master Jul 14, 2026
10 checks passed
@vt128
vt128 deleted the fix/serialize-udf-register-connect branch July 14, 2026 17:56
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