More error handling

Handle epoll_ctl errors by closing the connection
This commit is contained in:
2025-08-18 15:59:35 -04:00
parent 141ae823ab
commit 8b18c5fde9

View File

@@ -301,11 +301,10 @@ int main(int argc, char *argv[]) {
int networkThreads = config->server.network_threads; int networkThreads = config->server.network_threads;
for (int i = 0; i < networkThreads; ++i) { for (int i = 0; i < networkThreads; ++i) {
threads.emplace_back([network_epollfd, i, threads.emplace_back(
max_request_size = [network_epollfd, i,
config->server.max_request_size_bytes, max_request_size = config->server.max_request_size_bytes,
event_batch_size = event_batch_size = config->server.event_batch_size]() {
config->server.event_batch_size]() {
pthread_setname_np(pthread_self(), pthread_setname_np(pthread_self(),
("network-" + std::to_string(i)).c_str()); ("network-" + std::to_string(i)).c_str());
std::vector<struct epoll_event> events(event_batch_size); std::vector<struct epoll_event> events(event_batch_size);
@@ -331,7 +330,8 @@ int main(int argc, char *argv[]) {
std::unique_ptr<Connection> conn{ std::unique_ptr<Connection> conn{
static_cast<Connection *>(events[i].data.ptr)}; static_cast<Connection *>(events[i].data.ptr)};
conn->tsan_acquire(); conn->tsan_acquire();
events[i].data.ptr = nullptr; // Clear epoll pointer (we own it now) events[i].data.ptr =
nullptr; // Clear epoll pointer (we own it now)
const int fd = conn->fd; const int fd = conn->fd;
if (events[i].events & (EPOLLERR | EPOLLHUP | EPOLLRDHUP)) { if (events[i].events & (EPOLLERR | EPOLLHUP | EPOLLRDHUP)) {
@@ -358,11 +358,14 @@ int main(int argc, char *argv[]) {
} }
// Transfer ownership back to epoll: unique_ptr -> raw pointer // Transfer ownership back to epoll: unique_ptr -> raw pointer
conn->tsan_release(); conn->tsan_release();
events[i].data.ptr = conn.release(); // epoll now owns the connection Connection *raw_conn =
conn.release(); // Get raw pointer before epoll_ctl
events[i].data.ptr = raw_conn; // epoll now owns the connection
int e = epoll_ctl(network_epollfd, EPOLL_CTL_MOD, fd, &events[i]); int e = epoll_ctl(network_epollfd, EPOLL_CTL_MOD, fd, &events[i]);
if (e == -1) { if (e == -1) {
perror("epoll_ctl"); perror("epoll_ctl");
abort(); // Connection leaked on abort() is acceptable - OS cleanup delete raw_conn; // Clean up connection on epoll failure
continue;
} }
} }
} }
@@ -454,13 +457,15 @@ int main(int argc, char *argv[]) {
struct epoll_event event{}; struct epoll_event event{};
event.events = EPOLLIN | EPOLLONESHOT | EPOLLRDHUP; event.events = EPOLLIN | EPOLLONESHOT | EPOLLRDHUP;
conn->tsan_release(); conn->tsan_release();
Connection *raw_conn =
conn.release(); // Get raw pointer before epoll_ctl
event.data.ptr = event.data.ptr =
conn.release(); // network epoll now owns the connection raw_conn; // network epoll now owns the connection
int e = epoll_ctl(network_epollfd, EPOLL_CTL_ADD, fd, &event); int e = epoll_ctl(network_epollfd, EPOLL_CTL_ADD, fd, &event);
if (e == -1) { if (e == -1) {
perror("epoll_ctl"); perror("epoll_ctl");
abort(); // Connection leaked on abort() is acceptable - OS delete raw_conn; // Clean up connection on epoll failure
// cleanup continue;
} }
} }
} }