Fix a race during successive accepted sockets

pull/792/head
klzgrad 2026-05-05 16:10:35 +08:00
parent 2cfaf950f7
commit 67d15ea670
2 changed files with 10 additions and 9 deletions

View File

@ -88,25 +88,20 @@ NaiveProxy::~NaiveProxy() = default;
void NaiveProxy::DoAcceptLoop() {
int result;
accept_loop_needs_restart_ = false;
do {
result = listen_socket_->Accept(
&accepted_socket_, base::BindOnce(&NaiveProxy::OnAcceptComplete,
weak_ptr_factory_.GetWeakPtr()));
if (result == ERR_IO_PENDING) {
accept_loop_needs_restart_ = true;
return;
}
HandleAcceptResult(result);
OnAcceptComplete(result);
} while (result == OK);
}
void NaiveProxy::OnAcceptComplete(int result) {
HandleAcceptResult(result);
if (result == OK) {
DoAcceptLoop();
}
}
void NaiveProxy::HandleAcceptResult(int result) {
if (result != OK) {
LOG(ERROR) << "Accept error: " << ErrorToShortString(result);
return;
@ -135,6 +130,9 @@ void NaiveProxy::HandleAcceptResult(int result) {
url_getter_->StartOne();
}
DoConnect();
if (accept_loop_needs_restart_) {
DoAcceptLoop();
}
}
}
@ -144,6 +142,9 @@ void NaiveProxy::OnPreambleComplete(int result) {
return;
}
DoConnect();
if (accept_loop_needs_restart_) {
DoAcceptLoop();
}
}
void NaiveProxy::DoConnect() {

View File

@ -56,7 +56,6 @@ class NaiveProxy {
void DoAcceptLoop();
void OnAcceptComplete(int result);
void HandleAcceptResult(int result);
void OnPreambleComplete(int result);
@ -90,6 +89,7 @@ class NaiveProxy {
unsigned int last_id_;
bool accept_loop_needs_restart_ = false;
std::unique_ptr<StreamSocket> accepted_socket_;
std::vector<TunnelId> tunnel_ids_;