Skip to content

False-positive RequestContext leak warning when using RetryingClient #6420

@ikhoon

Description

@ikhoon

When a ServiceRequestContext is pushed, Armeria validates it against any existing ClientRequestContext stored in the thread-local.

if (RequestContextUtil.equalsIgnoreWrapper(oldCtx.root(), this)) {

If ClientRequestContext.root() is null, the validation logic throws an IllegalStateException since it thinks the context is leaked.

Normally, ClientRequestContext.root() is set when a client is executed within a service, so this check works as expected.

this(null, meterRegistry, sessionProtocol,
id, method, reqTarget, endpointGroup, options, req, rpcReq, requestOptions,
serviceRequestContext(), /* responseCancellationScheduler */ null,
requestStartTimeNanos, requestStartTimeMicros);
}

However, if a client request is executed outside a ServiceRequestContext, which can easily happen during asynchronous composition, then ClientRequestContext.root() remains null.

clientA.execute()
       .thenComposeAsync(unused -> clientB.execute());

In such cases, even though the request was initiated and completed from a valid ServiceRequestContext, Armeria incorrectly reports a request context leak.

This issue is especially reproducible with RetryingClient. Since its responses are completed on a context-aware event loop, a ClientRequestContext is always present in the thread-local.

final HttpResponse res = HttpResponse.of(responseFuture, ctx.eventLoop());

That is triggering the problematic validation. In contrast, without RetryingClient, responses complete on the channel’s event loop and the warning does not occur.

I do think that Armeria should not report a RequestContext leak when the client request actually starts and completes within a valid service scope.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions