fix(audit): one-fwrite records + document db stmt-cache invariant (F6)#32
Merged
Merged
Conversation
Two audit findings, both small: 1. Audit JSONL interleave. With --audit, records emitted from compute.async / gpu.async worker threads share stderr with the event loop, and ShJsonWriter wrote token-by-token (many fwrites plus a separate fputc newline), so two records could interleave byte-wise and corrupt the JSONL. Accumulate each record into a thread-local buffer and flush it with a SINGLE fwrite, which the stdio stream lock makes atomic relative to other threads. Lock-free (a record is always built start-to-finish on one thread, no nesting), no coupling to the log lock. Oversized records (>4 KiB, none in practice) truncate rather than interleave. Output format is unchanged (test_audit still green). 2. db prepared-statement cache non-reentrancy (iterator-invalidation note). Document the invariant at the row-callback call site and the LRU-evict site in cap/db.c: a row callback must not re-enter the statement cache, because evicting the in-flight stmt (sqlite3_finalize) would dangle it for the next sqlite3_step. Holds today (all callers are pure C result- marshallers that hand the result set to script only after the loop ends); comment-only, to preserve the invariant if a streaming per-row script callback API is ever added. Validated: build clean, 50/50 unit suites (incl. test_audit), green under ASan. The concurrent-interleave fix is exercised by the TSan CI job.
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.
Audit finding F6 (last of the batch), two small parts:
--audit, records emitted fromcompute.async/gpu.asyncworker threads share stderr with the event loop, andShJsonWriterwrote token-by-token (manyfwrites + a separate newlinefputc), so two records could interleave byte-wise and corrupt the JSONL. Each record now accumulates into a thread-local buffer and flushes with a singlefwrite, which the stdio stream lock makes atomic relative to other threads. Lock-free (records are built start-to-finish on one thread, no nesting), no log-lock coupling. Oversized records (>4 KiB, none in practice) truncate rather than interleave. Output format unchanged (test_auditgreen).cap/db.c: a row callback must not re-enter the statement cache (evicting the in-flightstmtwould dangle it for the nextsqlite3_step). Holds today (pure C result-marshallers); comment-only, to preserve it if a streaming per-row script callback API is ever added.Validated: build clean, 50/50 unit suites (incl.
test_audit), green under ASan. The concurrent-interleave fix is exercised by the TSan CI job.