From 50685bfb91aadaa255ebc47d885c53e8cbc55006 Mon Sep 17 00:00:00 2001 From: thc1006 <84045975+thc1006@users.noreply.github.com> Date: Fri, 7 Aug 2026 06:31:30 +0800 Subject: [PATCH] Cancel from the IO thread in the curl cancel cases ACancelBeforeTheResponseReportsCancelled cancelled from the test thread while the IO thread was driving the easy handle, and TSAN caught Abort's curl_easy_setopt against Curl_resolv. The client side is #4375. Nothing listens on 19937, so the connection fails and PerformCurlMessage dispatches ConnectFailed from the IO thread. Cancelling there still leaves the state short of Response with the abort flag raised, which is what the completion callback's WasAborted arm needs, and Abort now runs on the thread that owns the handle. Both cases record the thread they cancelled on and assert it is not the test thread, so a later edit cannot move it back without failing. --- ext/test/http/curl_http_test.cc | 45 +++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/ext/test/http/curl_http_test.cc b/ext/test/http/curl_http_test.cc index 5f11963d83..bfd9153743 100644 --- a/ext/test/http/curl_http_test.cc +++ b/ext/test/http/curl_http_test.cc @@ -95,15 +95,21 @@ class TerminalCountingHandler : public CustomEventHandler cancelled_from_callback_.fetch_add(1, std::memory_order_release); } } - else if (state == http_client::SessionState::Response && cancel_at_response_ != nullptr) + else if (state == cancel_at_ && cancel_target_ != nullptr) { - auto *session = cancel_at_response_; - cancel_at_response_ = nullptr; + auto *session = cancel_target_; + cancel_target_ = nullptr; + cancelled_from_ = std::this_thread::get_id(); session->CancelSession(); } } - http_client::Session *cancel_at_response_ = nullptr; + // Cancelling reaches curl_easy_setopt through Abort(), so it has to run on the thread that + // owns the handle. Both cases below cancel from an event the IO thread dispatches, and assert + // on cancelled_from_ so that a later edit cannot quietly move it back to the caller. + http_client::Session *cancel_target_ = nullptr; + http_client::SessionState cancel_at_ = http_client::SessionState::Response; + std::thread::id cancelled_from_{}; std::atomic terminal_count_{0}; std::atomic cancelled_from_callback_{0}; }; @@ -511,8 +517,9 @@ TEST_F(BasicCurlHttpTests, ACancelAfterTheResponseReportsOneOutcome) auto request = session->CreateRequest(); request->SetUri("get/"); - auto handler = std::make_shared(); - handler->cancel_at_response_ = session.get(); + auto handler = std::make_shared(); + handler->cancel_target_ = session.get(); + handler->cancel_at_ = http_client::SessionState::Response; session->SendRequest(handler); ASSERT_TRUE(waitForRequests(30, 1)); @@ -524,33 +531,33 @@ TEST_F(BasicCurlHttpTests, ACancelAfterTheResponseReportsOneOutcome) session_manager->FinishAllSessions(); } -// The other arm of the same callback, which nothing exercised. The server handler takes -// mtx_requests before it answers, so holding it keeps a response from racing the cancel, and the -// abort is torn down through Session::FinishOperation while the transfer is still in flight. +// The other arm of the same callback, which nothing exercised. Nothing listens on 19937, so the +// connection fails and PerformCurlMessage dispatches ConnectFailed from the IO thread. Cancelling +// there leaves the state short of Response with the abort flag raised, which is what the arm +// needs, and keeps Abort() on the thread that owns the easy handle. TEST_F(BasicCurlHttpTests, ACancelBeforeTheResponseReportsCancelled) { received_requests_.clear(); auto session_manager = std::make_shared()->Create(); EXPECT_TRUE(session_manager != nullptr); - auto session = session_manager->CreateSession("http://127.0.0.1:19000"); + auto session = session_manager->CreateSession("http://127.0.0.1:19937"); auto request = session->CreateRequest(); request->SetUri("get/"); - auto handler = std::make_shared(); + auto handler = std::make_shared(); + handler->cancel_target_ = session.get(); + handler->cancel_at_ = http_client::SessionState::ConnectFailed; - { - std::unique_lock lock_requests(mtx_requests); - session->SendRequest(handler); - session->CancelSession(); - session->FinishSession(); - } + session->SendRequest(handler); + session->FinishSession(); EXPECT_FALSE(handler->got_response_.load(std::memory_order_acquire)); // Counted by its empty reason so the assertion holds the arm this covers rather than whatever - // else reports a cancel. Two arrive in all, the other from Cleanup, and the total is left - // unasserted because that doubling is not what this change decides. + // else reports a cancel. EXPECT_EQ(1, handler->cancelled_from_callback_.load(std::memory_order_acquire)); + EXPECT_NE(handler->cancelled_from_, std::this_thread::get_id()) + << "the cancel has to run on the IO thread, see #4369"; session_manager->FinishAllSessions(); }