-
Notifications
You must be signed in to change notification settings - Fork 90
fix(ckpt): reap finished tasks in accept loop #2554
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
Open
qiwu575757
wants to merge
1
commit into
alibaba:main
Choose a base branch
from
qiwu575757:fix/ckpt/listener-joinset-leak
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+34
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,6 +62,14 @@ pub async fn run_listener( | |
| break; | ||
| } | ||
| } | ||
|
|
||
| // Reap finished connections. JoinSet holds each task's handle and output | ||
| // slot until it is joined, so without this the set grows by one entry per | ||
| // CLI invocation and is only released at shutdown. try_join_next never | ||
| // awaits, so reaping cannot delay the next accept; a join_next branch in | ||
| // the select! above would instead need a second mutable borrow of | ||
| // join_set while the accept arm still spawns into it. | ||
| while join_set.try_join_next().is_some() {} | ||
| } | ||
|
|
||
| // 7. Wait for in-flight tasks to complete (with timeout) | ||
|
|
@@ -136,3 +144,29 @@ async fn handle_connection( | |
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 删除这个测试,因为他并没有测到你修的问题,只测试了 tokio 包本身的功能。 |
||
| use tokio::task::JoinSet; | ||
|
|
||
| /// Pins the semantics the accept loop relies on: completed tasks stay in the | ||
| /// set until reaped, and `try_join_next` drains them without awaiting. If the | ||
| /// reap call in the accept loop is dropped, the daemon leaks one entry per | ||
| /// connection, which is what caused ~8 KB of growth per CLI invocation. | ||
| #[tokio::test] | ||
| async fn try_join_next_reaps_completed_tasks() { | ||
| let mut set = JoinSet::new(); | ||
| for _ in 0..16 { | ||
| set.spawn(async {}); | ||
| } | ||
| while !set.is_empty() { | ||
| let before = set.len(); | ||
| if set.try_join_next().is_none() { | ||
| tokio::task::yield_now().await; | ||
| continue; | ||
| } | ||
| assert_eq!(set.len(), before - 1, "reaping must release one entry"); | ||
| } | ||
| assert!(set.try_join_next().is_none(), "empty set must not block"); | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.