log_ring: seqlock the records to prevent torn cross-process reads

The lossy MPSC log ring published each record by writing its text and THEN storing
the slot's sequence. A consumer that passed the seq==generation check could then
read text while a producer 'capacity' generations later overwrote that same slot
(it wrote text before bumping seq), yielding a torn line. Diagnostics-only and
practically unreachable (it needs the consumer a full ring behind -- ~60k lines/s
between two host drains), but a real data race.

Make it a proper seqlock: the producer stores seq 0 (in-progress) and fences
BEFORE touching the record, then publishes the generation after the text; the
consumer copies the record out and re-checks seq, dropping the line if it changed.
The ring stays lossy, never torn.

Adds log_ring_test (previously zero coverage): a deterministic wrap-drop case plus
a threaded torn-read guard (4 producers + a slow consumer on a 32-slot ring) that
emits 0 torn lines out of ~300k produced. Closes both the cross-process item and
the log_ring coverage gap.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-24 01:57:37 +02:00
parent 6840d9df88
commit 8fd209c73f
4 changed files with 205 additions and 11 deletions

View File

@@ -76,6 +76,12 @@ add_executable(mkb_ring_test mkb_ring_test.cpp)
target_link_libraries(mkb_ring_test PRIVATE coop_common)
add_test(NAME mkb_ring_test COMMAND mkb_ring_test)
# Unit test for the lossy MPSC log ring: deterministic wrap-drop + a threaded torn-read guard
# (many producers + a slow consumer on a small ring) that the seqlock copy+recheck must keep clean.
add_executable(log_ring_test log_ring_test.cpp)
target_link_libraries(log_ring_test PRIVATE coop_common)
add_test(NAME log_ring_test COMMAND log_ring_test)
# Unit test for the audio mixer math (decode/sum/soft-clip/encode). Header-only.
add_executable(audio_mix_test audio_mix_test.cpp)
target_include_directories(audio_mix_test PRIVATE ${CMAKE_SOURCE_DIR}/host/src)
@@ -321,6 +327,7 @@ coop_output_subdir(tests
detour_gate_test
hook_install_test
mkb_ring_test
log_ring_test
mkb_map_test
audio_mix_test
tone_analysis_test