[Bug] Skywalking-java HttpClient 5.x plugin closes the caller thread's active span when FutureCallback runs outside the I/O thread
Search before asking
- [x] 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?
- [x] Yes I am willing to submit a pull request on my own!
Code of Conduct
- [x] I agree to follow this project's Code of Conduct
The plugin implicitly assumes that FutureCallback always runs on the I/O thread.
Hi, I'd like to work on this issue and submit a PR for the fix. I'll first reproduce the behavior and investigate the FutureCallbackWrapper span handling. If that's okay, I'll proceed with the implementation and regression test.
Hi, I'd like to work on this issue and submit a PR for the fix. I'll first reproduce the behavior and investigate the FutureCallbackWrapper span handling. If that's okay, I'll proceed with the implementation and regression test.
Assigned to you. If you have any problem, let us know.
Sure, thanks for assigning it to me! I’m working on it now. I’m currently tracing the HttpClient 5.x instrumentation flow, particularly the FutureCallbackWrapper and how the SkyWalking context/span is propagated and closed across callback threads. I’ll also add a regression test to cover the case where the callback executes on the caller/business thread. It may take me a little time to properly reproduce and verify the fix, but I’ll keep working on it and let you know if I run into any issues.