Skip to content

Thread safety bug in LogStream: sync() is not protected by critical section #8596

Closed

Description

@timosachsenberg

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_warn

However, 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::endl is used (calls flush() → sync())
  • Buffer overflows (calls overflow() → sync())
  • Explicit flush() calls

Unprotected Shared State

The syncLF_() function modifies several non-thread-safe members without locking:

  1. incomplete_line_ (std::string) - lines 251, 265
  2. log_cache_ (std::map) - lines 130, 164
  3. log_time_cache_ (std::map) - lines 133, 138, 167
  4. 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, released

Each 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

  1. Add mutex to sync(): Protect syncLF_() with a mutex or extend the critical section
  2. Use thread-local buffers: Each thread gets its own buffer, merged on flush
  3. Atomic multi-statement logging: Provide a mechanism to lock for entire log messages

Environment

Related

Activity

  1. timosachsenberg commented on Jan 7, 2026

    @timosachsenberg
    ContributorAuthor

    @cbielow does this sound like a real bug to you? could explain a few segfaults I got when replacing std::endl with '\n'

  2. cbielow commented on Jan 7, 2026

    @cbielow
    Contributor

    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...)

  3. timosachsenberg commented on Jan 7, 2026

    @timosachsenberg
    ContributorAuthor

    cool thanks. I will come up with a draft

  4. github-actions commented on Jul 7, 2026

    @github-actions
    Contributor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions