fix(redis): prevent defer callback accumulation in long-lived coroutines#7734
Merged
Conversation
When `pipeline(callback)` or `transaction(callback)` is called repeatedly in a long-lived coroutine (e.g. while(true) loop), each call registers a new `defer` closure via `__call()`, but the outer `executeMultiExec()` finally block immediately clears the context connection. Since `Context::has()` uses `isset()` which returns false for null values, the next call sees no existing connection and registers yet another defer. These defer closures accumulate indefinitely because the coroutine never terminates, causing a slow memory leak. Fix: Move the `Context::has()` check into the finally block so it evaluates after `__call()` has stored the connection. This way the connection stays in context and is reused on subsequent calls, with only one defer ever registered per coroutine. Made-with: Cursor
huangdijia
previously approved these changes
Apr 13, 2026
Member
|
此改动 如果是一个while true协程 多次调用 |
Contributor
Author
确实存在这个问题。 我看了单测的代码,3个连接,但是启动了20个协程并发,因为连接的释放是在defer中才释放,而不是在每次命令执行完成之后释放,所以连接池会耗尽。 看了下连接复用是想要在 我先看看代码,大佬们看下有没有什么建议。 |
Member
Contributor
Author
|
我想了一下,如果还是放 如此说来,需要在每次执行完毕后,立即归还链接。但是我没想好怎么改,以及影响范围。 顺便mark下,defer泄漏由此次优化引入:#7394 |
Contributor
Author
|
使用 |
Made-with: Cursor
Made-with: Cursor
huangdijia
reviewed
Apr 13, 2026
…e methods Made-with: Cursor
huangdijia
previously approved these changes
Apr 13, 2026
limingxinleo
approved these changes
Apr 21, 2026
Member
|
代码我合进来了,你测试下,我改了一行代码。 |
Contributor
Author
|
测试了下,没问题 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

fix: #7733