Skip to content

Commit bf26a22

Browse files
Add exit-time destructor test
1 parent b6ec06b commit bf26a22

File tree

3 files changed

+62
-6
lines changed

3 files changed

+62
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ test/httplib.cc
1616
test/httplib.h
1717
test/test
1818
test/server_fuzzer
19+
test/test_no_exit_time_dtors
1920
test/test_proxy
2021
test/test_split
2122
test/test.xcodeproj/xcuser*

test/Makefile

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ ZLIB_SUPPORT = -DCPPHTTPLIB_ZLIB_SUPPORT -lz
1818
BROTLI_DIR = $(PREFIX)/opt/brotli
1919
BROTLI_SUPPORT = -DCPPHTTPLIB_BROTLI_SUPPORT -I$(BROTLI_DIR)/include -L$(BROTLI_DIR)/lib -lbrotlicommon -lbrotlienc -lbrotlidec
2020

21-
TEST_ARGS = gtest/src/gtest-all.cc gtest/src/gtest_main.cc -Igtest -Igtest/include $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) -pthread -lcurl
21+
TEST_ARGS = gtest_main.o gtest-all.o -Igtest -Igtest/include $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) -pthread -lcurl
2222

2323
# By default, use standalone_fuzz_target_runner.
2424
# This runner does no fuzzing, but simply executes the inputs
@@ -33,19 +33,30 @@ REALPATH = $(shell which grealpath 2>/dev/null || which realpath 2>/dev/null)
3333
STYLE_CHECK_FILES = $(filter-out httplib.h httplib.cc, \
3434
$(wildcard example/*.h example/*.cc fuzzing/*.h fuzzing/*.cc *.h *.cc ../httplib.h))
3535

36-
all : test test_split
36+
all : test test_no_exit_time_dtors test_split
3737
./test
38+
GTEST_FILTER="ExitTimeDtorsTest.*" ./test_no_exit_time_dtors
3839

3940
proxy : test_proxy
4041
./test_proxy
4142

42-
test : test.cc include_httplib.cc ../httplib.h Makefile cert.pem
43+
gtest-all.o : gtest/src/gtest-all.cc
44+
$(CXX) -c -o $@ $(CXXFLAGS) -Igtest -Igtest/include $<
45+
46+
gtest_main.o : gtest/src/gtest_main.cc
47+
$(CXX) -c -o $@ $(CXXFLAGS) -Igtest -Igtest/include $<
48+
49+
test : gtest_main.o gtest-all.o test.cc include_httplib.cc ../httplib.h Makefile cert.pem
4350
$(CXX) -o $@ -I.. $(CXXFLAGS) test.cc include_httplib.cc $(TEST_ARGS)
4451
@file $@
4552

53+
test_no_exit_time_dtors : gtest_main.o gtest-all.o test.cc ../httplib.h Makefile cert.pem
54+
$(CXX) -o $@ -I.. $(CXXFLAGS) -DCPPHTTPLIB_NO_EXIT_TIME_DESTRUCTORS \
55+
$(if $(findstring clang,$(CXX)),-Wexit-time-destructors -Werror=exit-time-destructors) test.cc $(TEST_ARGS)
56+
4657
# Note: The intention of test_split is to verify that it works to compile and
4758
# link the split httplib.h, so there is normally no need to execute it.
48-
test_split : test.cc ../httplib.h httplib.cc Makefile cert.pem
59+
test_split : gtest_main.o gtest-all.o test.cc ../httplib.h httplib.cc Makefile cert.pem
4960
$(CXX) -o $@ $(CXXFLAGS) test.cc httplib.cc $(TEST_ARGS)
5061

5162
check_abi:
@@ -73,7 +84,7 @@ style_check: $(STYLE_CHECK_FILES)
7384
echo "All files are properly formatted."; \
7485
fi
7586

76-
test_proxy : test_proxy.cc ../httplib.h Makefile cert.pem
87+
test_proxy : gtest_main.o gtest-all.o test_proxy.cc ../httplib.h Makefile cert.pem
7788
$(CXX) -o $@ -I.. $(CXXFLAGS) test_proxy.cc $(TEST_ARGS)
7889

7990
# Runs server_fuzzer.cc based on value of $(LIB_FUZZING_ENGINE).
@@ -98,5 +109,5 @@ cert.pem:
98109
./gen-certs.sh
99110

100111
clean:
101-
rm -rf test test_split test_proxy server_fuzzer *.pem *.0 *.o *.1 *.srl httplib.h httplib.cc _build* *.dSYM
112+
rm -rf test test_no_exit_time_dtors test_split test_proxy server_fuzzer *.pem *.0 *.o *.1 *.srl httplib.h httplib.cc _build* *.dSYM
102113

test/test.cc

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8470,3 +8470,47 @@ TEST(ClientInThreadTest, Issue2068) {
84708470
t.join();
84718471
}
84728472
}
8473+
8474+
Server *issue2097_svr = nullptr;
8475+
std::thread *issue2097_svr_thread = nullptr;
8476+
8477+
TEST(ExitTimeDtorsTest, Issue2097) {
8478+
ASSERT_EXIT(
8479+
{
8480+
issue2097_svr = new Server();
8481+
std::atexit([]() {
8482+
// Wait a bit before stopping server to simulate delayed exit
8483+
std::this_thread::sleep_for(std::chrono::milliseconds(200));
8484+
issue2097_svr->stop();
8485+
issue2097_svr_thread->join();
8486+
});
8487+
8488+
issue2097_svr_thread = new std::thread([]() {
8489+
issue2097_svr->Get(
8490+
"/hi", [](const Request & /*req*/, httplib::Response &res) {
8491+
res.set_content("Quack", "text/plain");
8492+
});
8493+
8494+
issue2097_svr->listen(HOST, PORT);
8495+
});
8496+
8497+
std::thread cli_thread([]() {
8498+
Client cli(HOST, PORT);
8499+
while (true) {
8500+
auto res = cli.Get("/hi");
8501+
std::this_thread::sleep_for(std::chrono::milliseconds(1));
8502+
}
8503+
});
8504+
8505+
std::thread([]() {
8506+
std::this_thread::sleep_for(std::chrono::milliseconds(200));
8507+
std::exit(42);
8508+
}).join();
8509+
},
8510+
#ifdef CPPHTTPLIB_NO_EXIT_TIME_DESTRUCTORS
8511+
::testing::ExitedWithCode(42),
8512+
#else
8513+
::testing::KilledBySignal(SIGSEGV),
8514+
#endif
8515+
"");
8516+
}

0 commit comments

Comments
 (0)