Skip to content

Commit 4dbf11c

Browse files
committed
Fix semaphore not being released on Throwable, see #1314
Motivation: Fix for #1314 introduced a regression where the Semaphore leaks and is not released on Throwable. Modification: Release Semaphore when called handler method is onThrowable too, not only onComplete. Result No more Semaphore leak
1 parent fd907c0 commit 4dbf11c

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

client/src/main/java/org/asynchttpclient/filter/ReleasePermitOnComplete.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@
1111
import org.asynchttpclient.AsyncHandler;
1212

1313
/**
14-
* Wrapper for {@link AsyncHandler}s to release a permit on {@link AsyncHandler#onCompleted()}.
15-
* This is done via a dynamic proxy to preserve all interfaces of the wrapped handler.
14+
* Wrapper for {@link AsyncHandler}s to release a permit on {@link AsyncHandler#onCompleted()}. This is done via a dynamic proxy to preserve all interfaces of the wrapped handler.
1615
*/
1716
public class ReleasePermitOnComplete {
1817

1918
/**
2019
* Wrap handler to release the permit of the semaphore on {@link AsyncHandler#onCompleted()}.
20+
*
21+
* @param handler the handler to be wrapped
22+
* @param available the Semaphore to be released when the wrapped handler is completed
23+
* @return the wrapped handler
2124
*/
2225
@SuppressWarnings("unchecked")
2326
public static <T> AsyncHandler<T> wrap(final AsyncHandler<T> handler, final Semaphore available) {
@@ -31,8 +34,11 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
3134
try {
3235
return method.invoke(handler, args);
3336
} finally {
34-
if ("onCompleted".equals(method.getName())) {
37+
switch (method.getName()) {
38+
case "onCompleted":
39+
case "onThrowable":
3540
available.release();
41+
default:
3642
}
3743
}
3844
}
@@ -41,6 +47,8 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
4147

4248
/**
4349
* Extract all interfaces of a class.
50+
* @param handlerClass the handler class
51+
* @return all interfaces implemented by this class
4452
*/
4553
static Class<?>[] allInterfaces(Class<?> handlerClass) {
4654
Set<Class<?>> allInterfaces = new HashSet<>();

0 commit comments

Comments
 (0)