Skip to content

Commit 71dbc35

Browse files
committed
Abort coroutine if setting main epoll flags fail
1 parent 3356985 commit 71dbc35

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

src/lib/lwan-thread.c

+22-16
Original file line numberDiff line numberDiff line change
@@ -565,10 +565,10 @@ conn_flags_to_epoll_events(enum lwan_connection_flags flags)
565565
return EPOLL_EVENTS(flags);
566566
}
567567

568-
static void update_epoll_flags(const struct lwan *lwan,
569-
struct lwan_connection *conn,
570-
int epoll_fd,
571-
enum lwan_connection_coro_yield yield_result)
568+
static int update_epoll_flags(const struct lwan *lwan,
569+
struct lwan_connection *conn,
570+
int epoll_fd,
571+
enum lwan_connection_coro_yield yield_result)
572572
{
573573
static const enum lwan_connection_flags or_mask[CONN_CORO_MAX] = {
574574
[CONN_CORO_YIELD] = 0,
@@ -611,14 +611,13 @@ static void update_epoll_flags(const struct lwan *lwan,
611611
assert((conn->flags & CONN_TLS) == (prev_flags & CONN_TLS));
612612

613613
if (LWAN_EVENTS(conn->flags) == LWAN_EVENTS(prev_flags))
614-
return;
614+
return 0;
615615

616616
struct epoll_event event = {.events = conn_flags_to_epoll_events(conn->flags),
617617
.data.ptr = conn};
618618
int fd = lwan_connection_get_fd(lwan, conn);
619619

620-
if (UNLIKELY(epoll_ctl(epoll_fd, EPOLL_CTL_MOD, fd, &event) < 0))
621-
lwan_status_perror("epoll_ctl");
620+
return epoll_ctl(epoll_fd, EPOLL_CTL_MOD, fd, &event);
622621
}
623622

624623
static void unasync_await_conn(void *data1, void *data2)
@@ -885,12 +884,16 @@ static ALWAYS_INLINE void resume_coro(struct timeout_queue *tq,
885884

886885
enum lwan_connection_coro_yield from_coro = coro_resume_value(
887886
conn_to_resume->coro, (int64_t)(intptr_t)conn_to_yield);
888-
if (UNLIKELY(from_coro == CONN_CORO_ABORT)) {
889-
timeout_queue_expire(tq, conn_to_resume);
890-
} else {
891-
update_epoll_flags(tq->lwan, conn_to_resume, epoll_fd, from_coro);
892-
timeout_queue_move_to_last(tq, conn_to_resume);
887+
if (LIKELY(from_coro != CONN_CORO_ABORT)) {
888+
int r =
889+
update_epoll_flags(tq->lwan, conn_to_resume, epoll_fd, from_coro);
890+
if (LIKELY(!r)) {
891+
timeout_queue_move_to_last(tq, conn_to_resume);
892+
return;
893+
}
893894
}
895+
896+
timeout_queue_expire(tq, conn_to_resume);
894897
}
895898

896899
static void update_date_cache(struct lwan_thread *thread)
@@ -950,15 +953,18 @@ static bool process_pending_timers(struct timeout_queue *tq,
950953
bool should_expire_timers = false;
951954

952955
while ((timeout = timeouts_get(t->wheel))) {
953-
struct lwan_request *request;
954-
955956
if (timeout == &tq->timeout) {
956957
should_expire_timers = true;
957958
continue;
958959
}
959960

960-
request = container_of(timeout, struct lwan_request, timeout);
961-
update_epoll_flags(tq->lwan, request->conn, epoll_fd, CONN_CORO_RESUME);
961+
struct lwan_request *request =
962+
container_of(timeout, struct lwan_request, timeout);
963+
int r = update_epoll_flags(tq->lwan, request->conn, epoll_fd,
964+
CONN_CORO_RESUME);
965+
if (UNLIKELY(r < 0)) {
966+
timeout_queue_expire(tq, request->conn);
967+
}
962968
}
963969

964970
if (should_expire_timers) {

0 commit comments

Comments
 (0)