Skip to content

Commit 86291d3

Browse files
committed
Fix leaking HTTP client on client build failure
1 parent 0b6c8b9 commit 86291d3

File tree

1 file changed

+10
-4
lines changed
  • modules/repository-s3/src/main/java/org/elasticsearch/repositories/s3

1 file changed

+10
-4
lines changed

modules/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3Service.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,16 @@ public AmazonS3Reference client(RepositoryMetadata repositoryMetadata) {
158158
}
159159
// TODO NOMERGE: consider alternative methods of retaining an httpClient reference for an explicit close() call.
160160
SdkHttpClient httpClient = buildHttpClient(clientSettings);
161-
final AmazonS3Reference clientReference = new AmazonS3Reference(buildClient(clientSettings, httpClient), httpClient);
162-
clientReference.mustIncRef();
163-
clientsCache = Maps.copyMapWithAddedEntry(clientsCache, clientSettings, clientReference);
164-
return clientReference;
161+
Releasable toRelease = httpClient::close;
162+
try {
163+
final AmazonS3Reference clientReference = new AmazonS3Reference(buildClient(clientSettings, httpClient), httpClient);
164+
clientReference.mustIncRef();
165+
clientsCache = Maps.copyMapWithAddedEntry(clientsCache, clientSettings, clientReference);
166+
toRelease = null;
167+
return clientReference;
168+
} finally {
169+
Releasables.close(toRelease);
170+
}
165171
}
166172
}
167173

0 commit comments

Comments
 (0)