lettuce icon indicating copy to clipboard operation
lettuce copied to clipboard

Lettuce clientOptions config & NioSocketChannel may trigger memory leak

Open adar-v opened this issue 8 months ago • 2 comments

Bug Report

Current Behavior

I use spring webflux and lettuce. When I want to set the lettuce worker thread not on the io thread, my service will find that the memory surges every once in a while, and the service will become unresponsive and stuck after a while.

Stack trace
lettuce-nioEventLoop-7-1
  at sun.nio.ch.EPoll.wait(IJII)I (EPoll.java(Native Method))
  at sun.nio.ch.EPollSelectorImpl.doSelect(Ljava/util/function/Consumer;J)I (EPollSelectorImpl.java:118)
  at sun.nio.ch.SelectorImpl.lockAndDoSelect(Ljava/util/function/Consumer;J)I (SelectorImpl.java:129)
  at sun.nio.ch.SelectorImpl.select()I (SelectorImpl.java:146)
  at io.netty.channel.nio.SelectedSelectionKeySetSelector.select()I (SelectedSelectionKeySetSelector.java:68)
  at io.netty.channel.nio.NioEventLoop.select(J)I (NioEventLoop.java:813)
  at io.netty.channel.nio.NioEventLoop.run()V (NioEventLoop.java:460)
  at io.netty.util.concurrent.SingleThreadEventExecutor$4.run()V (SingleThreadEventExecutor.java:997)
  at io.netty.util.internal.ThreadExecutorMap$2.run()V (ThreadExecutorMap.java:74)
  at io.netty.util.concurrent.FastThreadLocalRunnable.run()V (FastThreadLocalRunnable.java:30)
  at java.lang.Thread.run()V (Thread.java:833)

Input Code

Input Code Old configuration mode, no memory leak occurs
    @Bean
    public ReactiveRedisConnectionFactory reactiveRedisConnectionFactory() {
        return new LettuceConnectionFactory(aliRedisProperties.getHost(), aliRedisProperties.getPort());
    }

New configuration mode, will trigger memory leak occurs

    @Bean
    public ReactiveRedisConnectionFactory reactiveRedisConnectionFactory() {
        LettuceClientConfiguration configuration = LettuceClientConfiguration.builder()
                .clientOptions(ClientOptions.builder().publishOnScheduler(true).build())
                .build();
        return new LettuceConnectionFactory(new RedisStandaloneConfiguration(aliRedisProperties.getHost(), aliRedisProperties.getPort()), configuration);
    }

Expected behavior/code

Environment

  • spring framework: 2.6.9
  • Lettuce version(s): 6.1.8.Release
  • Redis version: 5.0.6
  • Netty: 4.1.78

Possible Solution

Additional context

memory dump

Image
one instance of io.netty.channel.socket.nio.NioSocketChannel loaded by org.springframework.boot.loader.LaunchedURLClassLoader @ 0x6458005b0 occupies 2,443,638,760 (91.93%) bytes. The memory is accumulated in one instance of java.lang.Object[], loaded by <system class loader>, which occupies 2,443,636,752 (91.93%) bytes.

Thread io.netty.util.concurrent.FastThreadLocalThread @ 0x645bb4888 lettuce-nioEventLoop-7-1 has a local variable or reference to sun.nio.ch.EPollSelectorImpl @ 0x645bbe688 which is on the shortest path to java.lang.Object[550104] @ 0x6e43aa798. The thread io.netty.util.concurrent.FastThreadLocalThread @ 0x645bb4888 lettuce-nioEventLoop-7-1 keeps local variables with total size 29,232 (0.00%) bytes.

Significant stack frames and local variables

sun.nio.ch.EPollSelectorImpl.doSelect(Ljava/util/function/Consumer;J)I (EPollSelectorImpl.java:118)
sun.nio.ch.EPollSelectorImpl @ 0x645bbe688 retains 832 (0.00%) bytes
sun.nio.ch.SelectorImpl.lockAndDoSelect(Ljava/util/function/Consumer;J)I (SelectorImpl.java:129)
sun.nio.ch.EPollSelectorImpl @ 0x645bbe688 retains 832 (0.00%) bytes

adar-v avatar May 03 '25 06:05 adar-v

Hey @adar-v ,

thanks for reporting this issue. Could you help us reproduce this so we can solve it? Can you build up a minimum reproducible example that showcases the issue, or is it very random? How often does it happen?

tishun avatar May 07 '25 07:05 tishun

  1. I found that after changing to another connection mode, it also happens, but the frequency seems to have decreased.
  2. This problem occurs very randomly, and no time characteristics or behavioral characteristics have been found, so a demo cannot be provided.

adar-v avatar May 15 '25 02:05 adar-v

I found the same problem. I also use spring webflux and lettuce, and same configuration code .

configuration code :

` @Configuration public class LettuceCustomizer implements LettuceClientConfigurationBuilderCustomizer {

@Override
public void customize(LettuceClientConfiguration.LettuceClientConfigurationBuilder clientConfigurationBuilder) {
    clientConfigurationBuilder.clientOptions(ClientOptions.builder().publishOnScheduler(true).build());
}

}

`

angrey avatar Jun 24 '25 06:06 angrey

  1. I found that after changing to another connection mode, it also happens, but the frequency seems to have decreased.
  2. This problem occurs very randomly, and no time characteristics or behavioral characteristics have been found, so a demo cannot be provided.

@adar-v How to change another connection mode ? Thanks .

angrey avatar Jun 24 '25 06:06 angrey