From 44515fc0c1b517fc18765fbec8d8fa0a61ebb554 Mon Sep 17 00:00:00 2001 From: Daniel McCoy Stephenson Date: Fri, 23 Oct 2026 20:37:49 -0600 Subject: [PATCH 1/2] Stop the sending thread when the constructor fails after starting it (#16) A throw in init() after start() reset shared_ but left thread_ joinable, so close() found nothing to close and the destructor called std::terminate. The catch now closes with no wait, which stops and detaches (or joins) the thread exactly as close() does. The test fails each allocation the constructor makes in turn, on the calling thread only, through a counting operator new in the test binary. Co-Authored-By: Claude Opus 5.5 --- test/test_trace_client.cpp | 47 ++++++++++++++++++++++++++++++++++++++ trace_client.hpp | 4 +++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/test/test_trace_client.cpp b/test/test_trace_client.cpp index 0f379eb..6ae1e7b 100644 --- a/test/test_trace_client.cpp +++ b/test/test_trace_client.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -60,6 +61,24 @@ static std::string currentTest; } \ } while (0) +// Counts down the calling thread's allocations and fails the one that finds +// it at zero, once; -1 never fails. Thread-local, so neither the sending +// thread nor a StubServer is affected. +static thread_local long allocationsUntilFailure = -1; + +void *operator new(std::size_t size) { + if (allocationsUntilFailure >= 0 && allocationsUntilFailure-- == 0) throw std::bad_alloc(); + void *p = std::malloc(size ? size : 1); + if (!p) throw std::bad_alloc(); + return p; +} + +void operator delete(void *p) noexcept { std::free(p); } + +#if __cplusplus >= 201402L +void operator delete(void *p, std::size_t) noexcept { std::free(p); } +#endif + static double secondsSince(std::chrono::steady_clock::time_point start) { return std::chrono::duration(std::chrono::steady_clock::now() - start).count(); } @@ -890,6 +909,33 @@ static void destructorClosesToo() { CHECK_EQ(1u, server.received().size()); } +static void everyAllocationTheConstructorMakesMayFailWithoutAborting() { + // Made before failures are armed; the ID is too long for the small-string + // buffer, so copying it in after the thread has started allocates. + const std::string base = "http://127.0.0.1:9", app = "MyGame", version = "1.2.3", key = "k"; + const std::string id(100, 'i'); + const trace_client::InstallId install = trace_client::InstallId::of(id); + for (long n = 0;; ++n) { + bool failed = false; + { + allocationsUntilFailure = n; + trace_client::TraceClient client(base, app, version, key, install); + failed = allocationsUntilFailure < 0; + allocationsUntilFailure = -1; + if (failed) { + CHECK(!client.isEnabled()); + CHECK_EQ(std::string("unavailable"), client.disabledReason()); + CHECK(client.installId().empty()); + client.report("startup"); + } else { + CHECK(client.isEnabled()); + CHECK_EQ(id, client.installId()); + } + } // a sending thread left joinable here would terminate the program + if (!failed) break; + } +} + static void closeReturnsWithinTheTimeoutWhenTheServerHangs() { StubServer server; server.hold(); // never answers while held @@ -1316,6 +1362,7 @@ int main() { {"queueIsBoundedAndDropsRatherThanGrows", queueIsBoundedAndDropsRatherThanGrows}, {"closeSendsWhatWasJustQueuedBeforeStopping", closeSendsWhatWasJustQueuedBeforeStopping}, {"destructorClosesToo", destructorClosesToo}, + {"everyAllocationTheConstructorMakesMayFailWithoutAborting", everyAllocationTheConstructorMakesMayFailWithoutAborting}, {"closeReturnsWithinTheTimeoutWhenTheServerHangs", closeReturnsWithinTheTimeoutWhenTheServerHangs}, {"closeIsPromptAndIdempotent", closeIsPromptAndIdempotent}, {"reportTagsEveryEventWithTheProgramVersionTrimmed", reportTagsEveryEventWithTheProgramVersionTrimmed}, diff --git a/trace_client.hpp b/trace_client.hpp index 8026f8f..ded7b83 100644 --- a/trace_client.hpp +++ b/trace_client.hpp @@ -1102,7 +1102,9 @@ class TraceClient { } } } catch (...) { - shared_.reset(); + // The sending thread may already be running: stop it as close() + // does, or the destructor would find it joinable and terminate. + close(0.0); installId_.clear(); reason_ = REASON_UNAVAILABLE; } From 2688720dab0d4aee159740ecd48054821c7bc64e Mon Sep 17 00:00:00 2001 From: Daniel McCoy Stephenson Date: Fri, 23 Oct 2026 20:39:52 -0600 Subject: [PATCH 2/2] Leave operator new alone under the address and thread sanitizers Clang's sanitizer runtimes define operator new in a static archive, so a replacement in the test binary fails to link there. Under them the allocation-failure test constructs once without failing anything. Co-Authored-By: Claude Opus 5.5 --- test/test_trace_client.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/test/test_trace_client.cpp b/test/test_trace_client.cpp index 6ae1e7b..313e2d9 100644 --- a/test/test_trace_client.cpp +++ b/test/test_trace_client.cpp @@ -63,9 +63,19 @@ static std::string currentTest; // Counts down the calling thread's allocations and fails the one that finds // it at zero, once; -1 never fails. Thread-local, so neither the sending -// thread nor a StubServer is affected. +// thread nor a StubServer is affected. Clang's address and thread sanitizer +// runtimes define operator new themselves, so there nothing is ever failed. static thread_local long allocationsUntilFailure = -1; +#if defined(__SANITIZE_ADDRESS__) || defined(__SANITIZE_THREAD__) +#define SANITIZER_OWNS_NEW +#elif defined(__has_feature) +#if __has_feature(address_sanitizer) || __has_feature(thread_sanitizer) +#define SANITIZER_OWNS_NEW +#endif +#endif + +#if !defined(SANITIZER_OWNS_NEW) void *operator new(std::size_t size) { if (allocationsUntilFailure >= 0 && allocationsUntilFailure-- == 0) throw std::bad_alloc(); void *p = std::malloc(size ? size : 1); @@ -78,6 +88,7 @@ void operator delete(void *p) noexcept { std::free(p); } #if __cplusplus >= 201402L void operator delete(void *p, std::size_t) noexcept { std::free(p); } #endif +#endif static double secondsSince(std::chrono::steady_clock::time_point start) { return std::chrono::duration(std::chrono::steady_clock::now() - start).count();