Repository navigation
Fix LogStreamBuf thread-safety: shared static buffer, sink & Colorizer (#9515) - #9583
Conversation
|
@timosachsenberg The following names might be missing from the
Please consider applying the following patch: --- AUTHORS 2026-06-16 06:36:25.687693644 +0000
+++ AUTHORS.new 2026-06-16 06:36:30.814822240 +0000
@@ -23,8 +23,8 @@
- Chirag Dave
- Chris Bauer
- Chris Bielow
- - Christie Mathews
- Christian Ehrlich
+ - Christie Mathews
- Clemens Groepl
- Cornelia Friedle
- Daniel Jameson |
|
Warning Review limit reached
More reviews will be available in 6 minutes and 27 seconds. Learn how PR review limits work. Your organization has used up its prepaid credits, and credit purchases are no longer available. Enable the review add-on in the billing tab to keep reviews running — you're only billed for reviews past your plan's rate limits ($0.25/file). ⌛ How to resolve this issue?After more reviews become available, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available. Please see our Fair Usage Limits Policy for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
📝 WalkthroughWalkthroughThread-safety of ChangesLogStream thread-safety hardening
Possibly related issues
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/tests/class_tests/openms/source/LogStream_test.cpp`:
- Around line 139-147: The current loop logic only counts total occurrences of
"racing_line_" to verify the count matches num_iterations, which is insufficient
because a dropped message and a duplicated message would cancel out and still
pass. Instead of just counting occurrences in the for loop starting at the
out.find("racing_line_") call, extract and track the individual message IDs from
the captured output by parsing each "racing_line_" occurrence along with its
associated numeric ID, then assert that each ID from 0 to num_iterations-1
appears exactly once by checking that no IDs are duplicated and none are
missing.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 4d80ce00-c2a2-4f70-92c2-7dc621882b26
📒 Files selected for processing (3)
src/openms/include/OpenMS/CONCEPT/LogStream.hsrc/openms/source/CONCEPT/LogStream.cppsrc/tests/class_tests/openms/source/LogStream_test.cpp
df9e523 to
564a411
Compare
#9515) OPENMS_LOG_* routes through thread-local LogStream/LogStreamBuf instances, but three shared resources survived the thread-local refactor (#8620/#8596) and corrupt the heap when logging from OpenMP parallel regions (0xC0000374 on Windows; hit in practice by the imzML reader in #9454): 1. syncLF_() assembled lines through a function-local `static char buf[BUFFER_LENGTH]` shared across every thread. Replaced with direct std::string::append into incomplete_line_/outstring, so there is no scratch buffer (and no out-of-bounds read when a racing thread overwrites the trailing NUL). 2. distribute_() wrote to the shared sink (std::cerr/std::cout) and mutated the shared global Colorizer (yellow/red/magenta -- its std::stringstream and flags) from multiple threads without synchronization. Both are now serialized by a single global std::mutex held around the whole distribute_() body. The mutex is intentionally leaked so it stays valid during static teardown (a std::mutex, unlike the former OpenMP critical section, does not depend on the OpenMP runtime). Notifier callbacks run after the lock is released to avoid self-deadlock. 3. expandPrefix_() used non-reentrant std::localtime (shared static tm). Replaced with localtime_r/localtime_s. Also corrects the stale LogStream.h note that claimed the macros are protected by OPENMS_THREAD_CRITICAL (they are not -- safety comes from thread-local buffers plus the new sink mutex), and adds a regression test that logs 5000 unique messages from an 8-thread OpenMP region and asserts each arrives exactly once and intact. Co-Authored-By: Claude Opus 4.8 <[email protected]>
564a411 to
389b45f
Compare
Add entries for all substantive non-test PRs merged since the last changelog update: - #9623 ThermoRawFile full mzML metadata extraction + polarity fix - #9602 New FileInfo library class usable from C++ and pyOpenMS - #9521 IMAGING multi-region support (add missing PR reference) - #9620 Deisotoper::isToleranceSupported + ProSE/SSE crash fix on low-res data - #9610 DistanceMatrix::setValue cached-minimum guard fix - #9497 DistanceMatrix::operator==, ClusteringGrid, File path guard fixes - #9583 LogStreamBuf thread-safety fix - #9514 CI Windows migration to Visual Studio 2026 Claude-Session: https://claude.ai/code/session_01YLg6pWyjyTxpWAU6mgrDzC Co-authored-by: Claude <[email protected]>
Fixes #9515.
Problem
OPENMS_LOG_*resolves to thread-localLogStream/LogStreamBufinstances (#8620 / #8596), so per-stream state (incomplete_line_,log_cache_, …) is no longer raced. But three shared resources survived the refactor and corrupt the heap when warnings are emitted from inside an OpenMP parallel region (0xC0000374/STATUS_HEAP_CORRUPTIONon Windows). This was hit in practice by the imzML reader in #9454, whosepopulateSpectraWithData_()logs a per-array warning for every spectrum inside a#pragma omp parallel for.Root cause & fix
Shared
staticline buffer.LogStreamBuf::syncLF_()assembled lines through a function-localstatic char buf[BUFFER_LENGTH]shared across all threads (strncpy/memcpyin, C-string read back out). A racing overwrite of the trailing'\0'could also driveincomplete_line_ += &buf[0]past the array.→ Removed the scratch buffer entirely: append directly into
incomplete_line_/outstringwithstd::string::append(ptr, len).Unsynchronized shared sink and shared global
Colorizer.distribute_()wrote*(stream) << … << std::endlto the sharedstd::cerr/std::coutand called(*colorizer_)()/.undo()on the shared globalColorizer(yellow/red/magenta) from multiple threads. The colorizer mutates a heap-backedstd::stringstream(input_) unconditionally (independent of TTY redirection), which is itself a genuine data race and a direct heap-corruption source on the defaultcerrpath.→ Serialized the entire
distribute_()body with a single globalstd::mutex(covers the stream writes, the Colorizer state mutation, and prefix expansion). The mutex is intentionally leaked so it stays valid during static teardown — unlike the former OpenMP critical section, astd::mutexdoes not depend on the OpenMP runtime, so locking in the destructor path is safe. Notifier callbacks are invoked after the lock is released so a notifier that logs cannot self-deadlock.Non-reentrant
localtime.expandPrefix_()usedstd::localtime(shared statictm), which races when a time-format prefix (%T/%t/%D/…) is configured.→ Replaced with
localtime_r/localtime_s.Also fixes the stale class doc in
LogStream.h, which claimed the macros are "protected by aOPENMS_THREAD_CRITICALdirective". They are not — thread-safety comes from the thread-local buffers plus the new sink mutex.Testing
LogStream_test.cpp: logs 5000 unique messages from an 8-thread OpenMP region (capturingcerr, which carries theyellowcolorizer) and asserts every message arrives exactly once and intact (count == 5000). Passes locally.LogStream_testsuite passes.static bufand Colorizerstringstreamraces; the heap-verifier catches the OOB path).Notes
🤖 Generated with Claude Code
Summary by CodeRabbit
Bug Fixes
Documentation
Tests