Repository navigation
Description
Summary
The OpenMS logger has a concurrency bug where sync() (called by std::endl/flush()) is not protected by the OPENMS_THREAD_CRITICAL lock, leading to potential data races and segfaults in multi-threaded code.
Root Cause
The OPENMS_LOG_* macros use OPENMS_THREAD_CRITICAL(LOGSTREAM) to protect individual << operations:
// LogStream.h:447-449
#define OPENMS_LOG_WARN \
OPENMS_THREAD_CRITICAL(LOGSTREAM) \
OpenMS_Log_warnHowever, the sync() method in LogStreamBuf is not protected:
// LogStream.cpp:298-305
int LogStreamBuf::sync()
{
int ret = 0;
ret = syncLF_(); // NO LOCK PROTECTION!
return ret;
}sync() is called when:
std::endlis used (callsflush()→sync())- Buffer overflows (calls
overflow()→sync()) - Explicit
flush()calls
Unprotected Shared State
The syncLF_() function modifies several non-thread-safe members without locking:
incomplete_line_(std::string) - lines 251, 265log_cache_(std::map) - lines 130, 164log_time_cache_(std::map) - lines 133, 138, 167- Buffer pointers via
pbump()- line 293
Race Condition Scenario
Thread A Thread B
──────── ────────
OPENMS_LOG_WARN << "msg1"
OPENMS_LOG_WARN << "msg2"
OPENMS_LOG_WARN << std::endl
→ calls sync() OPENMS_LOG_WARN << std::endl
→ modifies incomplete_line_ → calls sync() CONCURRENTLY
→ modifies log_cache_ → CORRUPTS shared state
→ pbump() → SEGFAULT
Additional Issue: Multi-Statement Logging
The current design also doesn't protect multi-statement log messages:
OPENMS_LOG_WARN << "Warning: " << name; // Lock acquired, released
OPENMS_LOG_WARN << " in '" << prefix; // Lock acquired, released (another thread can interleave!)
OPENMS_LOG_WARN << "!" << std::endl; // Lock acquired, releasedEach statement is a separate critical section, allowing interleaving between them.
How This Was Discovered
PR #8582 replaced std::endl with '\n' for performance. This caused Param_test to segfault in CI. The std::endl was inadvertently masking the race condition by:
- Flushing frequently (smaller data chunks)
- Keeping
incomplete_line_small - Reducing the window for concurrent modification
Suggested Fixes
- Add mutex to
sync(): ProtectsyncLF_()with a mutex or extend the critical section - Use thread-local buffers: Each thread gets its own buffer, merged on flush
- Atomic multi-statement logging: Provide a mechanism to lock for entire log messages
Environment
- Discovered during PR perf: replace std::endl with '\n' in high-impact files to improve I/O performance #8582 review
- Affects all multi-threaded OpenMS code using
OPENMS_LOG_*macros - OpenMP must be enabled (
_OPENMPdefined) for the issue to manifest
Related
- PR perf: replace std::endl with '\n' in high-impact files to improve I/O performance #8582: perf: replace std::endl with '\n' in high-impact files
- The segfault was in
Param_testwhich usesOPENMS_LOG_WARNin multi-statement patterns
Activity
timosachsenberg commentedon Jan 7, 2026
@cbielow does this sound like a real bug to you? could explain a few segfaults I got when replacing std::endl with '\n'
cbielow commentedon Jan 7, 2026
I think #8582 is unlikely to affect Param_test since a) Param_test does not use multithreading AFAIK b) Param_test only has a handful of Warnings via LogStream...
However, making Loggers threadlocal would void the need for any mutex/critical section and should be easy to do (maybe at the cost of N duplications, where N is the number of threads...)
timosachsenberg commentedon Jan 7, 2026
cool thanks. I will come up with a draft
[FEATURE] Add thread-local logging to fix data races (issue #8596)
Add thread-local logging to fix data races in LogStream (#8596)
Combine thread-safe logging (PR #8610) with OPENMS_LOG_* macro optimi…
Fix LogStreamBuf thread-safety: shared static buffer, sink & Colorizer (
Fix LogStreamBuf thread-safety: shared static buffer, sink & Colorizer (
Fix LogStreamBuf thread-safety: shared static buffer, sink & Colorizer (
github-actions commentedon Jul 7, 2026
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.