Skip to content

Commit 227d2c2

Browse files
fishpenguinyukun.yu
andauthored
Add EINTR and EAGAIN judge for accept (#1438)
* Add EINTR and EAGAIN judge for accept * Add EINTR signal tests * Cancel win32 and win64 compile on signal unittest Co-authored-by: yukun.yu <[email protected]>
1 parent 93e53c9 commit 227d2c2

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

httplib.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5507,6 +5507,8 @@ inline bool Server::listen_internal() {
55075507
// Try to accept new connections after a short sleep.
55085508
std::this_thread::sleep_for(std::chrono::milliseconds(1));
55095509
continue;
5510+
} else if (errno == EINTR || errno == EAGAIN) {
5511+
continue;
55105512
}
55115513
if (svr_sock_ != INVALID_SOCKET) {
55125514
detail::close_socket(svr_sock_);

test/test.cc

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <httplib.h>
2+
#include <signal.h>
23

34
#include <gtest/gtest.h>
45

@@ -943,6 +944,44 @@ TEST(UrlWithSpace, Redirect_Online) {
943944

944945
#endif
945946

947+
#if !defined(_WIN32) && !defined(_WIN64)
948+
TEST(ReceiveSignals, Signal) {
949+
auto setupSignalHandlers = []() {
950+
struct sigaction act;
951+
952+
sigemptyset(&act.sa_mask);
953+
act.sa_flags = SA_SIGINFO;
954+
act.sa_sigaction = [](int sig, siginfo_t *, void *) {
955+
switch (sig) {
956+
case SIGINT:
957+
default: break;
958+
}
959+
};
960+
::sigaction(SIGINT, &act, nullptr);
961+
};
962+
963+
Server svr;
964+
int port = 0;
965+
auto thread = std::thread([&]() {
966+
setupSignalHandlers();
967+
port = svr.bind_to_any_port("localhost");
968+
svr.listen_after_bind();
969+
});
970+
971+
while (!svr.is_running()) {
972+
std::this_thread::sleep_for(std::chrono::milliseconds(1));
973+
}
974+
975+
ASSERT_TRUE(svr.is_running());
976+
pthread_kill(thread.native_handle(), SIGINT);
977+
std::this_thread::sleep_for(std::chrono::milliseconds(100));
978+
ASSERT_TRUE(svr.is_running());
979+
svr.stop();
980+
thread.join();
981+
ASSERT_FALSE(svr.is_running());
982+
}
983+
#endif
984+
946985
TEST(RedirectToDifferentPort, Redirect) {
947986
Server svr1;
948987
svr1.Get("/1", [&](const Request & /*req*/, Response &res) {

0 commit comments

Comments
 (0)