spring-framework icon indicating copy to clipboard operation
spring-framework copied to clipboard

Kotlin: R2DBC pool leaks connections on request cancellation

Open artbasov opened this issue 1 month ago • 2 comments

Hi

Affects: Spring Boot (3.5.7), Spring Framework (6.2.12), namely spring-r2dbc, i think

Made a simple reproducer with detailed explanation and steps. Please, find it here.

Shortly, in Kotlin Spring Boot Webflux application with data-r2dbc and postgresql driver, When request is cancelled during query execution and cancellation caught to insert another entity within separate transaction (Via TransactionalOperator), the connection that were handling the query become leaked (within idle in transaction state with ClientRead wait_event and no longer gets leased by the ConnectionPool

With C such request-cancel operations, where C = spring.r2dbc.pool.max-size (10 by default), the service become unresponsive (infinite wait on pool to acquire connection).

Not sure if this is the right place since I wasn't able to repeat this behavior with Java, so it might be related to kotlinx-coroutines-reactor.

Starting from here due to error messages

2025-11-07T15:38:30.257+03:00  WARN 49567 --- [spring-cpool-leak] [ctor-http-nio-4] reactor.core.publisher.FluxUsingWhen     : Async resource cleanup failed after cancel

java.lang.IllegalStateException: Transaction synchronization is not active
	at org.springframework.transaction.reactive.TransactionSynchronizationManager.getSynchronizations(TransactionSynchronizationManager.java:233) ~[spring-tx-6.2.12.jar:6.2.12]
	at org.springframework.transaction.reactive.AbstractReactiveTransactionManager.triggerBeforeCompletion(AbstractReactiveTransactionManager.java:654) ~[spring-tx-6.2.12.jar:6.2.12]
	at org.springframework.transaction.reactive.AbstractReactiveTransactionManager.processRollback(AbstractReactiveTransactionManager.java:548) ~[spring-tx-6.2.12.jar:6.2.12]
	at org.springframework.transaction.reactive.AbstractReactiveTransactionManager.lambda$rollback$43(AbstractReactiveTransactionManager.java:535) ~[spring-tx-6.2.12.jar:6.2.12]
<reactor + kotlinx.coroutines stack>

That might suggest that there are some problems with R2dbcTransactionManager

artbasov avatar Nov 07 '25 13:11 artbasov