Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions src/iocore/net/P_NetAccept.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "iocore/net/NetAcceptEventIO.h"
#include "Server.h"

#include <atomic>
#include <vector>

struct NetAccept;
Expand All @@ -60,21 +61,29 @@ AcceptFunction net_accept;

class UnixNetVConnection;

// TODO fix race between cancel accept and call back
struct NetAcceptAction : public Action, public RefCountObjInHeap {
Server *server;
std::atomic<Server *> server{nullptr};

void
cancel(Continuation *cont = nullptr) override
NetAcceptAction(Continuation *cont, Server *s)
{
Action::cancel(cont);
server->close();
continuation = cont;
if (cont != nullptr) {
mutex = cont->mutex;
}
server.store(s, std::memory_order_release);
}

Continuation *
operator=(Continuation *acont)
void
cancel(Continuation *cont = nullptr) override
{
return Action::operator=(acont);
// Close the server before setting the cancelled flag. This ensures that
// when acceptEvent() sees cancelled == true, the server close is already
// complete, preventing use-after-free races.
Server *s = server.exchange(nullptr, std::memory_order_acq_rel);
if (s != nullptr) {
s->close();
}
Action::cancel(cont);
}

~NetAcceptAction() override
Expand Down
4 changes: 1 addition & 3 deletions src/iocore/net/QUICNetProcessor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,7 @@ QUICNetProcessor::main_accept(Continuation *cont, SOCKET fd, AcceptOptions const
na->server.sock = UnixSocket{fd};
ats_ip_copy(&na->server.accept_addr, &accept_ip);

na->action_ = new NetAcceptAction();
*na->action_ = cont;
na->action_->server = &na->server;
na->action_ = new NetAcceptAction(cont, &na->server);
na->init_accept();

return na->action_.get();
Expand Down
5 changes: 4 additions & 1 deletion src/iocore/net/UnixNetAccept.cc
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ NetAccept::acceptEvent(int event, void *ep)
MUTEX_TRY_LOCK(lock, m, e->ethread);
if (lock.is_locked()) {
if (action_->cancelled) {
// Server was already closed by whoever called cancel().
e->cancel();
Metrics::Gauge::decrement(net_rsb.accepts_currently_open);
delete this;
Expand All @@ -487,6 +488,7 @@ NetAccept::acceptEvent(int event, void *ep)

int res;
if ((res = net_accept(this, e, false)) < 0) {
action_->cancel();
Metrics::Gauge::decrement(net_rsb.accepts_currently_open);
/* INKqa11179 */
Warning("Accept on port %d failed with error no %d", ats_ip_port_host_order(&server.addr), res);
Expand Down Expand Up @@ -637,7 +639,7 @@ NetAccept::acceptFastEvent(int event, void *ep)
return EVENT_CONT;

Lerror:
server.close();
action_->cancel();
e->cancel();
Metrics::Gauge::decrement(net_rsb.accepts_currently_open);
delete this;
Expand All @@ -656,6 +658,7 @@ NetAccept::acceptLoopEvent(int event, Event *e)
}

// Don't think this ever happens ...
action_->cancel();
Metrics::Gauge::decrement(net_rsb.accepts_currently_open);
delete this;
return EVENT_DONE;
Expand Down
4 changes: 1 addition & 3 deletions src/iocore/net/UnixNetProcessor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,7 @@ UnixNetProcessor::accept_internal(Continuation *cont, int fd, AcceptOptions cons
na->proxyPort = sa ? sa->proxyPort : nullptr;
na->snpa = dynamic_cast<SSLNextProtocolAccept *>(cont);

na->action_ = new NetAcceptAction();
*na->action_ = cont;
na->action_->server = &na->server;
na->action_ = new NetAcceptAction(cont, &na->server);

if (opt.frequent_accept) { // true
if (accept_threads > 0 && listen_per_thread == 0) {
Expand Down