SAP / SAP/cloud-sdk-java

Cache evection of HTTP Client lead to not closed HttpClients

Offen
#760 4 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

feature request
Vorherrschende Sprache
Java
Sterne
41
Forks
33
Ø Merge
18 Std. 34 Min.
Gemergte PRs (30 T.)
19

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit DefaultApacheHttpClient5Cache.java bei der verlinkten Cache-Erstellung und prüfe die Diskussion in Issue 755. Verfolge, wie gepoolte CloseableHttpClients erstellt, zwischengespeichert, entfernt und während lang laufender oder asynchroner Vorgänge verwendet werden; die Aufgabe ist abgeschlossen, wenn entfernte Clients geschlossen werden, ohne noch verwendete Clients zu unterbrechen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
java
Bereich
networking
Issue-Typ
Bug
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
32/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.