Skip to content

Commit 0e15cd6

Browse files
committed
add timeout for countdown latch
1 parent e500a60 commit 0e15cd6

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

src/main/java/org/privacyidea/AsyncRequestCallable.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Map;
2222
import java.util.concurrent.Callable;
2323
import java.util.concurrent.CountDownLatch;
24+
import java.util.concurrent.TimeUnit;
2425
import okhttp3.Call;
2526
import okhttp3.Callback;
2627
import okhttp3.Response;
@@ -43,7 +44,8 @@ public class AsyncRequestCallable implements Callable<String>, Callback
4344
final String[] callbackResult = {null};
4445
private CountDownLatch latch;
4546

46-
public AsyncRequestCallable(PrivacyIDEA privacyIDEA, Endpoint endpoint, String path, Map<String, String> params, Map<String, String> headers, boolean authTokenRequired, String method)
47+
public AsyncRequestCallable(PrivacyIDEA privacyIDEA, Endpoint endpoint, String path, Map<String, String> params,
48+
Map<String, String> headers, boolean authTokenRequired, String method)
4749
{
4850
this.privacyIDEA = privacyIDEA;
4951
this.endpoint = endpoint;
@@ -69,7 +71,11 @@ public String call() throws Exception
6971
String tmpPath = path;
7072
path = ENDPOINT_AUTH;
7173
endpoint.sendRequestAsync(ENDPOINT_AUTH, privacyIDEA.serviceAccountParam(), Collections.emptyMap(), PIConstants.POST, this);
72-
latch.await();
74+
if (!latch.await(30, TimeUnit.SECONDS))
75+
{
76+
privacyIDEA.error("Latch timed out...");
77+
return "";
78+
}
7379
// Extract the auth token from the response
7480
String response = callbackResult[0];
7581
String authToken = privacyIDEA.parser.extractAuthToken(response);
@@ -87,7 +93,11 @@ public String call() throws Exception
8793
// Do the actual request
8894
latch = new CountDownLatch(1);
8995
endpoint.sendRequestAsync(path, params, headers, method, this);
90-
latch.await();
96+
if (!latch.await(30, TimeUnit.SECONDS))
97+
{
98+
privacyIDEA.error("Latch timed out...");
99+
return "";
100+
}
91101
return callbackResult[0];
92102
}
93103

0 commit comments

Comments
 (0)