Skip to content

Improve resource management for Client-Side Caching and fix RediSearch memory corruption #3526#3624

Open
YoHanKi wants to merge 4 commits intoredis:mainfrom
YoHanKi:feature/caching-resource-management
Open

Improve resource management for Client-Side Caching and fix RediSearch memory corruption #3526#3624
YoHanKi wants to merge 4 commits intoredis:mainfrom
YoHanKi:feature/caching-resource-management

Conversation

@YoHanKi
Copy link

@YoHanKi YoHanKi commented Jan 24, 2026

Make sure that:

  • You have read the contribution guidelines.
  • You have created a feature request first to discuss your contribution intent. Please reference the feature request ticket number in the pull request.
  • You applied code formatting rules using the mvn formatter:format target. Don’t submit any formatting related changes.
  • You submit test cases (unit or integration tests) that back your changes.

Description

This PR addresses two critical issues related to resource management and memory safety:

1. Improve Resource Management in Client-Side Caching (#3526)

  • Problem: DefaultRedisCache did not remove PushListener from the connection when closed, leading to potential memory leaks and redundant invalidation logic. Also, closing a CacheFrontend forcibly closed the underlying connection, which is problematic for shared connection scenarios.
  • Solution:
    • Updated DefaultRedisCache to track and explicitly remove all registered listeners during close().
    • Introduced a closeConnection option (defaulting to true for backward compatibility) to allow users to decide whether the underlying Redis connection should be closed along with the cache.
  • Verification: Added DefaultRedisCacheUnitTests to verify listener removal and connection management.

2. Fix Memory Corruption/JVM Crash in RediSearch

  • Problem: EncodedComplexOutput was storing read-only views of Netty's pooled ByteBuffers. These buffers could be reclaimed or reused before parsing was complete, leading to SIGSEGV or data corruption, especially on Linux/Ubuntu environments with Netty 4.2+.
  • Solution: Modified EncodedComplexOutput to explicitly copy the ByteBuffer content into a new heap-allocated buffer before storing it, ensuring data persistence during the parsing phase.
  • Verification: Added testSearchWithLargeJsonPayloads to RediSearchIntegrationTests and verified the fix in an Ubuntu-based Docker environment with Netty 4.2.4.Final and PARANOID leak detection.

Changes

  • ClientSideCaching: Added overloads for enable and create with closeConnection parameter.
  • DefaultRedisCache: Implemented listener tracking and removal, added closeConnection logic.
  • EncodedComplexOutput: Implemented ByteBuffer copying.
  • DefaultRedisCacheUnitTests: New unit tests for caching resource management.
  • RediSearchIntegrationTests: Added stress test for large JSON payloads.

Fixes #3526

- [Caching] Explicitly remove PushListener from connection on DefaultRedisCache.close() to prevent memory leaks (Fixes redis#3526).
- [Caching] Add 'closeConnection' flag to ClientSideCaching and DefaultRedisCache to allow shared connection scenarios.
- [RediSearch] Implement ByteBuffer copying in EncodedComplexOutput to prevent SIGSEGV/JVM crashes caused by Netty pooled buffer reclamation during parsing.
- Maintain backward compatibility by keeping 'closeConnection' default as true.

Signed-off-by: YoHanKi <alcuz137@gmail.com>
- Add DefaultRedisCacheUnitTests to verify listener removal and connection closing logic using Mockito.
- Add testSearchWithLargeJsonPayloads to RediSearchIntegrationTests to verify memory safety and correct parsing of large search results.
- Ensure all new test classes follow project conventions, including License headers and @tag usage.

Signed-off-by: YoHanKi <alcuz137@gmail.com>
- Reformat RediSearchIntegrationTests.java using 'formatter:format' to comply with project conventions.

Signed-off-by: YoHanKi <alcuz137@gmail.com>
@jit-ci
Copy link

jit-ci bot commented Jan 24, 2026

Hi, I’m Jit, a friendly security platform designed to help developers build secure applications from day zero with an MVS (Minimal viable security) mindset.

In case there are security findings, they will be communicated to you as a comment inside the PR.

Hope you’ll enjoy using Jit.

Questions? Comments? Want to learn more? Get in touch with us.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Memory corruption on large responses for ft.search

1 participant