From c29b0fed0c64dab3c5bf82a08ef21701b698af53 Mon Sep 17 00:00:00 2001 From: Violeta Georgieva Date: Tue, 7 Jan 2025 18:40:09 +0200 Subject: [PATCH] Http2Pool: Do not proceed with the acquisition if Borrower is cancelled (#3582) --- .../reactor/netty/http/client/Http2Pool.java | 7 ++++- .../netty/http/client/Http2PoolTest.java | 26 ++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/reactor-netty-http/src/main/java/reactor/netty/http/client/Http2Pool.java b/reactor-netty-http/src/main/java/reactor/netty/http/client/Http2Pool.java index 8682651ebd..f262a3b5a4 100644 --- a/reactor-netty-http/src/main/java/reactor/netty/http/client/Http2Pool.java +++ b/reactor-netty-http/src/main/java/reactor/netty/http/client/Http2Pool.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 VMware, Inc. or its affiliates, All Rights Reserved. + * Copyright (c) 2021-2025 VMware, Inc. or its affiliates, All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -332,6 +332,11 @@ else if (testEvictionPredicate(ref.slot)) { } void doAcquire(Borrower borrower) { + if (borrower.get()) { + // Borrower is cancelled, do nothing + return; + } + if (isDisposed()) { borrower.fail(new PoolShutdownException()); return; diff --git a/reactor-netty-http/src/test/java/reactor/netty/http/client/Http2PoolTest.java b/reactor-netty-http/src/test/java/reactor/netty/http/client/Http2PoolTest.java index d91b476553..03ddc9f883 100644 --- a/reactor-netty-http/src/test/java/reactor/netty/http/client/Http2PoolTest.java +++ b/reactor-netty-http/src/test/java/reactor/netty/http/client/Http2PoolTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 VMware, Inc. or its affiliates, All Rights Reserved. + * Copyright (c) 2021-2025 VMware, Inc. or its affiliates, All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,6 +22,7 @@ import io.netty.handler.codec.http2.Http2FrameCodecBuilder; import io.netty.handler.codec.http2.Http2MultiplexHandler; import org.junit.jupiter.api.Test; +import org.reactivestreams.Subscription; import reactor.core.Disposable; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @@ -46,6 +47,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; import java.util.function.BiFunction; import java.util.concurrent.atomic.AtomicReference; @@ -141,6 +143,28 @@ void acquireRelease() { } } + @Test + void doAcquireNotCalledIfBorrowerInScopeCancelledEarly() { + AtomicInteger allocator = new AtomicInteger(); + PoolBuilder> poolBuilder = + PoolBuilder.from(Mono.fromSupplier(() -> { + allocator.incrementAndGet(); + Channel channel = new EmbeddedChannel( + new TestChannelId(), + Http2FrameCodecBuilder.forClient().build()); + return Connection.from(channel); + })); + + Http2Pool http2Pool = poolBuilder.build(config -> new Http2Pool(config, null)); + + assertThat(allocator).as("before invoking acquire").hasValue(0); + + // Borrower is in state cancelled before the actual acquisition + http2Pool.acquire().doOnSubscribe(Subscription::cancel).subscribe(); + + assertThat(allocator).as("after invoking acquire").hasValue(0); + } + @Test void evictClosedConnection() throws Exception { PoolBuilder> poolBuilder =