From 67d15ea6709103eaa824af6fd76c15fcc4d37494 Mon Sep 17 00:00:00 2001 From: klzgrad Date: Tue, 5 May 2026 16:10:35 +0800 Subject: [PATCH] Fix a race during successive accepted sockets --- src/net/tools/naive/naive_proxy.cc | 17 +++++++++-------- src/net/tools/naive/naive_proxy.h | 2 +- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/net/tools/naive/naive_proxy.cc b/src/net/tools/naive/naive_proxy.cc index 414537fab1..21ba8b51ec 100644 --- a/src/net/tools/naive/naive_proxy.cc +++ b/src/net/tools/naive/naive_proxy.cc @@ -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() { diff --git a/src/net/tools/naive/naive_proxy.h b/src/net/tools/naive/naive_proxy.h index 57e062a3b6..b31c002892 100644 --- a/src/net/tools/naive/naive_proxy.h +++ b/src/net/tools/naive/naive_proxy.h @@ -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 accepted_socket_; std::vector tunnel_ids_;