Skip to content

fix(server): Use tokio::select when waiting for connection close/global graceful shutdown - #30

Merged
raykroeker merged 5 commits into
linkerd:mainfrom
thesw4rm:fix-serve-conn-leak
Nov 2, 2026
Merged

raykroeker merged 5 commits into
linkerd:mainfrom
thesw4rm:fix-serve-conn-leak

Conversation

@thesw4rm

@thesw4rm thesw4rm commented Oct 7, 2026 •

Copy link
Copy Markdown

Solves linkerd/linkerd2#15684

BUG

Connections were not being dropped from memory after being closed. server_conn was waiting on a release shutdown signal. This was a regression likely caused by the hyper 0.14 to 1 bump in a95478a

FIX

Switch back to a tokio::select pattern. Branch predictor is optimized to check the connection being closed first, before polling for a shutdown signal.

Signed-off-by: Yadunandan Pillai contact@mail.thesw4rm.com

@raykroeker

Copy link
Copy Markdown
Member

Thanks for sending the PR. Some feedback to consider:

  1. Housekeeping. Have a look at the sign-off in CONTRIBUTING.md and DCO it'd be great to signoff on all commits.
  2. Given we're no longer using graceful, drop the feature from Cargo.toml.
  3. The version change to Cargo.lock isn't tied to this change.
  4. Add some testing that demonstrates the validity of the tokio::select pattern.

@raykroeker raykroeker self-assigned this Oct 8, 2026
@thesw4rm
thesw4rm force-pushed the fix-serve-conn-leak branch 6 times, most recently from 555de8d to ccace6d Compare October 8, 2026 23:37
@thesw4rm

thesw4rm commented Oct 8, 2026 •

Copy link
Copy Markdown
Author

@raykroeker

Appreciate it!

1, Signoff passes DCO now.
2. Graceful is dropped from Cargo.toml
3. Cargo.lock looks like it is out of sync. My build keeps changing it. I've kept it 0.26.0 for now, but kubert package is 0.27.0
4. Unit tests added. They consistently fail on the old pattern, and pass on the new one. I'm not sure that this test should be kept in there though. The bug in the previous code was, from what I've understood, a one-off instance rather than behavior we expect to see again.

The earlier pattern came as a result of the upgrade from Hyper 0.14 to
Hyper 1.x. We were blocking on the global shutdown signal after each
connection, causing the connection structs to stay allocated in memory.
Over a flood of many connections (even after they close), this was
causing significant memory leaks.

Signed-off-by: Yadunandan Pillai <contact@mail.thesw4rm.com>
Signed-off-by: Yadunandan Pillai <contact@mail.thesw4rm.com>
Signed-off-by: Yadunandan Pillai <contact@mail.thesw4rm.com>
@thesw4rm
thesw4rm force-pushed the fix-serve-conn-leak branch from ccace6d to 8ce4f15 Compare October 8, 2026 23:55
@thesw4rm thesw4rm changed the title Use tokio::select when waiting for connection close/global graceful shutdown fix(server): Use tokio::select when waiting for connection close/global graceful shutdown Oct 9, 2026
@thesw4rm

thesw4rm commented Oct 9, 2026 •

Copy link
Copy Markdown
Author

Hey @raykroeker . Looks like the issue reporter confirmed that linkerd/linkerd2#15684 is likely directly related to the fix (same reproduction steps).

They're not comfortable building their own image. Do you think this PR is ready to merge, so they can confirm the fix on their end?

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Yadunandan Pillai <contact@mail.thesw4rm.com>
@thesw4rm

Copy link
Copy Markdown
Author

@raykroeker bumping this one, if you've got the time!

@anthoturc

anthoturc commented Nov 2, 2026 •

Copy link
Copy Markdown

The PR LGTM but it needs a rebase (edit: apparently it does not need a rebase :D).

A nice-to-have would be a test for ensuring that shutdown closes in-flight connections gracefully. I don't think that path was tested prior to this change and it isn't strictly related but I'm calling it out because you did alter the shutdown path a little bit.

Based on the observed behavior in linkerd/linkerd2#15684, this fix makes sense to me. There was never a mechanism for the parent task to exit when the spawned child task was done.

// tasks. Each one is dropped when its task completes.
server.abort();
let _ = server.await;
tokio::time::timeout(std::time::Duration::from_secs(2), drain_tx.closed())

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there anything special about using a two second timeout here?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really. It shouldn't ever take more than a few hundred milliseconds to drain.

@anthoturc
anthoturc requested a review from raykroeker November 2, 2026 16:10
@anthoturc

Copy link
Copy Markdown

let's get @raykroeker's sign off as well. i'll poke today. thanks for waiting!

@raykroeker
raykroeker merged commit 657b3a2 into linkerd:main Nov 2, 2026
32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants