Pooled clients leak a connection per call: pipelines and transactions taken from UnifiedJedis are never closed
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 77/100
Hướng nghiên cứu
Đọc các phương thức được dẫn trong AbstractClient.java về thao tác đưa vào hàng đợi theo lô và định kỳ, sau đó đọc AbstractWorker.java về xử lý lỗi của worker. Xác nhận cách các pipeline và transaction được lấy và đóng, đồng thời bổ sung các kiểm thử hồi quy thực hiện số lời gọi nhiều hơn maxTotal của một pool có giới hạn. Hoàn thành khi các lời gọi đó trả kết nối về pool mà không làm cạn pool.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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.
- Ngôn ngữ chính
- Java
- Star
- 629
- Fork
- 132
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của gresrun/jesque
-
Latest jedis SDK support?Có thể làm lại được @gresrun đã nhận 295 ngày trước và không có pull request nào đang mở. Đang mởDiscussion
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 38/100
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 45/100
-
Redis Cluster IssueĐang mở
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 25/100
-
NoSuchConstructorExceptionĐang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 35/100
Tất cả issue của gresrun/jesque
Issue tương tự
-
code-maintenance
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 77/100
Maintainer thường phản hồi trong vòng 1 ngày
-
bug priority: medium Review effort [1-5]: 3 status: needs-triage type: bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
carlos-emr/carlos#5258 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug: author create/update with an unknown avatarId returns 404 instead of 400 UNKNOWN_REFERENCECó thể đã có người làm @Vinu2111 đã nhận hôm nay. Đang mởbug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
atomityhq/blog-cms#59 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
PlatformSpecific.hasGreenThreads incorrectly returns false for JDK updates and non-GA versionsĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
Maintainer thường phản hồi trong vòng 1 ngày
-
dev/release: verify-release-candidate.sh runs 0 Java tests with Debian's MavenCó thể đã có người làm @kou đã nhận hôm nay. Đang mởType: bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
apache/arrow-adbc#4947 ·
Maintainer thường phản hồi trong vòng 1 ngày