Skip to content
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

Clean up sendability in the bootstraps #3051

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

Lukasa
Copy link
Contributor

@Lukasa Lukasa commented Jan 13, 2025

Motivation:

The bootstraps are the next most obvious target for cleaning up Sendability issues. These issues are mostly just missing annotations, but there are a few places where we had actual latent threading bugs that were missed. A lot more of the control flow is made more explicit in this patch, and in general it should get a lot easier to be confident that the code is correct.

Modifications:

  • Add necessary @Sendable annotations
  • Clean up some incorrect self captures of bootstraps, which are not Sendable, and which could lead to real threading bugs
  • Make a few methods static to avoid needing to capture self at all.
  • Make a few threading assumptions clear by using the isolated views or the sync operations
  • Add some missing Sendable constraints on interior generic functions.

Result:

Better safety, better correctness in the bootstraps.

@Lukasa Lukasa added the 🔨 semver/patch No public API change. label Jan 13, 2025
@@ -474,17 +479,17 @@ public final class ServerBootstrap {
func fireThroughPipeline(_ future: EventLoopFuture<Void>, context: ChannelHandlerContext) {
ctxEventLoop.assertInEventLoop()
assert(ctxEventLoop === context.eventLoop)
let loopBoundContext = context.loopBound
let loopBoundValues = NIOLoopBound((context: context, self: self), eventLoop: context.eventLoop)
Copy link
Member

Choose a reason for hiding this comment

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

Can't we use the assume isolated APIs here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, we can. Updated.

Copy link
Member

Choose a reason for hiding this comment

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

Love it!

@Lukasa
Copy link
Contributor Author

Lukasa commented Jan 13, 2025

Unfortunately this has regressed allocations. For example, in test_1000_tcpconnections we've seen 6 extra allocations per loop iteration. These appear to all be coming from the isolated views, so we should take a moment to clean them up.

@Lukasa
Copy link
Contributor Author

Lukasa commented Jan 13, 2025

Ok, hopefully #3055 should have resolved those regressions, so let's see.

@Lukasa Lukasa force-pushed the cb-clean-up-sendability-in-the-bootstraps branch 2 times, most recently from 5840d79 to 31cea7b Compare January 14, 2025 16:39
@Lukasa
Copy link
Contributor Author

Lukasa commented Jan 14, 2025

Oh, it not only resolved those regressions, but led to a benchmark improvement. That's nice!

@Lukasa Lukasa force-pushed the cb-clean-up-sendability-in-the-bootstraps branch from d82ccfd to 296ac85 Compare January 14, 2025 17:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔨 semver/patch No public API change.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants