jedis
jedis copied to clipboard
JedisSentinelPool is not thread safe. It can create sentinel connection leak
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.
See https://github.com/xetorthio/jedis/issues/1910
hi @gkorland 1910 is not same as this issue. this issue is only related to shutdown of JedisSentinelPool
@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 This fix is not merged. I didn't received response on this thread. However I have tested this change and it works perfectly.
@akshay12390 can you describe the problem you faced?
@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 that is the exact problem I faced. You can try using my patch
@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.
This issue is marked stale. It will be closed in 30 days if it is not updated.