Pooled clients leak a connection per call: pipelines and transactions taken from UnifiedJedis are never closed
评估
调研方向
阅读 AbstractClient.java 中关于批量和定期入队操作的引用方法,然后阅读 AbstractWorker.java 中关于 worker 故障处理的方法。确认 pipeline 和 transaction 如何获取和关闭,并添加回归测试,使调用次数超过有界池的 maxTotal。完成标准是这些调用将连接归还到池中,且不会耗尽连接池。
由索引模型根据 Issue 内容生成。
描述
Version: jesque 3.0.0 with its jedis 7.2.1. The code is unchanged on master (e4d551d, jedis 8.0.0).
What happens
These calls each take a connection from the client's UnifiedJedis pool and never return it:
ClientPoolImpl.batchEnqueue(...), through a pipeline;ClientPoolImpl.recurringEnqueue(...)andClientPoolImpl.removeRecurringEnqueue(...), through a transaction;WorkerPoolImplfailure handling, through a transaction, when theFailQueueStrategyreturnsgetFailQueueMaxItems(...) > 0.
With a bounded pool, the call after maxTotal such calls fails:
redis.clients.jedis.exceptions.JedisException: Could not get a resource from the pool
Caused by: java.util.NoSuchElementException: Timeout waiting for idle object, maxWaitDuration=PT1S
at redis.clients.jedis.util.Pool.getResource(Pool.java:42)
...
at redis.clients.jedis.UnifiedJedis.pipelined(UnifiedJedis.java:6001)
at redis.clients.jedis.RedisClient.pipelined(RedisClient.java:167)
at net.greghaines.jesque.client.AbstractClient.doBatchEnqueue(AbstractClient.java:210)
That trace is from master (jedis 8.0.0). In the worker, the exception is caught and logged at WARN, so failures silently stop being recorded. With the default pool config (unbounded, non-blocking), nothing throws. Instead, every call leaves one more connection open on Redis: 50 batch enqueues took CLIENT LIST from 2 to 51 connections.
Why
UnifiedJedis.pipelined() and UnifiedJedis.multi() create the pipeline or transaction on a connection taken from the pool, with closeConnection = true, and only their close() returns it:
https://github.com/redis/jedis/blob/26bbb8e33af4efe569d8b7d1126329e257f79e0b/src/main/java/redis/clients/jedis/UnifiedJedis.java#L5129-L5137
https://github.com/redis/jedis/blob/26bbb8e33af4efe569d8b7d1126329e257f79e0b/src/main/java/redis/clients/jedis/UnifiedJedis.java#L5150-L5158
Jesque calls sync() or exec(), but never close():
- batch enqueue: https://github.com/gresrun/jesque/blob/2351628e4a7ee12e58b01a85ab93e314dfb9021d/src/main/java/net/greghaines/jesque/client/AbstractClient.java#L204-L216
- recurring enqueue: https://github.com/gresrun/jesque/blob/2351628e4a7ee12e58b01a85ab93e314dfb9021d/src/main/java/net/greghaines/jesque/client/AbstractClient.java#L363-L384
- recurring removal: https://github.com/gresrun/jesque/blob/2351628e4a7ee12e58b01a85ab93e314dfb9021d/src/main/java/net/greghaines/jesque/client/AbstractClient.java#L403-L422
- worker fail queue: https://github.com/gresrun/jesque/blob/2351628e4a7ee12e58b01a85ab93e314dfb9021d/src/main/java/net/greghaines/jesque/worker/AbstractWorker.java#L569-L574
In 2.3.0 these calls ran inside PoolUtils.doWorkInPool, which returns the Jedis to the pool, so the leak is new in 3.0.0:
The non-pooled ClientImpl and WorkerImpl aren't affected: closing a pipeline or transaction of a plain Jedis leaves its connection open.
How to reproduce
- Create a pooled client with
maxTotal = 3,blockWhenExhausted = trueandmaxWait = 1s: aJedisPooledon jedis 7.x, orPoolUtils.createJedisPool(config, poolConfig)on master. - Pass it to a
ClientPoolImpl. - Call
client.batchEnqueue("q", List.of(job))four times. - The 4th call fails as above, although nothing else uses the pool.
Fix
Close the pipelines and transactions with try-with-resources. AbstractPipeline and AbstractTransaction implement Closeable without a checked exception. I'll open a PR with the fix and regression tests that run more calls than a bounded pool's maxTotal.
Workaround
Subclass ClientPoolImpl, override doBatchEnqueue (and the recurring methods, if you use them), and close the pipeline or transaction there.
- 主要语言
- Java
- 星标
- 629
- 派生
- 132
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
gresrun/jesque 的其他 Issue
-
Latest jedis SDK support?可能重新可做 @gresrun 于 296 天前认领,目前没有进行中的 PR。 未关闭Discussion
-
难度 3/5 1-2 天 新手友好度 38/100
-
难度 3/5 1-2 天 新手友好度 45/100
-
难度 5/5 一周以上 新手友好度 25/100
-
难度 3/5 1-2 天 新手友好度 35/100
相似的 Issue
-
难度 1/5 1 小时以内 新手友好度 75/100
521xueweihan/HelloGitHub#4105 ·
-
Awaiting further input from submitter
难度 2/5 1-3 小时 新手友好度 76/100
-
bug
难度 2/5 1-3 小时 新手友好度 82/100
-
Regression in 5.9.0: CuratorCache fails with ClassCastException when the client is not a CuratorFrameworkBase可能已有人在做 @sftwnd 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 76/100
-
bug
难度 2/5 1-3 小时 新手友好度 70/100
维护者通常 1 天内回复