Skip to content

Release thread-bound connection when closing sticky connection resources - #3496

Open
spor3006 wants to merge 1 commit into
spring-projects:mainfrom
spor3006:fix/2148-sticky-connection-refcount
Open

spor3006 wants to merge 1 commit into
spring-projects:mainfrom
spor3006:fix/2148-sticky-connection-refcount

Conversation

@spor3006

Copy link
Copy Markdown
  • You have read the Spring Data contribution guidelines.
  • You use the code formatters provided here and have them applied to your changes. Don’t submit any formatting related changes. (I matched the surrounding style by hand rather than running the IDE formatter; there are no formatting-only changes.)
  • You submit test cases (unit or integration tests) that back your changes.
  • You added yourself as author in the headers of the classes you touched. Amend the date range in the Apache license header if needed. For new types, add the license header (copy from another file and set the current year only).

Fixes #2148

Calling scan() (or opsForSet/Hash/ZSet().scan()) inside execute(SessionCallback) leaks the session's connection. executeWithStickyConnection picks up the connection bound to the thread and increments the holder's reference count, but the cursor later closes that connection directly instead of releasing the reference. The session ends up with a closed connection, the count never gets back to zero, and the holder stays bound to the thread.

Following the feedback on #3457, this keeps using the bound connection and fixes the reference counting instead. When the sticky connection is the thread-bound one, the callback gets a proxy whose close() goes through RedisConnectionUtils.releaseConnection(…). Cursors returned from the proxy are wrapped so that closing them releases the reference the same way rather than closing the bound connection. The release happens at most once per sticky call. Without a bound connection nothing changes: the cursor closes its own connection as before.

Cursors obtained through the command interfaces (e.g. connection.keyCommands().scan(…)) in a custom executeWithStickyConnection callback are not wrapped; all in-tree callers use the top-level methods. Happy to extend this if you prefer.

Tests: new RedisTemplateUnitTests cases with a mocked connection factory check that inside a session the bound connection is not closed by the cursor, is unbound from TransactionSynchronizationManager after the session, and is closed exactly once; and that outside a session closing the cursor still closes the connection. Two of them fail on current main. The existing RedisTemplate, RedisConnectionUtils, cursor and Default*Operations unit tests pass.

…ces.

executeWithStickyConnection(…) obtains its connection via RedisConnectionUtils and increments the reference count of a connection bound to the current thread, for example within execute(SessionCallback). Cursors returned from SCAN commands closed the bound connection directly, so the session lost its connection and the reference count never dropped back to zero, leaving the closed connection bound to the thread.

We now hand out a proxy for bound connections whose close(), and the close() of cursors obtained from it, releases the reference through RedisConnectionUtils.releaseConnection(…) instead of closing the bound connection.

Closes spring-projects#2148

Signed-off-by: Sparshal Kothari <41056517+spor3006@users.noreply.github.com>
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 11, 2026
@mp911de mp911de added status: waiting-for-triage An issue we've not yet triaged theme: transactions Transaction management, resource bindings, … and removed status: waiting-for-triage An issue we've not yet triaged labels Nov 16, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: waiting-for-triage An issue we've not yet triaged theme: transactions Transaction management, resource bindings, …

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Connection leak while using execute(SessionCallback) and executeWithStickyConnection

3 participants