Skip to content

A throw in init() after start() leaves thread_ joinable, so the destructor calls std::terminate #16

Closed

Description

@dmccoystephenson

Summary

If anything in TraceClient::init() throws after start() has launched the sending thread, the catch (...) resets shared_ but leaves thread_ joinable. close() then returns early because there is no state to close, so ~TraceClient destroys a joinable std::thread, which calls std::terminate. That breaks the "Never throws" promise in the README in its strongest form: the program is aborted.

Where

  • init() (trace_client.hpp, the else branch after the opt-out checks) calls start(...), which assigns thread_ = std::thread(detail::drain, shared) and then shared_ = shared.
  • It then runs installId_ = explicitId;, a std::string copy-assignment that can throw std::bad_alloc.
  • The catch (...) at the end of init() runs shared_.reset(); installId_.clear(); reason_ = REASON_UNAVAILABLE; and does nothing with thread_.
  • close() returns as soon as it finds no shared state, so the thread is neither joined nor detached, and the sending thread waits forever on a Shared that nothing will close.

The only throwing operation found on this path is the bad_alloc on a copy of at most 255 bytes, so this is unlikely in practice. But it is the one place where the client can take the program down instead of dropping a report.

Possible direction

In the catch, stop the thread the same way close() does before dropping the state. For example, keep a local copy of shared_, set closing under its mutex and notify, then detach() (or join()) thread_. Alternatively, do the fallible install-ID work before start() so that nothing that can throw runs once the thread exists. A test could inject the failure only through allocation hooks, so the fix may have to be checked by review rather than by a test.

Found while reviewing PR #15, which does not touch this path.

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


drafted by Claude on behalf of Daniel Stephenson

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

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