Skip to content

[Bug] Skywalking-java HttpClient 5.x plugin closes the caller thread's active span when FutureCallback runs outside the I/O thread #14097

Description

@xuzhiguang

Search before asking

  • I had searched in the issues and found no similar issues.

Apache SkyWalking Component

Java Agent (apache/skywalking-java)

What happened

try (CloseableHttpClient client = HttpAsyncClients.classic(
        asyncClient, Timeout.ofSeconds(30))) {

        String body = client.execute(
            new HttpGet("http://127.0.0.1:8080/hello"),
            response -> {
                // This lambda is executed by the business thread that invokes `execute`.
                return EntityUtils.toString(response.getEntity());
            }
        );

        System.out.println(body);
    }

When running the sample code with the SkyWalking Java Agent enabled, the HttpClient 5.x plugin may close the caller thread's active span unexpectedly.

The sample uses HttpAsyncClients.classic(...), which adapts an async HttpClient to the classic synchronous API. When the caller thread reads the response entity to EOF, HttpClient may invoke FutureCallback.completed() on that same caller thread.

The SkyWalking HttpClient 5.ugin wraps the callback in FutureCallbackWrapper. Its current implementation calls:
ContextManager.stopSpan(); without keeping a reference to the HTTP span created for this request.

Before reading the response body: TraceId is available
After reading the response body to EOF: TraceId becomes N/A

What you expected to happen

FutureCallback should only finish the span associated with the current HTTP request. It must not close the active span on the caller thread.

After reading the response body to EOF, the caller's Entry Span and TraceId should remain active until the business request actually completes.

How to reproduce

Please run the sample code included in the What happened section.

Anything else

No response

Are you willing to submit a pull request to fix on your own?

  • Yes I am willing to submit a pull request on my own!

Code of Conduct

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working and you are sure it's a bug!javaJava agent relatedpluginPlugin for agent or collector. Be used to extend the capabilities of default implementor.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions