-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
[Java][HttpClient] Fix memory leak with virtual threads (#21729) #21752
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Java][HttpClient] Fix memory leak with virtual threads (#21729) #21752
Conversation
|
Thanks for the PR but your commit (as shown in the Commits tab) is not linked to your Github account, which means this PR won't count as your contribution in https://github.com/OpenAPITools/openapi-generator/graphs/contributors. Let me know if you need help fixing it. |
|
also cc @aaronforest-wf - the author of #19054 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes a memory leak in the Apache HttpClient implementation when using virtual threads. The issue occurred because status codes and response headers were stored in ConcurrentHashMaps indexed by thread ID, causing unlimited growth when virtual threads are used since each execution creates a new thread.
- Replaces ConcurrentHashMap with ThreadLocal for storing status codes and response headers
- Removes the now-unused ConcurrentHashMap import
- Updates getter methods to use ThreadLocal.get() instead of map lookups
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| modules/openapi-generator/src/main/resources/Java/libraries/apache-httpclient/ApiClient.mustache | Template file updated to use ThreadLocal instead of ConcurrentHashMap for thread-safe storage |
| samples/client/petstore/java/apache-httpclient/src/main/java/org/openapitools/client/ApiClient.java | Generated sample code updated with ThreadLocal implementation |
| samples/client/echo_api/java/apache-httpclient/src/main/java/org/openapitools/client/ApiClient.java | Generated sample code updated with ThreadLocal implementation |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| @Deprecated | ||
| public int getStatusCode() { | ||
| return lastStatusCodeByThread.get(Thread.currentThread().getId()); | ||
| return lastStatusCode.get(); |
Copilot
AI
Aug 16, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ThreadLocal.get() can return null if no value has been set for the current thread. This will cause a NullPointerException when auto-unboxing Integer to int. Consider using ThreadLocal.withInitial() or add null checking.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same issue was always there before. It is an error to call the method before any call is made.
| @Deprecated | ||
| public Map<String, List<String>> getResponseHeaders() { | ||
| return lastResponseHeadersByThread.get(Thread.currentThread().getId()); | ||
| return lastResponseHeaders.get(); |
Copilot
AI
Aug 16, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ThreadLocal.get() can return null if no value has been set for the current thread. This could cause NullPointerException in calling code that expects a Map. Consider using ThreadLocal.withInitial() to provide a default empty map.
14f85d1 to
2e13656
Compare
Thank you. I hope I fixed that. If even the committer should be aligned, I'll probably need to jump on my personal laptop. |
|
let's give it a try. thanks for the contribution |
Fixes #21729 - memory leak in Apache HttpClient when the result (status code and headers) were stored under the thread id. In a setup when virtual thread executor is used, each execution uses a different thread, and so the map grows without limit.
The fix replaces ConcurrentHashMap with ThreadLocal, which should assure that once the Thread ends, the result is deallocated. It solution should be as thread-safe as before.
@wing328 - you reviewed the PR adding the memory leak, would you care to take a look at this fix?
PR checklist
Commit all changed files.
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*.IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
master(upcoming7.x.0minor release - breaking changes with fallbacks),8.0.x(breaking changes without fallbacks)"fixes #123"present in the PR description)