Skip to content

Commit 5cfda3c

Browse files
authored
[TEST] Cancel from the IO thread in the curl cancel cases (#4376)
1 parent 57bf864 commit 5cfda3c

1 file changed

Lines changed: 26 additions & 19 deletions

File tree

‎ext/test/http/curl_http_test.cc‎

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,21 @@ class TerminalCountingHandler : public CustomEventHandler
9595
cancelled_from_callback_.fetch_add(1, std::memory_order_release);
9696
}
9797
}
98-
else if (state == http_client::SessionState::Response && cancel_at_response_ != nullptr)
98+
else if (state == cancel_at_ && cancel_target_ != nullptr)
9999
{
100-
auto *session = cancel_at_response_;
101-
cancel_at_response_ = nullptr;
100+
auto *session = cancel_target_;
101+
cancel_target_ = nullptr;
102+
cancelled_from_ = std::this_thread::get_id();
102103
session->CancelSession();
103104
}
104105
}
105106

106-
http_client::Session *cancel_at_response_ = nullptr;
107+
// Cancelling reaches curl_easy_setopt through Abort(), so it has to run on the thread that
108+
// owns the handle. Both cases below cancel from an event the IO thread dispatches, and assert
109+
// on cancelled_from_ so that a later edit cannot quietly move it back to the caller.
110+
http_client::Session *cancel_target_ = nullptr;
111+
http_client::SessionState cancel_at_ = http_client::SessionState::Response;
112+
std::thread::id cancelled_from_{};
107113
std::atomic<int> terminal_count_{0};
108114
std::atomic<int> cancelled_from_callback_{0};
109115
};
@@ -511,8 +517,9 @@ TEST_F(BasicCurlHttpTests, ACancelAfterTheResponseReportsOneOutcome)
511517
auto request = session->CreateRequest();
512518
request->SetUri("get/");
513519

514-
auto handler = std::make_shared<TerminalCountingHandler>();
515-
handler->cancel_at_response_ = session.get();
520+
auto handler = std::make_shared<TerminalCountingHandler>();
521+
handler->cancel_target_ = session.get();
522+
handler->cancel_at_ = http_client::SessionState::Response;
516523

517524
session->SendRequest(handler);
518525
ASSERT_TRUE(waitForRequests(30, 1));
@@ -524,33 +531,33 @@ TEST_F(BasicCurlHttpTests, ACancelAfterTheResponseReportsOneOutcome)
524531
session_manager->FinishAllSessions();
525532
}
526533

527-
// The other arm of the same callback, which nothing exercised. The server handler takes
528-
// mtx_requests before it answers, so holding it keeps a response from racing the cancel, and the
529-
// abort is torn down through Session::FinishOperation while the transfer is still in flight.
534+
// The other arm of the same callback, which nothing exercised. Nothing listens on 19937, so the
535+
// connection fails and PerformCurlMessage dispatches ConnectFailed from the IO thread. Cancelling
536+
// there leaves the state short of Response with the abort flag raised, which is what the arm
537+
// needs, and keeps Abort() on the thread that owns the easy handle.
530538
TEST_F(BasicCurlHttpTests, ACancelBeforeTheResponseReportsCancelled)
531539
{
532540
received_requests_.clear();
533541
auto session_manager = std::make_shared<http_client::curl::HttpCurlClientFactory>()->Create();
534542
EXPECT_TRUE(session_manager != nullptr);
535543

536-
auto session = session_manager->CreateSession("http://127.0.0.1:19000");
544+
auto session = session_manager->CreateSession("http://127.0.0.1:19937");
537545
auto request = session->CreateRequest();
538546
request->SetUri("get/");
539547

540-
auto handler = std::make_shared<TerminalCountingHandler>();
548+
auto handler = std::make_shared<TerminalCountingHandler>();
549+
handler->cancel_target_ = session.get();
550+
handler->cancel_at_ = http_client::SessionState::ConnectFailed;
541551

542-
{
543-
std::unique_lock<std::mutex> lock_requests(mtx_requests);
544-
session->SendRequest(handler);
545-
session->CancelSession();
546-
session->FinishSession();
547-
}
552+
session->SendRequest(handler);
553+
session->FinishSession();
548554

549555
EXPECT_FALSE(handler->got_response_.load(std::memory_order_acquire));
550556
// Counted by its empty reason so the assertion holds the arm this covers rather than whatever
551-
// else reports a cancel. Two arrive in all, the other from Cleanup, and the total is left
552-
// unasserted because that doubling is not what this change decides.
557+
// else reports a cancel.
553558
EXPECT_EQ(1, handler->cancelled_from_callback_.load(std::memory_order_acquire));
559+
EXPECT_NE(handler->cancelled_from_, std::this_thread::get_id())
560+
<< "the cancel has to run on the IO thread, see #4369";
554561

555562
session_manager->FinishAllSessions();
556563
}

0 commit comments

Comments
 (0)