SAP / SAP/cloud-sdk-java

Cache evection of HTTP Client lead to not closed HttpClients

Open
#760 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature request
Dominant language
Java
Stars
41
Forks
33
Avg merge
18h 34m
Merged PRs (30d)
19

Description

Describe the Bug

The current implementation of caching and evicting an HTTP Client lead to not closed HttpClients.

https://github.com/SAP/cloud-sdk-java/blob/4fb1f6a0a4a2517a10dc4b02c69a6f072d98db8a/cloudplatform/connectivity-apache-httpclient5/src/main/java/com/sap/cloud/sdk/cloudplatform/connectivity/DefaultApacheHttpClient5Cache.java#L42-L47

Technically the HttpClients are CloseableHttpClient and should / need(specifically for pooled connections) to be closed after usage.
(See also this discussion)

In the current code-base, when a HttpClient with a PoolingHttpClientConnectionManager is created, it's not cleaned up properly on eviction.


The following workaround does not work in all cases:

        cache = Caffeine.newBuilder().expireAfterAccess(duration, unit).ticker(ticker).evictionListener((key, value, cause) -> {
            if (value instanceof CloseableHttpClient closeableHttpClient) {
                try {
                    closeableHttpClient.close();
                } catch (final Exception e) {
                    log.warn("Failed to close HttpClient. Ignoring the exception and continue.", e);
                }
            }
        }).build();

There are two basic scenarios:

  1. The client was created and is no longer in use and the eviction time triggers the cleanup. ✅
  2. The client was created and in still in use (long running operation, async operation, ...) and the eviction time triggers the cleanup. Then the evictionListener would kill the connection underneath. ❌
Steps to Reproduce

Code review.

Expected Behavior

Proper closing of HttpClients.

Screenshots

No response

Used Versions

Current state in main.

Code Examples
// Your code here
Stack Trace

No response

Log File
Log file ...
Affected Development Phase

Development

Impact

No Impact

Timeline

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with DefaultApacheHttpClient5Cache.java at the linked cache construction and review the discussion in issue 755. Trace how pooled CloseableHttpClients are created, cached, evicted, and used during long-running or asynchronous operations; done means evicted clients are closed without interrupting clients still in use.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
networking
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.