mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-06-10 16:47:14 +00:00
Make ThreadPool ctor exception-safe on partial thread creation (#2445)
* Make ThreadPool ctor exception-safe on partial thread creation If std::thread construction throws partway through the ThreadPool constructor (e.g., pthread_create returns EAGAIN under thread-resource pressure), the partially-built threads_ vector would destruct joinable std::thread objects, calling std::terminate(). Wrap the spawn loop and, on failure, signal shutdown to the workers already created, join them, and rethrow. Adds a reproducer test in test_thread_pool.cc that interposes pthread_create at link time to deterministically fail the second call, gated to POSIX + exceptions-enabled builds. Fix #2444 * Strip ASAN from test_thread_pool to coexist with pthread_create override Linux libasan installs its own pthread_create interceptor; our in-binary symbol override sits on top of it and corrupts ASAN's thread bookkeeping, which surfaces as "Joining already joined thread" on the very first test. Disable ASAN for this small unit-test binary -- ThreadPool memory behavior is still exercised under ASAN by the main `test` binary.
This commit is contained in:
@@ -202,8 +202,25 @@ test_split_no_tls : test.cc ../httplib.h httplib.cc Makefile
|
||||
$(CXX) -o $@ $(CXXFLAGS) test.cc httplib.cc $(TEST_ARGS_NO_TLS)
|
||||
|
||||
# ThreadPool unit tests (no TLS, no compression needed)
|
||||
#
|
||||
# The constructor-exception-safety reproducer test interposes pthread_create
|
||||
# at link time. The link flags below enable that interposition. ASAN is also
|
||||
# stripped from this target because libasan installs its own pthread_create
|
||||
# interceptor; layering our override on top corrupts ASAN's thread bookkeeping
|
||||
# and trips "Joining already joined thread" on Linux. ThreadPool memory
|
||||
# behavior is still covered by the ASAN-instrumented `test` binary.
|
||||
ifneq ($(OS), Windows_NT)
|
||||
ifeq ($(shell uname -s), Darwin)
|
||||
THREAD_POOL_INTERPOSE_LDFLAGS := -Wl,-flat_namespace
|
||||
else
|
||||
THREAD_POOL_INTERPOSE_LDFLAGS := -Wl,--export-dynamic
|
||||
endif
|
||||
endif
|
||||
|
||||
THREAD_POOL_CXXFLAGS := $(filter-out -fsanitize=address,$(CXXFLAGS))
|
||||
|
||||
test_thread_pool : test_thread_pool.cc ../httplib.h Makefile
|
||||
$(CXX) -o $@ -I.. $(CXXFLAGS) test_thread_pool.cc gtest/src/gtest-all.cc gtest/src/gtest_main.cc -Igtest -Igtest/include -lpthread
|
||||
$(CXX) -o $@ -I.. $(THREAD_POOL_CXXFLAGS) test_thread_pool.cc gtest/src/gtest-all.cc gtest/src/gtest_main.cc -Igtest -Igtest/include -lpthread $(THREAD_POOL_INTERPOSE_LDFLAGS)
|
||||
|
||||
check_abi:
|
||||
@./check-shared-library-abi-compatibility.sh
|
||||
|
||||
Reference in New Issue
Block a user