Skip to content

Commit 9765867

Browse files
Jouramieok2c
authored andcommitted
Fix resource leaking on error in AsyncCachingExec
1 parent dbb9e86 commit 9765867

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/AsyncCachingExec.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,9 @@ private void triggerResponse(
135135
final SimpleBody body = cacheResponse.getBody();
136136
final byte[] content = body != null ? body.getBodyBytes() : null;
137137
final ContentType contentType = body != null ? body.getContentType() : null;
138+
AsyncDataConsumer dataConsumer = null;
138139
try {
139-
final AsyncDataConsumer dataConsumer = asyncExecCallback.handleResponse(
140+
dataConsumer = asyncExecCallback.handleResponse(
140141
cacheResponse,
141142
content != null ? new BasicEntityDetails(content.length, contentType) : null);
142143
if (dataConsumer != null) {
@@ -148,6 +149,10 @@ private void triggerResponse(
148149
asyncExecCallback.completed();
149150
} catch (final HttpException | IOException ex) {
150151
asyncExecCallback.failed(ex);
152+
} finally {
153+
if (dataConsumer != null) {
154+
dataConsumer.releaseResources();
155+
}
151156
}
152157
}
153158

httpclient5/src/main/java/org/apache/hc/client5/http/async/methods/InflatingZstdDataConsumer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ public void streamEnd(final List<? extends Header> trailers) throws HttpExceptio
130130

131131
@Override
132132
public void releaseResources() {
133-
dctx.close();
133+
if (closed.compareAndSet(false, true)) {
134+
dctx.close();
135+
}
134136
downstream.releaseResources();
135137
}
136138
}

0 commit comments

Comments
 (0)