Skip to content

guard vfsMap with a mutex (upstream psanford/sqlite3vfs#19) - #1

Merged
canesin merged 2 commits into
mainfrom
pr-19-vfsmap-mutex
Aug 18, 2026
Merged

guard vfsMap with a mutex (upstream psanford/sqlite3vfs#19)#1
canesin merged 2 commits into
mainfrom
pr-19-vfsmap-mutex

Conversation

@canesin

@canesin canesin commented Aug 18, 2026

Copy link
Copy Markdown

Summary

Merges upstream psanford/sqlite3vfs#19 ("guard vfsMap with a mutex" by @maxencehenneron) into our fork, plus a regression test.

vfsMap was written by newVFS (on RegisterVFS) and read by vfsFromC on every VFS callback with no synchronization. A registration racing an in-flight callback is a Go runtime "concurrent map read and map write" — a fatal, unrecoverable whole-process crash. The fix adds a sync.RWMutex (matching the existing fileMux/fileMap pattern in the same file): write-lock in newVFS, read-lock in vfsFromC.

Evaluation

  • Only two unsynchronized vfsMap access sites exist in the repo (write in newVFS, read in vfsFromC); the patch covers both. Initialization is package-init, safe.
  • Lock is never held across cgo calls and never nested — no deadlock window. RWMutex is appropriate: registration is rare, callback lookup is hot and stays concurrent.
  • Reproduced the race locally with a regression test (vfsmap_race_test.go) driving real sqlite connections against a tmp VFS while registering VFSes from another goroutine:
    • pristine main + go test -race: DATA RACE reported between newVFS and vfsFromC (via goVFSFullPathname cgo callback), matching the production crash trace in the upstream PR description
    • with the fix + go test -race -count=2: ok
  • Full suite passes with and without -race; go vet and gofmt clean.

Why this matters for the litestream fork

Litestream registers/uses VFSes from concurrent goroutines, so this crash mode is reachable in our deployment path.

maxencehenneron and others added 2 commits July 7, 2026 14:58
registering a new vfs writes vfsMap while callbacks read it from other
threads, which trips the runtime concurrent map access check
Exercises concurrent RegisterVFS (vfsMap write in newVFS) against
in-flight VFS callbacks (vfsMap read in vfsFromC) driven through real
sqlite connections. Fails under -race without the vfsMux fix from
upstream PR psanford#19.
@canesin
canesin merged commit 58e5296 into main Aug 18, 2026
1 check passed
@canesin
canesin deleted the pr-19-vfsmap-mutex branch August 18, 2026 15:20
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.

2 participants