Skip to content

Synchronize web VFS last_error_ across threads - #20

Merged
mlin merged 1 commit into
mlin:mainfrom
Wid-L-Hacker:conrad/c6-web-last-error-race
Oct 24, 2026
Merged

mlin merged 1 commit into
mlin:mainfrom
Wid-L-Hacker:conrad/c6-web-last-error-race

Conversation

@Wid-L-Hacker

Copy link
Copy Markdown
Contributor

The web VFS shares one last-error string across connections but accesses it without synchronization. Concurrent error reporting can corrupt the string, not just mix up messages. This is the web counterpart of the nested-VFS race addressed in sqlite_zstd_vfs #19 (C5).

What happens today

Error paths in Open() and FetchDatabaseHeader() write last_error_. It is read through the xGetLastError trampoline and by SQLITE_VFS_LOG call sites. Under a failed-open storm — many threads opening databases against a failing origin at once — ThreadSanitizer reported write-against-write races inside the string's internals, and a write racing the strncpy in GetLastError().

The fix

A mutex now guards the string. The write sites set the message through a small SetLastError() helper, and GetLastError() copies the message out while holding the lock. Existing error strings and explicit return codes are unchanged; only the synchronization is new.

How it was verified

ThreadSanitizer, before and after, on the same storm workload (40 successful opens, 400 failed opens, 12,000 rows read back). The pristine build reports the race family described above. The patched build reports zero races in web_vfs.h, with identical functional outcomes.

Point for review

The mutex preserves the existing shared-slot semantics and mirrors the nested-VFS fix. The alternative is thread_local storage, which gives each thread its own error slot. If you prefer it here, the nested-VFS fix in sqlite_zstd_vfs #19 should change to match.

Found during follow-up review of the 2026-10-23 audits.

The shared VFS object's std::string was written by Open/FetchDatabaseHeader error paths and read by xGetLastError and logging with no synchronization; ThreadSanitizer caught concurrent writes through the string internals and a write-vs-read through GetLastError's strncpy during a failed-open storm. Guard set/get with a mutex (mirrors the nested-VFS C5 fix); error text and codes unchanged.
@Wid-L-Hacker
Wid-L-Hacker marked this pull request as ready for review October 24, 2026 19:15
@mlin
mlin merged commit 80dee60 into mlin:main Oct 24, 2026
5 checks passed
@mlin
mlin deleted the conrad/c6-web-last-error-race branch October 24, 2026 19:16
Wid-L-Hacker added a commit to Wid-L-Hacker/sqlite_web_vfs that referenced this pull request Oct 25, 2026
Resolves the FetchDatabaseHeader conflict with main: the branch's
geometry-first Content-Range validation, with its error paths in
main's SetLastError form (from mlin#20's last_error_ synchronization).
The resolved file is byte-identical to a local merge of main
(80dee60) into this branch, verified by building and testing the
merged tree (74 unit assertions, 9/9 origin tests, lint green).
Wid-L-Hacker added a commit to Wid-L-Hacker/sqlite_web_vfs that referenced this pull request Oct 25, 2026
Supersedes mlin#19: identical final content, based on current main
(after mlin#17, mlin#18, mlin#20), so it merges cleanly. See the PR body for
the full round-by-round description.
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