Skip to content

http: attach a caller context to async client requests - #49113

Merged
wbpcode merged 5 commits into
envoyproxy:mainfrom
wdauchy:shadow-callbacks-lifetime
Nov 13, 2026
Merged

wbpcode merged 5 commits into
envoyproxy:mainfrom
wdauchy:shadow-callbacks-lifetime

Conversation

@wdauchy

@wdauchy wdauchy commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Commit Message: http: attach a caller context to async client requests

Add AsyncClient::RequestContext, set with RequestOptions::setContext() as a shared_ptr. The request holds it until it is destroyed and returns it from Request::context(), so shared callbacks can get their per-request data back.

Use it for the mirror policy stats, which allocated callbacks per mirror that deleted themselves from onBeforeFinalizeUpstreamSpan().

Additional Description:
Follow-up to #48470, resolving the TODO on ShadowRequestCallbacks. As @botengyao pointed out, the self-deletion relied on onBeforeFinalizeUpstreamSpan() being called exactly once per request, so a change there could leak the object. The context approach was suggested by @wbpcode and keeps the callbacks' lifetime unchanged.

The shadow writer now uses its shared callbacks for all mirrors. For a policy with stat_prefix, it sets a ShadowRequestContext that records the outcome in onSuccess()/onFailure() and counts rq_cancelled when destroyed without one. When send()/startRequest() returns nullptr, a failure is recorded unless the callbacks already ran inline. Policies without stat_prefix allocate nothing.

Risk Level: Low. New optional field; existing callers are unchanged.
Testing: New async_client_impl_test cases (context reachable from the callbacks and released with the request on success, reset, cancel, immediate and creation failure); updated shadow_writer_impl_test; router_test and shadow_policy_integration_test pass.
Docs Changes: N/A
Release Notes: N/A
Platform Specific Features: N/A

Add send() and startRequest() overloads taking the callbacks as a
std::unique_ptr. The request owns them and destroys them in its
destructor, after their last call and also when the request is cancelled.
If no request is returned, the callbacks were already called inline and
destroyed; a request that cannot be created reports onFailure(Reset)
first, since the caller keeps nothing to report it through.

Use them for the mirror policy stats callbacks, which deleted themselves
from onBeforeFinalizeUpstreamSpan() and relied on it being called exactly
once per request.

Signed-off-by: William Dauchy <[email protected]>
@wdauchy

wdauchy commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

/retest

@wbpcode wbpcode left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have another Idea. That's say, for a pure C async callback, we usually set a void* user data. Then, at the callback, we could cast the user data back to explicit context.

I think we could do similar things there? That's say, set a shared pointer UserData/Context into RequestOptions.

Then, expose a method to the Request to expose the user data like OptRef<Context> context();. By this way, we could get rid of the semantics change of the callback's lifetime.

WDYT?

@wbpcode wbpcode self-assigned this Nov 10, 2026
Replace the send() and startRequest() overloads owning the callbacks with
a context set in RequestOptions, as suggested in review: setContext()
takes a shared_ptr to an AsyncClient::RequestContext, the request holds it
until it is destroyed, and Request::context() returns it, so callbacks can
get their per-request data back without changing their lifetime.

The shadow writer uses its shared callbacks for all mirrors again. For a
policy with stats it sets a ShadowRequestContext that records the outcome,
and counts a cancel when it is destroyed without one. A nullptr return is
counted as a failure unless the callbacks already ran inline.

Signed-off-by: William Dauchy <[email protected]>
@wdauchy
wdauchy requested a review from yanavlasov as a code owner November 10, 2026 09:20
@wdauchy wdauchy changed the title http: let the async client own request callbacks http: attach a caller context to async client requests Nov 10, 2026
@wdauchy

wdauchy commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, I like that better: it keeps the callbacks' lifetime as is and avoids allocating callbacks per mirror. Reworked in 162b75a: RequestOptions::setContext() now takes a shared_ptr<AsyncClient::RequestContext>, held by the request and exposed as context() on Request. The shadow writer uses its shared callbacks again and records outcomes through a ShadowRequestContext; a cancel is counted when the context is destroyed without an outcome, which follows the request's lifetime since the only other holder, the router's per-mirror options, is released once the mirror is started. PTAL.

@wdauchy

wdauchy commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

/retest

@wbpcode wbpcode left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update. one last comment!

Comment thread envoy/http/async_client.h Outdated
Comment on lines +93 to +97

/**
* @return the context set in the options of the request, if any.
*/
virtual OptRef<RequestContext> context() const PURE;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a default implementation.

Suggested change
/**
* @return the context set in the options of the request, if any.
*/
virtual OptRef<RequestContext> context() const PURE;
/**
* @return the context set in the options of the request, if any.
*/
virtual OptRef<RequestContext> context() const { return {}; }

Although I guess there should no out-of-tree implementation for this class, but in case.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 800c3e0, thanks. I also dropped the context() override in the dynatrace test that is no longer needed.

@wdauchy

wdauchy commented Nov 12, 2026

Copy link
Copy Markdown
Contributor Author

/retest

@wdauchy
wdauchy requested a review from wbpcode November 13, 2026 08:07

@wbpcode wbpcode left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks.

@wbpcode
wbpcode merged commit 8ce942c into envoyproxy:main Nov 13, 2026
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants