Skip to content

report() from inside the Logger races with close() on TraceClient::shared_ #14

Closed

Description

@dmccoystephenson

Summary

report() called from inside the Logger can race with close() on the program's thread. ThreadSanitizer reports this as a data race on TraceClient::shared_.

Where

  • TraceClient::close() (trace_client.hpp:1038-1040) does shared.swap(shared_) first, with no lock held, and only then waits for the sending thread to drain.
  • TraceClient::enqueue() (trace_client.hpp:1139) reads shared_ (if (!shared_ || ...), then shared_->...) with no lock held.
  • The Logger runs on the sending thread (deliver → Shared::log, trace_client.hpp:819-823). The logMutex comment in Shared (trace_client.hpp:435) says "a logger may itself call report()".

close() drains the queue after the swap, so a report that fails during the drain reaches the logger. If that logger calls report(), the sending thread reads shared_ while the program's thread writes it. This is not limited to a contrived test: a logger that reports its own drops (e.g. as a "trace-drop" event) is enough, as long as a report fails while close() is draining.

Evidence

This was found while adding aLoggerMayItselfReport in PR #13. In the first version of that test, the main thread waited for the stub server to see both requests and then called close(). TSan (CI job linux-sanitizers (thread), run 39592415441) reported:

WARNING: ThreadSanitizer: data race
  Write: std::__shared_ptr<Shared>::swap ... TraceClient::close(double) trace_client.hpp:1040
  Previous read: __shared_ptr::operator bool ... TraceClient::enqueue(...) trace_client.hpp:1139
    ... aLoggerMayItselfReport()::$_0 ... Shared::log trace_client.hpp:443 ... deliver trace_client.hpp:823
SUMMARY: ThreadSanitizer: data race trace_client.hpp:1040 in trace_client::TraceClient::close(double)

PR #13 keeps the test but makes the logger hand control back to the main thread before close() runs. That establishes a happens-before edge, so the test checks only "a logger may report", not this race.

Possible directions (owner's call)

  • Have enqueue reach the queue through state the sending thread owns, e.g. a closing check under shared->mutex, so it never reads TraceClient::shared_ from the sending thread. This needs shared_ itself to be read safely, e.g. through a copy taken under a lock or via std::atomic_load on the shared_ptr, which C++11 provides.
  • Or document that report() from inside the logger is only safe while no other thread is calling close(), and correct the logMutex comment.

A test for the fix could reuse aLoggerMayItselfReport from PR #13 without its handshake: the server returns 500 and the main thread calls close() while the logger reports. It should fail under TSan before the fix and pass after.

This issue was drafted during a Gardener session (https://github.com/Stephenson-Software/gardener).


drafted by Claude on behalf of Daniel Stephenson

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions