@@ -120,6 +120,37 @@ class CustomEventHandler : public http_client::EventHandler
120120// inside the Response event, which is the one moment both arms of the completion callback are
121121// eligible: DispatchEvent notifies the handler before it stores the new state, and the callback
122122// runs after both, so it sees an aborted operation that also has a response.
123+ class ReentrantSendHandler : public CustomEventHandler
124+ {
125+ public:
126+ void OnResponse (http_client::Response & /* response */ ) noexcept override
127+ {
128+ got_response_.store (true , std::memory_order_release);
129+
130+ if (session_ != nullptr && !resent_.exchange (true , std::memory_order_acq_rel))
131+ {
132+ auto again = session_->CreateRequest ();
133+ again->SetUri (" get/" );
134+ session_->SendRequest (self_.lock ());
135+ }
136+ }
137+
138+ void OnEvent (http_client::SessionState state, nostd::string_view /* reason */ ) noexcept override
139+ {
140+ if (state == http_client::SessionState::CreateFailed)
141+ {
142+ create_failed_.fetch_add (1 , std::memory_order_release);
143+ }
144+ }
145+
146+ http_client::Session *session_ = nullptr ;
147+ std::weak_ptr<ReentrantSendHandler> self_;
148+ std::atomic<int > create_failed_{0 };
149+
150+ private:
151+ std::atomic<bool > resent_{false };
152+ };
153+
123154class TerminalCountingHandler : public CustomEventHandler
124155{
125156public:
@@ -742,6 +773,63 @@ TEST_F(BasicCurlHttpTests, ResetMultiHandleWithASessionDoesNotDeadlock)
742773 client->FinishAllSessions ();
743774}
744775
776+ // The reproduction from #4396. A handler that sends again from OnResponse replaces the operation
777+ // whose Cleanup is running that handler, and Cleanup reads that operation again on the way out.
778+ TEST_F (BasicCurlHttpTests, ASecondRequestFromInsideTheResponseIsRefused)
779+ {
780+ received_requests_.clear ();
781+ auto session_manager = std::make_shared<http_client::curl::HttpCurlClientFactory>()->Create ();
782+ ASSERT_TRUE (session_manager != nullptr );
783+
784+ auto session = session_manager->CreateSession (" http://127.0.0.1:19000" );
785+ auto request = session->CreateRequest ();
786+ request->SetUri (" get/" );
787+
788+ auto handler = std::make_shared<ReentrantSendHandler>();
789+ handler->session_ = session.get ();
790+ handler->self_ = handler;
791+
792+ session->SendRequest (handler);
793+ ASSERT_TRUE (waitForRequests (30 , 1 ));
794+ session->FinishSession ();
795+
796+ EXPECT_TRUE (handler->got_response_ .load (std::memory_order_acquire));
797+ EXPECT_EQ (1 , handler->create_failed_ .load (std::memory_order_acquire));
798+
799+ session_manager->FinishAllSessions ();
800+ }
801+
802+ // The same thing without the re-entrancy, and the request the first send is reading from is not
803+ // replaced under it either.
804+ TEST_F (BasicCurlHttpTests, ASessionSendsOneRequest)
805+ {
806+ received_requests_.clear ();
807+ auto session_manager = std::make_shared<http_client::curl::HttpCurlClientFactory>()->Create ();
808+ ASSERT_TRUE (session_manager != nullptr );
809+
810+ auto session = session_manager->CreateSession (" http://127.0.0.1:19000" );
811+ auto request = session->CreateRequest ();
812+ request->SetUri (" get/" );
813+
814+ auto first = std::make_shared<ReentrantSendHandler>();
815+ session->SendRequest (first);
816+ ASSERT_TRUE (waitForRequests (30 , 1 ));
817+ session->FinishSession ();
818+
819+ EXPECT_TRUE (first->got_response_ .load (std::memory_order_acquire));
820+ EXPECT_EQ (0 , first->create_failed_ .load (std::memory_order_acquire));
821+
822+ auto again = session->CreateRequest ();
823+ EXPECT_EQ (request.get (), again.get ());
824+
825+ auto second = std::make_shared<ReentrantSendHandler>();
826+ session->SendRequest (second);
827+ EXPECT_EQ (1 , second->create_failed_ .load (std::memory_order_acquire));
828+ EXPECT_FALSE (second->got_response_ .load (std::memory_order_acquire));
829+
830+ session_manager->FinishAllSessions ();
831+ }
832+
745833// The caller-thread side of the same cancel. The server handler takes mtx_requests before it
746834// answers, so holding it keeps a response from racing the cancel and the abort lands while the
747835// IO thread is still driving the easy handle. That pairing is what #4369 caught.
0 commit comments