Repository navigation
[BUG] Report one outcome when a curl session is cancelled after the response arrives - #4363
Merged
marcalff merged 4 commits intoAug 5, 2026
Conversation
The async completion callback tested WasAborted() and a Response session state with two independent ifs, and the two are not exclusive. Cancelling only raises a flag, so a transfer that has already been answered still runs both arms, and a handler treating either as terminal counts one request twice. Only the transfer failing moves the state away from Sending, so a Response state means the server answered. That is the outcome worth reporting, and it is also the safe one: telling a caller its request was cancelled after the server accepted it invites a retry that writes the same batch again. Reachable through Shutdown, which calls CancelAllSessions while requests are in flight.
The comment claimed a request finishes once, which is wider than this change. A cancel arriving while the transfer fails still yields the dispatched failure state and then Cancelled; only the response case is made exclusive here.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4363 +/- ##
==========================================
+ Coverage 81.63% 81.77% +0.15%
==========================================
Files 493 493
Lines 19456 19456
==========================================
+ Hits 15880 15909 +29
+ Misses 3576 3547 -29
🚀 New features to boost your workflow:
|
Codecov put the patch at 50 per cent: nothing in the suite reached a cancel that gets as far as the callback, so the line reporting it never ran. The server handler takes mtx_requests before answering, so holding it keeps a response from racing the cancel while the abort is torn down through Session::FinishOperation. gcov on the changed lines: the else if runs 7 times and its body once, where the body was previously never reached. The case asserts at least one Cancelled rather than exactly one. It measures two, one dispatched by Cleanup while the state is still in flight and one from this callback, and that doubling is not what this change decides.
The previous assertion counted any Cancelled, so it stayed green whether or not the arm ran: Cleanup dispatches its own before the callback. It therefore covered the line without guarding it. GetCurlErrorMessage never yields an empty string, so Cleanup's Cancelled always carries a reason and the callback's never does. Counting the empty one names the arm exactly. Removing the arm turns the case red with 0 against 1, which the count alone did not do.
marcalff
approved these changes
Aug 5, 2026
marcalff
left a comment
Member
There was a problem hiding this comment.
LGTM, thanks for the fix and tests.
Member
Member
Author
|
Yes, tracking it. I put the detail on #4369 so it sits with the logs. Short of it: Fix is built and green locally. I asked over there which of two routes you'd rather have before I send it. |
1 task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of #4360, which lists three problems in the shared curl client's callback contract.
This is only the one with a single obvious answer. The other two need a decision I would
rather hear first, and they are still open on the issue.
What happens
Session::SendRequestpasses this completion callback toSendAsync:Two independent
ifs, and the two conditions are not exclusive.HttpOperation::Abortraises
is_aborted_and schedules the abort; it does not touch the session state. So atransfer that has already been answered, and then cancelled, satisfies both, and the handler
is told the request was cancelled and given its response.
The window is not narrow.
PerformCurlMessagedispatchesResponseand then callsCleanup, which is where the completion callback runs, and nothing between them issynchronised against
Abort.HttpClient::CancelAllSessionsis whatShutdowncalls, soshutting down while a response is arriving lands in it.
What it costs in tree
OtlpHttpClient::ResponseHandleralready guards against being finished twice: bothOnResponseand the terminalOnEventstates takestopping_with a compare and exchange,and only the winner calls
Unbind. Because the client deliversCancelledfirst, thewinner is the failure.
Unbindmoves the result callback out, so the response that followsfinds nothing to report and the export is recorded as failed while the server has it.
That is the reason to fix it here rather than in the consumers. A first writer guard cannot
pick the right outcome when it is handed the wrong one first.
Why the response wins
The state only reaches
Responsewhen the transfer succeeded: on a non-CURLE_OKresultPerformCurlMessagedispatchesConnectFailedorSendFailedfirst, which moves the stateout of the
Connecting/Connected/Sendingchain theResponsetransition needs. AResponsestate therefore means the server answered.That also makes it the safer of the two to report. Telling a caller its request was
cancelled when the server had already accepted it invites a retry, and for an exporter that
means writing the same batch twice.
Cancelledis unchanged for a cancel that lands while the request is still in flight, whichis the case it was written for.
What this leaves
The
Cancelledarm can still double up with whatCleanupsends.CleanupdispatchesCancelleditself whenever the state is stillCreated,Connecting,ConnectedorSending, and then runs this callback, so an operation aborted in flight and torn downthrough
Session::FinishOperationreportsCancelledtwice. The second case below measuresexactly that, two notifications for one request, which is why it asserts at least one rather
than one. An aborted operation whose transfer failed is the same shape, the failure state and
then
Cancelled.Each of those says one thing twice rather than two different things, and the consumers
collapse them:
OtlpHttpClientthroughstopping_, the Elasticsearch synchronous handlerthrough
recordCompletion, and its asynchronous handler not at all, which is #4338.Dropping this arm entirely would close those too, but that decides which layer owns the
event rather than correcting an outcome, and it is the third item on #4360. What this change
covers is the one pairing where the two notifications disagree and the wrong one wins.
Test
ACancelAfterTheResponseReportsOneOutcomecancels from inside the handler'sResponseevent. That is not a lucky interleaving:
DispatchEventnotifies the handler and only thenstores the new state, and the completion callback runs after both, so by the time it is
reached the operation is aborted and its state is
Response. Cancelling from the handleralso keeps
CancelSessionon the thread that owns the curl handle, which is where thebackground loop already is.
The handler counts terminal notifications and the case asserts one.
http_client_curl.ccreverted tomain, test keptExpected equality of these values: 1 ... Which is: 2ACancelBeforeTheResponseReportsCancelledcovers the other arm, which nothing in the suitereached, so the line that reports a cancelled request was never run and Codecov saw the patch
at 50 per cent. The server handler takes
mtx_requestsbefore it answers, so holding it keepsa response from racing the cancel, and the abort is torn down through
Session::FinishOperationwhile the transfer is still in flight.Two
Cancelledarrive, one fromCleanupand one from this callback, so counting them allwould stay green whether or not the arm ran.
GetCurlErrorMessagenever yields an emptystring, so
Cleanup's carries a reason and the callback's never does, and the case counts theempty one. Deleting the arm turns it red at 0 against 1. The total is deliberately not
asserted, since the doubling is the separate question above.
This one characterises rather than reproduces: it passes on
maintoo, where the same armruns from the first
if. What it adds is that the line cannot go uncovered again unnoticed.gcov on the changed lines, from the suite: the response test drives
OnResponse24 times, theelse ifis evaluated 7 times, and its body runs once, where before it ran not at all.21 of 21 in
curl_http_test, clang-format clean, and clang-tidy under the CI header filtersreports the same set as
mainfor both files.Checklist
CHANGELOG.mdupdated for non-trivial changesCancelledfollowed byOnResponsefor one request now receives onlyOnResponse. For theOTLP HTTP exporter that turns a shutdown racing a response from a reported failure into the
result the server actually returned.