From 5b92ad659e5c31401f90d2ef16d3763c1c0bb903 Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Tue, 17 Nov 2026 09:55:05 -0800 Subject: [PATCH 1/4] Limit Darwin TSan instrumentation around native thread-local errno access --- ixwebsocket/IXSocket.cpp | 8 ++++++++ test/IXSocketTest.cpp | 24 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/ixwebsocket/IXSocket.cpp b/ixwebsocket/IXSocket.cpp index d60f1657..8a484c82 100644 --- a/ixwebsocket/IXSocket.cpp +++ b/ixwebsocket/IXSocket.cpp @@ -306,6 +306,11 @@ namespace ix #endif } + // Darwin TSan can confuse reused per-thread errno storage with shared memory. + // These accessors touch only OS thread-local error state; keep callers instrumented. +#if defined(__APPLE__) && defined(__clang__) + __attribute__((no_sanitize("thread"))) +#endif int Socket::getErrno() { int err; @@ -319,6 +324,9 @@ namespace ix return err; } +#if defined(__APPLE__) && defined(__clang__) + __attribute__((no_sanitize("thread"))) +#endif void Socket::setErrno(int err) { #ifdef _WIN32 diff --git a/test/IXSocketTest.cpp b/test/IXSocketTest.cpp index 740090c1..fdc22d98 100644 --- a/test/IXSocketTest.cpp +++ b/test/IXSocketTest.cpp @@ -13,6 +13,7 @@ #include #include #include +#include using namespace ix; @@ -107,6 +108,29 @@ TEST_CASE("Empty and cancelled socket reads do not receive data", "[socket][read CHECK(socket.reads == 0); } +TEST_CASE("Socket error state remains local when threads are reused", "[socket][errno]") +{ + std::atomic isolated {true}; + auto churn = [&](int error) + { + for (int i = 0; i < 16; ++i) + { + std::thread worker( + [&, error] + { + Socket::setErrno(error); + if (Socket::getErrno() != error) isolated = false; + }); + worker.join(); + } + }; + std::thread a(churn, EWOULDBLOCK); + std::thread b(churn, EINVAL); + a.join(); + b.join(); + CHECK(isolated.load()); +} + namespace ix { void testSocket(const std::string& host, From d05eac3a05b1841bc489d6687f00d3ce449a5198 Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Tue, 17 Nov 2026 11:11:02 -0800 Subject: [PATCH 2/4] Decouple DNS worker lifetime and resolve numeric addresses inline --- ixwebsocket/IXDNSLookup.cpp | 52 +++++++++++++++++++++++++++---------- ixwebsocket/IXDNSLookup.h | 8 +++--- test/IXDNSLookupTest.cpp | 19 ++++++++++++++ 3 files changed, 60 insertions(+), 19 deletions(-) diff --git a/ixwebsocket/IXDNSLookup.cpp b/ixwebsocket/IXDNSLookup.cpp index 8eef0ac8..1ada3dd4 100644 --- a/ixwebsocket/IXDNSLookup.cpp +++ b/ixwebsocket/IXDNSLookup.cpp @@ -41,6 +41,16 @@ #endif #endif +namespace +{ + bool isNumericHost(const std::string& hostname) + { + struct in6_addr address; + return ix::inet_pton(AF_INET, hostname.c_str(), &address) == 1 || + ix::inet_pton(AF_INET6, hostname.c_str(), &address) == 1; + } +} // namespace + namespace ix { const int64_t DNSLookup::kDefaultWait = 1; // ms @@ -72,9 +82,7 @@ namespace ix // the whole process inside getaddrinfo (issue #560, reproduced with // "127.0.0.1") and that also fails to resolve loopback when the machine is // offline (issue #524). - struct in6_addr dummy; - bool numericHost = ix::inet_pton(AF_INET, hostname.c_str(), &dummy) == 1 || - ix::inet_pton(AF_INET6, hostname.c_str(), &dummy) == 1; + const bool numericHost = isNumericHost(hostname); if (numericHost) { hints.ai_flags |= AI_NUMERICHOST; @@ -111,6 +119,18 @@ namespace ix const CancellationRequest& isCancellationRequested, bool cancellable) { + // AI_NUMERICHOST | AI_NUMERICSERV performs no name/service lookup, and + // getAddrInfo deliberately omits the interface-querying AI_ADDRCONFIG here. + if (isNumericHost(_hostname)) + { + errMsg.clear(); + if (isCancellationRequested && isCancellationRequested()) + { + errMsg = "cancellation requested"; + return nullptr; + } + return getAddrInfo(_hostname, _port, errMsg); + } return cancellable ? resolveCancellable(errMsg, isCancellationRequested) : resolveUnCancellable(errMsg, isCancellationRequested); } @@ -121,7 +141,7 @@ namespace ix errMsg = "no error"; // Maybe a cancellation request got in before the background thread terminated ? - if (isCancellationRequested()) + if (isCancellationRequested && isCancellationRequested()) { errMsg = "cancellation requested"; return nullptr; @@ -135,6 +155,12 @@ namespace ix { errMsg = "no error"; + if (isCancellationRequested && isCancellationRequested()) + { + errMsg = "cancellation requested"; + return nullptr; + } + // Can only be called once, otherwise we would have to manage a pool // of background thread which is overkill for our usage. if (_done) @@ -153,11 +179,9 @@ namespace ix int port = _port; std::string hostname(_hostname); - // We make the background thread doing the work a shared pointer - // instead of a member variable, because it can keep running when - // this object goes out of scope, in case of cancellation - auto t = std::make_shared(&DNSLookup::run, this, self, hostname, port); - t->detach(); + // The detached worker owns its arguments and only accesses the lookup + // after acquiring a strong reference from self. + std::thread(&DNSLookup::run, self, hostname, port).detach(); while (!_done) { @@ -167,7 +191,7 @@ namespace ix std::this_thread::sleep_for(std::chrono::milliseconds(_wait)); // Were we cancelled ? - if (isCancellationRequested()) + if (isCancellationRequested && isCancellationRequested()) { errMsg = "cancellation requested"; return nullptr; @@ -175,7 +199,7 @@ namespace ix } // Maybe a cancellation request got in before the bg terminated ? - if (isCancellationRequested()) + if (isCancellationRequested && isCancellationRequested()) { errMsg = "cancellation requested"; return nullptr; @@ -198,10 +222,10 @@ namespace ix if (auto lock = self.lock()) { // Copy result into the member variables - setRes(res); - setErrMsg(errMsg); + lock->setRes(res); + lock->setErrMsg(errMsg); - _done = true; + lock->_done = true; } } diff --git a/ixwebsocket/IXDNSLookup.h b/ixwebsocket/IXDNSLookup.h index 246911cf..bc85a9df 100644 --- a/ixwebsocket/IXDNSLookup.h +++ b/ixwebsocket/IXDNSLookup.h @@ -4,7 +4,7 @@ * Copyright (c) 2018 Machine Zone, Inc. All rights reserved. * * Resolve a hostname+port to a struct addrinfo obtained with getaddrinfo - * Does this in a background thread so that it can be cancelled, since + * Numeric addresses resolve inline. Hostnames use a cancellable worker, since * getaddrinfo is a blocking call, and we don't want to block the main thread on Mobile. */ @@ -40,11 +40,9 @@ namespace ix AddrInfoPtr resolveUnCancellable(std::string& errMsg, const CancellationRequest& isCancellationRequested); - AddrInfoPtr getAddrInfo(const std::string& hostname, - int port, - std::string& errMsg); + static AddrInfoPtr getAddrInfo(const std::string& hostname, int port, std::string& errMsg); - void run(std::weak_ptr self, std::string hostname, int port); // thread runner + static void run(std::weak_ptr self, std::string hostname, int port); void setErrMsg(const std::string& errMsg); const std::string& getErrMsg(); diff --git a/test/IXDNSLookupTest.cpp b/test/IXDNSLookupTest.cpp index d5060a89..4129a674 100644 --- a/test/IXDNSLookupTest.cpp +++ b/test/IXDNSLookupTest.cpp @@ -46,3 +46,22 @@ TEST_CASE("dns", "[net]") REQUIRE(res == nullptr); } } + +TEST_CASE("Numeric addresses do not require a DNS worker", "[net][numeric_dns]") +{ + const std::string host = GENERATE(std::string("127.0.0.1"), std::string("::1")); + DNSLookup lookup(host, 80); + std::string error; + auto result = lookup.resolve(error, CancellationRequest()); + REQUIRE(result != nullptr); + CHECK(error.empty()); + CHECK(result->ai_family == (host == "::1" ? AF_INET6 : AF_INET)); +} + +TEST_CASE("Already cancelled DNS lookups do not start a worker", "[net][numeric_dns]") +{ + DNSLookup lookup("ixwebsocket-test.invalid.", 80); + std::string error; + CHECK(lookup.resolve(error, [] { return true; }) == nullptr); + CHECK(error == "cancellation requested"); +} From e009e7158c390a3dc3a1a552e838231d30d5f8d6 Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Tue, 17 Nov 2026 11:17:56 -0800 Subject: [PATCH 3/4] Preserve cancellation checks around numeric address parsing --- ixwebsocket/IXDNSLookup.cpp | 8 +++++++- test/IXDNSLookupTest.cpp | 10 ++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/ixwebsocket/IXDNSLookup.cpp b/ixwebsocket/IXDNSLookup.cpp index 1ada3dd4..070a46a9 100644 --- a/ixwebsocket/IXDNSLookup.cpp +++ b/ixwebsocket/IXDNSLookup.cpp @@ -129,7 +129,13 @@ namespace ix errMsg = "cancellation requested"; return nullptr; } - return getAddrInfo(_hostname, _port, errMsg); + auto result = getAddrInfo(_hostname, _port, errMsg); + if (isCancellationRequested && isCancellationRequested()) + { + errMsg = "cancellation requested"; + return nullptr; + } + return result; } return cancellable ? resolveCancellable(errMsg, isCancellationRequested) : resolveUnCancellable(errMsg, isCancellationRequested); diff --git a/test/IXDNSLookupTest.cpp b/test/IXDNSLookupTest.cpp index 4129a674..42526657 100644 --- a/test/IXDNSLookupTest.cpp +++ b/test/IXDNSLookupTest.cpp @@ -65,3 +65,13 @@ TEST_CASE("Already cancelled DNS lookups do not start a worker", "[net][numeric_ CHECK(lookup.resolve(error, [] { return true; }) == nullptr); CHECK(error == "cancellation requested"); } + +TEST_CASE("Numeric resolution preserves cancellation after parsing", "[net][numeric_dns]") +{ + DNSLookup lookup("127.0.0.1", 80); + unsigned checks = 0; + std::string error; + CHECK(lookup.resolve(error, [&] { return ++checks > 1; }) == nullptr); + CHECK(checks == 2); + CHECK(error == "cancellation requested"); +} From de50f6948e7aefaa4148398d948a2532bd802ee9 Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Tue, 17 Nov 2026 11:34:43 -0800 Subject: [PATCH 4/4] Expose an opt-in synchronous DNS workaround for static glibc builds --- CMakeLists.txt | 5 +++++ docs/build.md | 1 + ixwebsocket/IXDNSLookup.cpp | 3 +++ ixwebsocket/IXDNSLookup.h | 2 +- test/IXDNSLookupTest.cpp | 8 ++++++++ 5 files changed, 18 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b32d3d0d..6b1b4830 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -146,6 +146,7 @@ set( IXWEBSOCKET_HEADERS option(BUILD_SHARED_LIBS "Build shared libraries (.dll/.so) instead of static ones (.lib/.a)" OFF) option(USE_TLS "Enable TLS support" FALSE) +option(USE_ASYNC_DNS "Resolve hostnames on a cancellable background thread" ON) option(USE_NETWORK_FRAMEWORK "Use Apple's Network.framework TLS backend (macOS 10.15+)" OFF) if (USE_NETWORK_FRAMEWORK) if (NOT APPLE OR NOT CMAKE_SYSTEM_NAME STREQUAL "Darwin") @@ -229,6 +230,10 @@ else() ) endif() +if (NOT USE_ASYNC_DNS) + target_compile_definitions(ixwebsocket PRIVATE IXWEBSOCKET_USE_SYNC_DNS) +endif() + set(IXWEBSOCKET_PRIVATE_LIBS "") include(CheckAtomic) diff --git a/docs/build.md b/docs/build.md index 72f55d27..a034a24a 100644 --- a/docs/build.md +++ b/docs/build.md @@ -75,6 +75,7 @@ Options for building: * `-DUSE_NETWORK_FRAMEWORK=ON` enables the experimental native macOS TLS backend and also enables `USE_TLS`. It requires macOS 10.15 or later and links Apple's Network, Security, and CoreFoundation frameworks, with no OpenSSL library dependency. It cannot be combined with another TLS backend; the make target below overrides cached backend selections. Client and server TLS, including mutual TLS, are supported. The existing Apple backend remains the default. * `-DUSE_MBED_TLS=1` will use [mbedlts](https://tls.mbed.org/) for the TLS support * `-DUSE_WS=1` will build the ws interactive command line tool +* `-DUSE_ASYNC_DNS=OFF` resolves hostnames synchronously, for static-glibc deployments that cannot use the default resolver worker. This is an opt-in workaround: hostname resolution cannot be cancelled until the system resolver returns, so `stop()` can wait for it. The default is `ON`. Numeric IP addresses never need a DNS worker. * `-DUSE_TEST=1` will build the unittest * `-DIXWEBSOCKET_WARNINGS_AS_ERRORS=ON` treats library and CLI compiler warnings as errors. It is off by default; `make -f makefile.dev test WARNINGS_AS_ERRORS=ON` enables it and builds both targets, as used by Linux CI. Unix GCC/Clang builds enable `-Wshadow`. Warning flags are private to these targets and do not apply to vendored dependencies or consumers. diff --git a/ixwebsocket/IXDNSLookup.cpp b/ixwebsocket/IXDNSLookup.cpp index 070a46a9..9f829a44 100644 --- a/ixwebsocket/IXDNSLookup.cpp +++ b/ixwebsocket/IXDNSLookup.cpp @@ -137,6 +137,9 @@ namespace ix } return result; } +#ifdef IXWEBSOCKET_USE_SYNC_DNS + cancellable = false; +#endif return cancellable ? resolveCancellable(errMsg, isCancellationRequested) : resolveUnCancellable(errMsg, isCancellationRequested); } diff --git a/ixwebsocket/IXDNSLookup.h b/ixwebsocket/IXDNSLookup.h index bc85a9df..1f53d117 100644 --- a/ixwebsocket/IXDNSLookup.h +++ b/ixwebsocket/IXDNSLookup.h @@ -4,7 +4,7 @@ * Copyright (c) 2018 Machine Zone, Inc. All rights reserved. * * Resolve a hostname+port to a struct addrinfo obtained with getaddrinfo - * Numeric addresses resolve inline. Hostnames use a cancellable worker, since + * Numeric addresses resolve inline. Hostnames normally use a cancellable worker, since * getaddrinfo is a blocking call, and we don't want to block the main thread on Mobile. */ diff --git a/test/IXDNSLookupTest.cpp b/test/IXDNSLookupTest.cpp index 42526657..23a1d613 100644 --- a/test/IXDNSLookupTest.cpp +++ b/test/IXDNSLookupTest.cpp @@ -75,3 +75,11 @@ TEST_CASE("Numeric resolution preserves cancellation after parsing", "[net][nume CHECK(checks == 2); CHECK(error == "cancellation requested"); } + +TEST_CASE("Synchronous hostname resolution accepts an empty cancellation callback", + "[net][numeric_dns]") +{ + DNSLookup lookup("localhost", 80); + std::string error; + CHECK(lookup.resolve(error, CancellationRequest(), false) != nullptr); +}