Skip to content

Commit ad25b64

Browse files
committed
fix cancel/accept race in NetAccept that causes EBADF abort
`NetAcceptAction::cancel()` closes the server socket before setting the cancelled flag In the window between close and flag set, `net_accept()` can get `EBADF` from `accept4()`, see `cancelled=false`, and dispatch `EVENT_ERROR` to handlers that don't expect it now we check the atomic server pointer in all three accept paths before dispatching `EVENT_ERROR`
1 parent bb4b904 commit ad25b64

1 file changed

Lines changed: 3 additions & 3 deletions

File tree

src/iocore/net/UnixNetAccept.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ net_accept(NetAccept *na, void *ep, bool blockable)
112112
if (res == -EAGAIN || res == -ECONNABORTED || res == -EPIPE) {
113113
goto Ldone;
114114
}
115-
if (na->server.sock.is_ok() && !na->action_->cancelled) {
115+
if (na->action_->server.load(std::memory_order_acquire) != nullptr && na->server.sock.is_ok() && !na->action_->cancelled) {
116116
if (!blockable) {
117117
na->action_->continuation->handleEvent(EVENT_ERROR, reinterpret_cast<void *>(res));
118118
} else {
@@ -387,7 +387,7 @@ NetAccept::do_blocking_accept(EThread *t)
387387
case -1:
388388
[[fallthrough]];
389389
default:
390-
if (!action_->cancelled) {
390+
if (action_->server.load(std::memory_order_acquire) != nullptr && !action_->cancelled) {
391391
SCOPED_MUTEX_LOCK(lock, action_->mutex ? action_->mutex : t->mutex, t);
392392
action_->continuation->handleEvent(EVENT_ERROR, reinterpret_cast<void *>(res));
393393
Warning("accept thread received fatal error: errno = %d", errno);
@@ -580,7 +580,7 @@ NetAccept::acceptFastEvent(int event, void *ep)
580580
check_transient_accept_error(res);
581581
goto Ldone;
582582
}
583-
if (!action_->cancelled) {
583+
if (action_->server.load(std::memory_order_acquire) != nullptr && !action_->cancelled) {
584584
action_->continuation->handleEvent(EVENT_ERROR, reinterpret_cast<void *>(res));
585585
}
586586
goto Lerror;

0 commit comments

Comments
 (0)