Skip to content

fix: Prevent OkHttp idle threads from blocking JVM shutdown#1268

Open
svetanis wants to merge 1 commit into
google:mainfrom
svetanis:fix/graceful-jvm-shutdown
Open

fix: Prevent OkHttp idle threads from blocking JVM shutdown#1268
svetanis wants to merge 1 commit into
google:mainfrom
svetanis:fix/graceful-jvm-shutdown

Conversation

@svetanis

Copy link
Copy Markdown

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):

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:

  • I have added or updated unit tests for my change.
  • 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

  • I have read the CONTRIBUTING.md document.
  • My pull request contains a single commit.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • 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.

…hutdown

This fixes an issue where the JVM hangs waiting for background networking threads to timeout after completing an execution. Replaced default OkHttpClient builders with a new HttpUtils.createSharedHttpClient factory that injects a custom thread factory setting daemon=true.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Prevent OkHttp idle threads from blocking JVM shutdown

1 participant