Missing timeout check on CountDownLatch.await High

If a timeout check on CountDownLatch.await is missing, the thread might unblock even when the latch has not counted down to zero. This might cause synchronization errors.

Detector ID
java/missing-timeout-check-on-latch-await@v1.0
Category
Common Weakness Enumeration (CWE) external icon

Noncompliant example

1public Object getResultNonCompliant(long timeout, TimeUnit unit)
2    throws InterruptedException, ExecutionException, TimeoutException {
3    // Noncompliant: code does not check if await timed out or the latch counted down to zero.
4    completionLatch.await(timeout, unit);
5    return result;
6}

Compliant example

1public Object getResultCompliant(long timeout, TimeUnit unit)
2    throws InterruptedException, ExecutionException, TimeoutException {
3    // Compliant: code handles the case when await times out.
4    if (!completionLatch.await(timeout, unit)) {
5        throw new TimeoutException();
6    }
7    return result;
8}