fix: Prevent OkHttp idle threads from blocking JVM shutdown
Prevent OkHttp idle threads from blocking JVM shutdown
Please ensure you have read the contribution guide before creating a pull request.
Link to Issue or Description of Change
1. Link to an existing issue (if applicable):
- Closes: #1267
2. Or, if no issue exists, describe the change:
Problem:
The ADK framework does not cleanly terminate the JVM upon process completion when making chat completions requests because ChatCompletionsHttpClient and the internal Gemini API client construct default OkHttpClient instances. These produce non-daemon threads with a keep-alive timeout, keeping the JVM alive.
Solution:
Created HttpUtils.createSharedHttpClient() utility method that returns an OkHttpClient using a custom dispatcher with a daemon thread factory and 5-minute timeouts. Refactored ChatCompletionsHttpClient, ApiClient, and Gemini to invoke this utility and maintain their own shared daemon-thread client pools.
Testing Plan
Unit Tests:
- [x] I have added or updated unit tests for my change.
- [x] All unit tests pass locally.
All existing core tests pass successfully. (A new test for HttpUtils can be added to verify the ThreadFactory creates daemon threads).
Manual End-to-End (E2E) Tests:
Wired the Google native chat completion client into the external model-prism project (PR #1199). Ran the integration through all the demo tests covering all main ADK features. Explicitly tested the native Gemini provider (via the google_search_agent tests) as well as third-party OpenAI-compatible providers. After the final agent response is logged, the Java process successfully exits cleanly with code 0 across all model types instead of hanging while waiting for background threads to timeout.
Checklist
- [x] I have read the CONTRIBUTING.md document.
- [x] My pull request contains a single commit.
- [x] I have performed a self-review of my own code.
- [x] I have commented my code, particularly in hard-to-understand areas.
- [x] I have added tests that prove my fix is effective or that my feature works.
- [x] New and existing unit tests pass locally with my changes.
- [x] I have manually tested my changes end-to-end.
- [x] Any dependent changes have been merged and published in downstream modules.
Additional context
This fixes critical thread leaks for users running short-lived or background ADK agents.
Hi @svetanis, thank you for your contribution! We appreciate you taking the time to submit this pull request. Currently this PR is under review by our team, we will keep you posted if any additional information is required. thank you.
@anfatum, Could you please review this PR.
Hi @hemasekhar-p,
Thanks for the update! Just a quick heads up: I went ahead and rebased the branch onto the latest main so that it stays perfectly clean and conflict-free for your review.
Looking forward to hearing back from the team!