On Thu, 2 Jul 2026 21:45:07 GMT, Benjamin Peterson <[email protected]> 
wrote:

> Make `MinimalFuture` cancelation atomic. This prevents HTTP request 
> cancelation logic from racily completing the future with an error.
> 
> 
> 
> 
> ---------
> - [x] I confirm that I make this contribution in accordance with the [OpenJDK 
> Interim AI Policy](https://openjdk.org/legal/ai).

I'd suggest adding the following scenario to 
`test/jdk/java/net/httpclient/whitebox/java.net.http/jdk/internal/net/http/common/MinimalFutureTest.java`:

    @Test
    public void testCancel() {
        AtomicInteger cancelCount = new AtomicInteger();
        AtomicBoolean cancelled   = new AtomicBoolean();
        Cancelable cancelable = mayInterruptIfRunning -> {
            cancelCount.incrementAndGet();
            if (mayInterruptIfRunning) {
                cancelled.set(true);
            }
            return cancelled.get();
        };
        MinimalFuture<Object> future = new MinimalFuture<>(cancelable);
        CompletableFuture dependent = future.copy().whenComplete((x,t) ->
                System.out.println("expected: " + t));
        assertTrue(dependent.cancel(false));
        assertTrue(dependent.isCancelled());
        assertFalse(future.isCancelled());
        assertFalse(cancelled.get());
        assertEquals(1, cancelCount.get());
        assertTrue(dependent.cancel(true));
        assertTrue(dependent.isCancelled());
        assertFalse(future.isCancelled());
        assertTrue(cancelled.get());
        assertEquals(2, cancelCount.get());
    }

src/java.net.http/share/classes/jdk/internal/net/http/common/MinimalFuture.java 
line 106:

> 104:     public boolean cancel(boolean mayInterruptIfRunning) {
> 105:         if  (!super.cancel(mayInterruptIfRunning)) {
> 106:             return false;

Suggestion:

            assert super.isDone();
            return false;

test/jdk/java/net/httpclient/CancelRequestTest.java line 530:

> 528:             boolean hasCancellationException = false;
> 529:             try {
> 530:                 cf1.get();

It looks like you should apply the same changes as for the GET case here.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/31758#issuecomment-4894752691
PR Review Comment: https://git.openjdk.org/jdk/pull/31758#discussion_r3529647547
PR Review Comment: https://git.openjdk.org/jdk/pull/31758#discussion_r3529672811

Reply via email to