Skip to content

Conversation

@juriad
Copy link
Contributor

@juriad juriad commented Aug 14, 2025

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

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    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.
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR solves a reported issue, reference it using GitHub's linking syntax (e.g., having "fixes #123" present in the PR description)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@wing328
Copy link
Member

wing328 commented Aug 14, 2025

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.

Ref: https://github.com/OpenAPITools/openapi-generator/wiki/FAQ#how-can-i-update-commits-that-are-not-linked-to-my-github-account

@wing328
Copy link
Member

wing328 commented Aug 14, 2025

also cc @aaronforest-wf - the author of #19054

@wing328 wing328 added this to the 7.15.0 milestone Aug 16, 2025
@wing328 wing328 requested a review from Copilot August 16, 2025 03:59
Copy link
Contributor

Copilot AI left a 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();
Copy link

Copilot AI Aug 16, 2025

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.

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

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();
Copy link

Copilot AI Aug 16, 2025

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.

Copilot uses AI. Check for mistakes.
@juriad juriad force-pushed the java-http-client-fix-memory-leak-virtual-threads branch from 14f85d1 to 2e13656 Compare August 18, 2025 06:17
@juriad
Copy link
Contributor Author

juriad commented Aug 18, 2025

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.

Thank you. I hope I fixed that. If even the committer should be aligned, I'll probably need to jump on my personal laptop.

@wing328 wing328 closed this Aug 21, 2025
@wing328 wing328 reopened this Aug 21, 2025
@wing328 wing328 merged commit dbe0419 into OpenAPITools:master Aug 22, 2025
169 of 181 checks passed
@wing328
Copy link
Member

wing328 commented Aug 22, 2025

let's give it a try.

thanks for the contribution

Goopher pushed a commit to Goopher/openapi-generator that referenced this pull request Sep 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG][Java][HttpClient] Memory leak with virtual threads

2 participants