Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.Semaphore;
import java.util.function.Function;

public final class FleetCalls {

// one Fleet serves every tenant and its agents (1 CPU on prod), so a page must not fire dozens of calls at once
static final int MAX_IN_FLIGHT = 8;
private static final Semaphore IN_FLIGHT = new Semaphore(MAX_IN_FLIGHT);

private FleetCalls() {
}

Expand All @@ -17,12 +22,21 @@
List<Future<R>> pending;
try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) {
pending = keys.stream()
.map(key -> executor.submit(() -> call.apply(key)))
.map(key -> executor.submit(() -> limited(call, key)))
.toList();
}
return pending.stream().map(FleetCalls::resultOf).toList();
}

private static <T, R> R limited(Function<T, R> call, T key) throws InterruptedException {
IN_FLIGHT.acquire();
try {
return call.apply(key);
} finally {
IN_FLIGHT.release();
}
}

Check failure on line 38 in openframe-api-lib/src/main/java/com/openframe/api/service/rmm/fleet/FleetCalls.java

View workflow job for this annotation

GitHub Actions / Flamingo Code Review

limited() declares a checked throws InterruptedException, violating the unchecked-exceptions rule and losing the interrupt status

limited() declares a checked throws InterruptedException, violating the unchecked-exceptions rule and losing the interrupt status The new private method `limited` is declared `throws InterruptedException`, a checked exception. Per OFJAVA-014 all exceptions in this codebase must be unchecked, with no `throws` clauses in signatures. Worse, because this call runs inside `executor.submit(() -> limited(call, key))`, the InterruptedException is captured by the Future and only surfaces later through `resultOf`, which only special-cases RuntimeException — an InterruptedException thrown by `IN_FLIGHT.acquire()` is not a RuntimeException, so `future.resultNow()` will throw `IllegalStateException` (wrong state) instead of propagating the real cause, and the thread's interrupt status is silently lost (never re-set with `Thread.currentThread().interrupt()`). This can turn a shutdown/interrupt signal into a confusing IllegalStateException, and defeats cooperative cancellation of the virtual-thread pool.
Comment on lines +31 to +38

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🦩 🔴 [error/action_required] OFJAVA-014 limited() declares a checked throws InterruptedException, violating the unchecked-exceptions rule and losing the interrupt status

The new private method limited is declared throws InterruptedException, a checked exception. Per OFJAVA-014 all exceptions in this codebase must be unchecked, with no throws clauses in signatures. Worse, because this call runs inside executor.submit(() -> limited(call, key)), the InterruptedException is captured by the Future and only surfaces later through resultOf, which only special-cases RuntimeException — an InterruptedException thrown by IN_FLIGHT.acquire() is not a RuntimeException, so future.resultNow() will throw IllegalStateException (wrong state) instead of propagating the real cause, and the thread's interrupt status is silently lost (never re-set with Thread.currentThread().interrupt()). This can turn a shutdown/interrupt signal into a confusing IllegalStateException, and defeats cooperative cancellation of the virtual-thread pool.

Evidence
    private static <T, R> R limited(Function<T, R> call, T key) throws InterruptedException {
        IN_FLIGHT.acquire();
        try {
            return call.apply(key);
        } finally {
            IN_FLIGHT.release();
        }
    }

📝 Committable suggestion

Suggested change
private static <T, R> R limited(Function<T, R> call, T key) throws InterruptedException {
IN_FLIGHT.acquire();
try {
return call.apply(key);
} finally {
IN_FLIGHT.release();
}
}
private static <T, R> R limited(Function<T, R> call, T key) {
try {
IN_FLIGHT.acquire();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IllegalStateException("interrupted while waiting for Fleet call slot", e);
}
try {
return call.apply(key);
} finally {
IN_FLIGHT.release();
}
}
🤖 Prompt for AI agents
In openframe-api-lib/src/main/java/com/openframe/api/service/rmm/fleet/FleetCalls.java around lines 31-38, address this code-review finding: limited() declares a checked throws InterruptedException, violating the unchecked-exceptions rule and losing the interrupt status.
The new private method `limited` is declared `throws InterruptedException`, a checked exception. Per OFJAVA-014 all exceptions in this codebase must be unchecked, with no `throws` clauses in signatures. Worse, because this call runs inside `executor.submit(() -> limited(call, key))`, the InterruptedException is captured by the Future and only surfaces later through `resultOf`, which only special-cases RuntimeException — an InterruptedException thrown by `IN_FLIGHT.acquire()` is not a RuntimeException, so `future.resultNow()` will throw `IllegalStateException` (wrong state) instead of propagating the real cause, and the thread's interrupt status is silently lost (never re-set with `Thread.currentThread().interrupt()`). This can turn a shutdown/interrupt signal into a confusing IllegalStateException, and defeats cooperative cancellation of the virtual-thread pool.
The flagged code:
```
    private static <T, R> R limited(Function<T, R> call, T key) throws InterruptedException {
        IN_FLIGHT.acquire();
        try {
            return call.apply(key);
        } finally {
            IN_FLIGHT.release();
        }
    }
```
A suggested replacement for the flagged range:
    private static <T, R> R limited(Function<T, R> call, T key) {
        try {
            IN_FLIGHT.acquire();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new IllegalStateException("interrupted while waiting for Fleet call slot", e);
        }
        try {
            return call.apply(key);
        } finally {
            IN_FLIGHT.release();
        }
    }
Make the minimal change that resolves the finding; do not refactor unrelated code.

confidence: verifier 75 · author 78 — react 👍/👎 to teach the reviewer


private static <R> R resultOf(Future<R> future) {
if (future.state() == Future.State.FAILED && future.exceptionNow() instanceof RuntimeException e) {
throw e;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.openframe.api.service.rmm.fleet;

import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.IntStream;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

class FleetCallsTest {

@Test
void inParallel_moreCallsThanTheCap_neverMoreInFlightAndResultsKeepKeyOrder() {
// setup
AtomicInteger inFlight = new AtomicInteger();
AtomicInteger peak = new AtomicInteger();
List<Integer> keys = IntStream.range(0, 30).boxed().toList();

// execution
List<Integer> results = FleetCalls.inParallel(keys, key -> {
peak.accumulateAndGet(inFlight.incrementAndGet(), Math::max);
sleep(30);
inFlight.decrementAndGet();
return key * 10;
});

// verifications
assertThat(results).containsExactlyElementsOf(keys.stream().map(key -> key * 10).toList());
assertThat(peak.get()).isBetween(2, FleetCalls.MAX_IN_FLIGHT);
}

@Test
void inParallel_failingCall_rethrowsItsException() {
// execution
IllegalStateException ex = assertThrows(IllegalStateException.class,
() -> FleetCalls.inParallel(List.of(1, 2), key -> {
if (key == 2) {
throw new IllegalStateException("fleet down");
}
return key;
}));

// verifications
assertThat(ex.getMessage()).isEqualTo("fleet down");
}

private static void sleep(long millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
Loading