jedis icon indicating copy to clipboard operation
jedis copied to clipboard

JedisSentinelPool is not thread safe. It can create sentinel connection leak

Open khemkasudeep opened this issue 6 years ago • 9 comments

Expected behavior

JedisSentinelPool() should create connection to sentinels in MasterListener thread. Then pool.close() should always close that connection

JedisSentinelPool pool = new JedisSentinelPool() pool.close()

Actual behavior

There is a race around condition where connection is not closed some times.

Steps to reproduce:

for(i=0; i<10000;i++){ JedisSentinelPool pool = new JedisSentinelPool(); pool.close(); }

check number of client connections for your sentinel hosts. It should shootup.

alternatively, check how many threads are waiting on MasterListener as below - jstack pid | grep "MasterListener-mastername-" | wc -l

Reason:

This is a race around condition in JedisSentinelPool class.

This can happen if the code execution happens in the order mentioned below.

JedisSentinelPool pool = new JedisSentinelPool(); --> MasterListener().start() --> MasterListener.run --> j = new Jedis(host, port); --> if (!running.get()){} --> pool.close(); -> MasterListener. shutdown -> j.disconnect(); --> j.subscribe();

Basically here creation of MasterListener and its shutdown are interleaved. If we create MasterListener and say the thread is running but has not subscribed for messages with sentinel yet and in the meanwhile if someone one calls shutdown, caller would expect that MasterListener thread should close sentinel connections and eventually stop running.

But instead after shutdown has executed, MasterListener can go and call subscribe which will open a connection to sentinel and will keep listening on it for ever.

khemkasudeep avatar Jun 05 '18 12:06 khemkasudeep

See https://github.com/xetorthio/jedis/issues/1910

gkorland avatar Dec 25 '18 09:12 gkorland

hi @gkorland 1910 is not same as this issue. this issue is only related to shutdown of JedisSentinelPool

khemkasudeep avatar Dec 25 '18 09:12 khemkasudeep

@khemkasudeep we also faced same issue in our application recently. We are using version 2.9.0. Is the fix for this issue available in later versions now?

akshay12390 avatar Feb 10 '20 16:02 akshay12390

@akshay12390 This fix is not merged. I didn't received response on this thread. However I have tested this change and it works perfectly.

khemkasudeep avatar Feb 10 '20 16:02 khemkasudeep

@akshay12390 can you describe the problem you faced?

khemkasudeep avatar Feb 10 '20 16:02 khemkasudeep

@khemkasudeep So in few cases in our application flow path, we were creating sentinel pool and then immediately closing it and thus the master sentinel listeners still subscribed to messages, thereby leaving open connections which were only getting closed when application shutdown hook was getting called.

akshay12390 avatar Feb 10 '20 16:02 akshay12390

@akshay12390 that is the exact problem I faced. You can try using my patch

khemkasudeep avatar Feb 11 '20 03:02 khemkasudeep

@khemkasudeep Hi, mate. Thanks for this Issue to solve. I faced that like you. How to use this JedisSentinelPool class? I am using version Jedis 2.9.0. I understand version 2.10.2 is latest in version 2. Is it work well or a fixed issue? I'm looking forward to your reply.

devil5462 avatar Nov 05 '20 13:11 devil5462

This issue is marked stale. It will be closed in 30 days if it is not updated.

github-actions[bot] avatar Feb 15 '24 00:02 github-actions[bot]