Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resource leak: JedisPooled does not return connection to the pool on socket exception #3864

Closed
chochos opened this issue Jun 14, 2024 · 4 comments · Fixed by #3865
Closed
Labels

Comments

@chochos
Copy link

chochos commented Jun 14, 2024

JedisPooled is very convenient because you can just issue commands without having to do a try-with-resources for every call.

When using a pipeline, try-with-resources is still necessary. But, if a socket exception occurs, the pipe is closed but the connection is not returned to the pool, so with enough exceptions, the pool is exhausted.

To reproduce, I ran a local redis server and then ran this Groovy script:

import redis.clients.jedis.JedisPooled

var pool = new org.apache.commons.pool2.impl.GenericObjectPoolConfig<redis.clients.jedis.Connection>()
pool.maxTotal=4
pool.maxWait=java.time.Duration.ofMillis(200)

def redis = new JedisPooled(pool, 'localhost', 6379)
def log = org.slf4j.LoggerFactory.getLogger("demo")

while (true) {
    try (var pipe = redis.pipelined()) {
        println pipe.smembers('currencies')
        println "POOL max ${redis.pool.maxTotal} active ${redis.pool.numActive}"
    } catch (RuntimeException ex) {
        log.error("oops, something went wrong", ex)
        println "POOL max ${redis.pool.maxTotal} active ${redis.pool.numActive}"
    }
    Thread.sleep(2000)
}

This will print active connections as 1. Then stop the server, and let the script show an exception; start the server again, and now it will display active connections as 2; you can repeat this until the pool is exhausted and the error changes from JedisConnectionException saying "unexpected end of stream" to a JedisException caused by a NoSuchElementException thrown by the pool.

I have managed to avoid this by enabling borrowOnTrue setting on the pool, but this causes some overhead which is undesirable. I believe the JedisPooled should catch the socket exceptions and discard the connection so the pool can create a new one when needed.

@sazzad16
Copy link
Contributor

@chochos Thank you for raising this.

Hopefully it is fixed. You can find the fix in Jedis 5.2.0-SNAPSHOT shortly.

@Ranjeet116966
Copy link

@chochos I am also facing something similar issue here, can you please check this discussion thread - #4028 and confirm that it's because of the above issue and upgrading the jedis to 5.2.0 could help in resolving the issue TIA.

@sazzad16
Copy link
Contributor

sazzad16 commented Dec 4, 2024

@Ranjeet116966 This issue is quite specific about using Pipeline from JedisPooled. You aren't using Pipeline.

@Ranjeet116966
Copy link

@sazzad16 can you please check the discussion thread and suggest if there is anything else that i can try to get rid of this error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants