Skip to content

fix cancel/accept race in NetAccept that causes EBADF abort - #12925

Closed
JakeChampion wants to merge 2 commits into
apache:masterfrom
JakeChampion:jake/ccc
Closed

JakeChampion wants to merge 2 commits into
apache:masterfrom
JakeChampion:jake/ccc

Conversation

@JakeChampion

Copy link
Copy Markdown
Contributor

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

@JakeChampion
JakeChampion force-pushed the jake/ccc branch 3 times, most recently from 27c9a8a to b259ce1 Compare February 27, 2026 14:43
`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`

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a race condition between NetAcceptAction::cancel() and the net_accept() accept paths that could cause an unhandled EBADF abort. The cancel operation atomically closes the server socket (via server.exchange(nullptr, ...)) before setting the cancelled flag, leaving a window where an accept call can fail with EBADF, see cancelled == false, and spuriously dispatch EVENT_ERROR to a continuation that does not expect it.

Changes:

  • Adds an atomic std::atomic<Server *> server check (action_->server.load(std::memory_order_acquire) != nullptr) as the primary guard before dispatching EVENT_ERROR in all three accept code paths (net_accept, do_blocking_accept, acceptFastEvent).
  • The NetAcceptAction::cancel() implementation (in P_NetAccept.h) uses server.exchange(nullptr, std::memory_order_acq_rel) so the null pointer state serves as a reliable cancellation indicator before cancelled is set.

Comment thread src/iocore/net/UnixNetAccept.cc Outdated
@zwoop

zwoop commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

[approve ci]

@JakeChampion
JakeChampion marked this pull request as ready for review June 5, 2026 18:22
@JosiahWI
JosiahWI self-requested a review June 9, 2026 12:39
@JosiahWI

JosiahWI commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

I think this maybe got missed in our PR scrub somehow. I'm putting it on my list to review.

@JosiahWI

JosiahWI commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

@JakeChampion Have you considered the Copilot post? Was it intentional to leave the is_ok call on line 115?

@JakeChampion

Copy link
Copy Markdown
Contributor Author

@JakeChampion Have you considered the Copilot post? Was it intentional to leave the is_ok call on line 115?

apologies, it was not intentional, i've pushed another commit now 👍

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 3 comments.

Comment thread src/iocore/net/UnixNetAccept.cc
Comment thread src/iocore/net/UnixNetAccept.cc
Comment thread src/iocore/net/UnixNetAccept.cc

@JosiahWI JosiahWI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please review Copilot's comments and resolve with a comment if they are incorrect.

@JosiahWI

Copy link
Copy Markdown
Contributor

@JakeChampion I see that you replied to Copilot with "applied". Were you intending to push changes?

@JakeChampion

Copy link
Copy Markdown
Contributor Author

@JakeChampion I see that you replied to Copilot with "applied". Were you intending to push changes?

I did push but it looks like github is confused about it
image

@JakeChampion

Copy link
Copy Markdown
Contributor Author

I'll open a new PR for this to see if that helps

@JakeChampion

Copy link
Copy Markdown
Contributor Author

#13702

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants