Skip to content

Commit 5fd6af3

Browse files
committed
Completable Futures get and remove refs from its parent manager
1 parent 3f93c92 commit 5fd6af3

2 files changed

Lines changed: 7 additions & 0 deletions

File tree

src/main/java/software/amazon/awssdk/crt/CrtResource.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,9 @@ public long getNativeHandle() {
238238
*/
239239
public void addRef() {
240240
refCount.incrementAndGet();
241+
if (debugNativeObjects) {
242+
Log.log(ResourceLogLevel, Log.LogSubject.JavaCrtResource, String.format("Instance of class %s(%d) is adding a reference", this.getClass().getCanonicalName(), id));
243+
}
241244
}
242245

243246
/**

src/main/java/software/amazon/awssdk/crt/http/HttpClientConnectionManager.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ public CompletableFuture<HttpClientConnection> acquireConnection() {
162162
}
163163

164164
CompletableFuture<HttpClientConnection> returnedFuture = new CompletableFuture<>();
165+
/* Futures need to add a ref so the HttpClientConnectionManager doesn't clean up from underneath the Future. */
166+
this.addRef();
167+
/* This insures that whether a CompletableFuture completes or throws an exception, we remove the ref it added. */
168+
returnedFuture.whenComplete((conn, ex) -> decRef());
165169
httpClientConnectionManagerAcquireConnection(this.getNativeHandle(), returnedFuture);
166170
return returnedFuture;
167171
}

0 commit comments

Comments
 (0)