Skip to content

[Java][apache-httpclient] Apply connect timeout to requests, add read timeout support#24284

Open
treaz wants to merge 3 commits into
OpenAPITools:masterfrom
treaz:fix-apache-httpclient-connect-timeout-24269
Open

[Java][apache-httpclient] Apply connect timeout to requests, add read timeout support#24284
treaz wants to merge 3 commits into
OpenAPITools:masterfrom
treaz:fix-apache-httpclient-connect-timeout-24269

Conversation

@treaz

@treaz treaz commented Jul 13, 2026

Copy link
Copy Markdown

Fixes #24269.

For the Java client with library=apache-httpclient, ApiClient.setConnectTimeout(int) stored the value in a connectionTimeout field that was never read again, so the setter was a silent no-op: the client fell back to Apache HttpClient 5's own defaults (3-minute connect timeout, unbounded response wait).

Changes

  • invokeAPI() now builds a per-request RequestConfig from the configured timeouts and sets it on the HttpClientContext, so setConnectTimeout(ms) actually bounds the TCP connect time, consistent with the other Java libraries (okhttp-gson, native).
  • Added symmetric getReadTimeout()/setReadTimeout(ms) mapped to the response timeout, as suggested in the issue (mirrors the okhttp-gson naming).
  • A value of 0 (the default) leaves the underlying client's configuration untouched.

The per-request RequestConfig approach was chosen over rebuilding the client with a PoolingHttpClientConnectionManager/ConnectionConfig because:

  • ConnectionConfig only exists since httpclient5 5.2, while the Gradle template still ships 5.1.3 (RequestConfig#setConnectTimeout(Timeout) exists since 5.0 and is honored in all 5.x versions, taking precedence over connection-manager defaults in 5.3+);
  • it preserves a user-supplied custom CloseableHttpClient (constructor or setHttpClient) instead of silently discarding it.

Tests

Added ApiClientTest to the apache-httpclient petstore sample:

  • getter/setter round-trips for both timeouts;
  • a regression test that a request against a socket that accepts but never responds fails after the configured 500 ms read timeout (verified: 0.52 s elapsed) instead of hanging forever;
  • a regression test that a connect to a non-routable TEST-NET-1 address fails within bounds instead of taking Apache's 3-minute default.

All 136 test classes in the sample pass; the echo_api and jackson3 samples were regenerated (./bin/generate-samples.sh bin/configs/java-apache-httpclient*.yaml) and compile.

PR checklist

  • Read the contribution guidelines.
  • Run the following to build the project and update samples: ./bin/generate-samples.sh bin/configs/java-apache-httpclient*.yaml (only the apache-httpclient templates changed; no doc changes, so export_docs_generators.sh produces no diff).
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

cc @bbdouglas @sreeshas @jfiala @lukoyanov @cbornet @jeff9finger @karismann @Zomzog @lwlee2608 @martin-mfg


Summary by cubic

Applies per-request connect and read timeouts in the Java apache-httpclient client to prevent hangs and make setConnectTimeout effective. Preserves defaults from a custom CloseableHttpClient. Fixes #24269.

  • Bug Fixes

    • Applies setConnectTimeout(ms) per request to bound TCP connect time.
    • Builds per-request RequestConfig from the underlying client's defaults (via Configurable); 0 uses client defaults.
    • Rejects negative values in setConnectTimeout()/setReadTimeout().
  • New Features

    • Added getReadTimeout()/setReadTimeout(ms) to control response wait time.
    • Works with httpclient5 5.1.x+; per-request config is compatible across versions.

Written for commit b3fa161. Summary will update on new commits.

Review in cubic

…ead timeout support

setConnectTimeout() stored the value in a field that was never read, so
the configured timeout was silently ignored and Apache HttpClient's own
defaults applied (3-minute connect timeout, unbounded response wait).

Wire connectionTimeout into a per-request RequestConfig set on the
HttpClientContext in invokeAPI(), and add symmetric getReadTimeout()/
setReadTimeout() support mapped to the response timeout. The per-request
approach works with both httpclient5 5.1.x (Gradle template) and 5.2.x
(Maven template) and preserves a user-supplied custom CloseableHttpClient
instead of rebuilding it.

Fixes OpenAPITools#24269

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 5 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

…faults, reject negative timeouts

Address review feedback:
- when building the per-request RequestConfig, start from the default
  request configuration of the underlying HTTP client (via Configurable)
  so a caller-supplied client's redirect policy, cookie spec and other
  request defaults are preserved and only the configured timeouts are
  overridden
- reject negative values in setConnectTimeout()/setReadTimeout() with
  IllegalArgumentException instead of silently ignoring them
@treaz

treaz commented Jul 13, 2026

Copy link
Copy Markdown
Author

Addressed the review findings in caedbe4:

  • Custom client request-config defaults being discarded (P1/P2): the per-request RequestConfig is now built from the underlying client's own default configuration when available (Configurable#getConfig() + RequestConfig.copy(), both present since httpclient5 5.0), so only the configured timeouts are overridden and a custom client's redirect policy, cookie spec, and other request defaults are preserved. Added a regression test (testCustomClientRequestConfigDefaultsArePreserved) that supplies a custom client with a 500 ms default response timeout, sets only the connect timeout on the ApiClient, and verifies the response timeout still fires (0.53 s observed).
  • Negative timeouts silently accepted (P3): setConnectTimeout()/setReadTimeout() now throw IllegalArgumentException for negative values, with a test covering both.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 5 files (changes from recent commits).

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

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.

[BUG][JAVA][apache-httpclient] setConnectTimeout()/getConnectTimeout() have no effect — connectionTimeout field is never applied to the HttpClient

1 participant