Repository navigation
[BUG] Stop OTLP ForceFlush returning on the first notification and overrunning its deadline - #4357
Conversation
c2d3c4c to
db7ebb9
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4357 +/- ##
==========================================
+ Coverage 81.91% 81.91% +0.01%
==========================================
Files 494 494
Lines 19654 19658 +4
==========================================
+ Hits 16097 16101 +4
Misses 3557 3557
🚀 New features to boost your workflow:
|
|
The one red job is a Docker Hub timeout in the functional test setup, before anything The same job passed on the previous commit of this branch, It is also not the I do not have re-run rights on the repository. Happy to force-push the same tree to |
db7ebb9 to
6015be8
Compare
6015be8 to
1e19483
Compare
…errunning its deadline OtlpGrpcClient::ForceFlush left its wait loop on any notification without re-reading the completion counter, so one finished request reported the whole flush as complete. The loop condition is the only place that counter is read, and the break skipped it. Drop the break, let the loop condition decide, and return that condition instead of the leftover duration. Both clients waited for the configured export timeout on every iteration, so ForceFlush could block well past the deadline it was given. Bound each wait by the caller's remaining time as well. OtlpHttpClient::Shutdown calls ForceFlush(1ms) in a loop, so shutdown cost a multiple of the export timeout. The wait stays bounded and the condition is re-read on every wakeup, which is what covers a notification lost between the check and the wait. ForceFlushReturnsWithinTheCallerDeadline holds a request open with the nosend HTTP client and asserts the call returns inside its own deadline rather than the client timeout. Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com>
1e19483 to
1d4bf6c
Compare
The mock stub ran the completion callback inline, so a request was always finished before ForceFlush() could be called and the loop under test never had anything to wait for. Holding the callback leaves the request in flight, which is all the two cases need; the counters stay private to the .cc. ForceFlushReturnsWithinTheCallerDeadline asks for 50 ms against the 10 s export timeout. ForceFlushKeepsWaitingWhenAnotherRequestCompletes leaves two requests in flight and completes one while the flush is parked, which is the notification that used to end the wait. Registration is by source text, so both cases exist in every build and opt out in the fixture rather than being compiled away.
The empty running_sessions_ short circuit could not change the answer. ReleaseSession is the only place a session leaves the map, and it bumps finished_session_counter_ under the same lock, so the counter equals the number of sessions removed. addSession is the only place one enters, and its increment follows the insert, so the entry snapshot is never larger than the number inserted. An empty map therefore already implies the counter has reached the snapshot. ForceFlushReportsSuccessOnceTheSessionIsDone asserts that invariant. It passes on main as well, where the old return happened to agree, so it guards the new predicate rather than reproducing a bug.
An assertion that ended a case before it completed the deferred requests reached ~OtlpGrpcClient, which loops until running_requests reaches zero and has no deadline. TryCancel() cannot move that counter for a request the stub is holding, so the binary hung rather than reporting the failure. Measured: exit 124 under a 30 s bound before, exit 1 after. A scope guard declared after the exporter drains on the way out, so the completion happens however the case leaves.
Two warnings the checks the repository runs would have flagged. The drain guard declared a destructor and deleted copies without the matching move operations, which cppcoreguidelines-special-member-functions wants; and the fixture sat at namespace scope where nothing outside the file names it, which misc-use-internal-linkage wants in an anonymous namespace. Measured against main with the CI header filters: one warning before, one after, both bugprone-derived-method-shadowing-base-method from the mock stub. Zero delta. The anonymous namespace is safe here: the fixture name appears nowhere else in the tree, and Bazel builds this file as its own cc_test rather than linking it into a combined binary.
Five seconds caught the ten second overrun this fixes but would have let a multi-second regression through. The HTTP case next to it uses one second against the same 50 ms deadline, and the gRPC case measures 66 ms.
Towards #4339. @owent confirmed the inverted comparison and suggested bounding the wait by the caller's remaining time, which is what this does. It does not close the issue; see "What this leaves" below.
OTLP gRPC reported the whole flush done on one completion
OtlpGrpcClient::ForceFlushleft its wait loop on any notification:std::cv_status::timeout != statusis true when the wait was notified, and the loop condition is the only placefinished_request_counteris read, so thebreakskipped it. One finished request out of many ended the wait, and the function then returned the leftover duration rather than the predicate.Rather than inverting the test I dropped the
breakand let the loop condition decide, which is what the==form reduces to once the wait is bounded. The boundedwait_foris what covers a notification lost between the check and the wait; thebreakwas not doing that. The return is the counter predicate now.The comment above it described
running_sessions_and gc session cleanup, neither of which exists in the gRPC client. It is a verbatim copy of theOtlpHttpClientone, down to the missing space inforever.We, so it is replaced rather than kept.Both clients waited on the exporter's timeout, not the caller's
ForceFlush(1ms)could block for the full configured export timeout. The wait interval ismin(configured timeout, caller's remaining time)now, as suggested. No notification path and no lock discipline changes.OtlpHttpClient::Shutdownis where that shows up in practice:Each of those calls could take
options_.timeout, so shutdown cost a multiple of the export timeout rather than a multiple of 1 ms.The result is the predicate, on both clients
Both loops used to end by returning what was left of the deadline. A completion that lands between the last check and the last wait takes its notification with it, and the wait then consumes the rest of the deadline and reports a timeout for work that had already finished. Each client now returns the condition it was waiting on.
One thing I tried and backed out
OtlpGrpcClientincrementsfinished_request_counterbefore it runsgrpc_async_callback, which is where theExportResultreaches the caller, so a bounded wait can see the counter satisfied while the result is still on its way. Moving the increment after the callback closes that, and I had it that way for a while.It is not worth it here.
DelegateAsyncExportis public and takes the callback, so a caller whose callback callsForceFlush()would wait for an increment that cannot happen until the callback returns, and the no-deadline form of that wait never ends. Trading a bounded early signal for an unbounded self-wait is the wrong way round, and neither of the two bugs this PR fixes needs the change. Where a request counts as finished belongs with the identity work #4339 still needs.Test
In
otlp_http_exporter_custom_client_test.cc,ForceFlushReturnsWithinTheCallerDeadlineholds a request open with the nosend HTTP client and assertsForceFlush(50ms)returns inside its deadline against a 30 second client timeout. It drivesOtlpHttpClient::Exportdirectly with a non-zero request budget, so it does not depend onENABLE_ASYNC_EXPORT.otlp_http_client.ccreverted tomain, test keptExpected: (elapsed) < (1000), actual: 30000 vs 1000ForceFlushReportsSuccessOnceTheSessionIsDonecovers the other side of the new return, that a flush with nothing left to wait for says so. Nothing was asserting the return value on that path. It passes onmainas well, where the oldtimeout_steady > 0happened to agree, so it guards the predicate rather than reproducing a bug.Built and run in two configurations,
WITH_ASYNC_EXPORT_PREVIEWoff and on, seven of seven passing in both, whole file 51 ms.The file carries the
#ifndef OPENTELEMETRY_STL_VERSIONguard its neighbours inexporters/otlp/testalready use, so like them it builds underWITH_STL=OFFand is empty in thecmake.c++*.stl.testjobs.The gRPC half is covered through the stub.
OtlpGrpcClientAsyncDatais defined in the.cc, so no test can readfinished_request_counterdirectly, but it does not have to:OtlpMockTraceServiceStubwas running the completion callback inline, so every request was already finished by the timeForceFlushcould be called and the loop never had anything to wait for. Holding the callback instead leaves the request in flight, which is all these two cases need.In
otlp_grpc_exporter_test.cc,OtlpGrpcExporterFlushTestPeercarries two cases.ForceFlushReturnsWithinTheCallerDeadlineasks for 50 ms against the 10 second export timeout.ForceFlushKeepsWaitingWhenAnotherRequestCompletesleaves two requests in flight and completes one while the flush is parked, which is the notification that used to end the wait.otlp_grpc_client.ccreverted tomain, tests keptExpected: (elapsed.count()) < (5000), actual: 10000 vs 5000Value of: flushed / Actual: true / Expected: falseThe second row is the bug in one line: the flush returned success at the exact moment a different request completed, with the one it was waiting for still in flight.
56 of 56 with
WITH_ASYNC_EXPORT_PREVIEW=ON. With it off the client tracks no in-flight requests, so the two cases skip in the fixture rather than being compiled away.gtest_add_testsregisters from the source text, so an#if-ed out case is still registered with CTest and then passes without running. CTest lists both, and the binary reports 54 passed, 2 skipped.What this leaves
ForceFlushstill does not fully meet the contract in your point 1. Both clients snapshot a monotonic total and wait for the finished total to reach it, so a request that starts after the call can satisfy the snapshot while one that was pending before it is still running. On entrystarted=10 finished=8; two new requests start and finish; finished reaches 10 and the wait ends with the original two in flight.Reading both clients for that follow-up turned up four more places where the boundary sits in the wrong spot, all of them on
mainand none of them touched here:during
PopulateRequestsees nothing;running_sessions_insert under the lock andstart_session_counter_after it;running_sessions_.empty(), which counts sessions startedafter the call, so a flush can also wait too long;
Unbindruns the caller's callback beforeReleaseSession, deliberately, because theresponse lives in an arena the session data owns.
All five want the same thing, a token issued at the exporter's
Exportentry rather than a counting total, which also settles the wrap a monotonic total has on a 32 bit target. That is a change to both hot paths, so I kept it out of one you had already scoped and wrote the set up on #4339 instead. Happy to send it as a follow-up.Behaviour change worth knowing
OtlpGrpcClient::Shutdown()defaults to a timeout of 0, documented as "no timeout is applied". With thebreakgone that call waits for outstanding requests instead of returning after the first completion, so an application that exported and exited immediately will now see the export finish rather than be cancelled.It does not wait indefinitely under a normal configuration: a call sets
context->set_deadline(now() + options.timeout)wheneveroptions.timeoutis positive, so each one completes or fails within the export timeout and increments the counter either way. A caller that wants the old return-early behaviour can pass an explicit short timeout.One thing I noticed but did not touch
OtlpGrpcClientOptions::timeoutandOtlpHttpClientOptions::timeoutboth default to a zero duration, and the exporters fill in a real value. If a client is left with zero,set_deadlineis skipped, and inForceFlushthe wait interval is zero as well, so the loop spins instead of sleeping.maindoes the same thing there, sincewait_for(lock, 0)returnscv_status::timeoutand the old!=test then did not break either, so this is not something the PR changes. I left it alone because the sensible re-check interval for "no configured timeout" is a policy call, and falling back to the caller's remaining time would reintroduce the overflow thatAdjustWaitForTimeoutexists to avoid. A sketch of the policy is on #4339 with the rest of the follow-up.Checklist
CHANGELOG.mdupdated for non-trivial changesForceFlushreturnsfalsein cases where it used to returntrue, and a defaultShutdown()now waits. Both are described above.CI note
func_otlp_grpchas a pre-existing nondeterministic double free that aborts after the tests report passing, and it reds gcc-14 and clang-18 onmainas well. It is not related to this change.