Skip to content

Remove the per-Bus unparking thread - #42

Open
updawg wants to merge 1 commit into
jonhoo:mainfrom
updawg:inline-unpark
Open

Remove the per-Bus unparking thread#42
updawg wants to merge 1 commit into
jonhoo:mainfrom
updawg:inline-unpark

Conversation

@updawg

@updawg updawg commented Jun 1, 2026

Copy link
Copy Markdown

Bus::new spawns a dedicated thread whose only job is to call unpark() so that broadcast_inner doesn't have to. The handle is dropped, so that thread runs for the entire lifetime of the Bus and there's no way to join it.

That means every Bus permanently owns a background thread. In a process that creates a lot of buses, or in tests that assert no threads are leaked, the lingering thread is awkward to account for.

This drops the thread and calls unpark() inline in broadcast_inner again. The trade-off is that the unpark is back on the broadcast path (only when there's actually a blocked receiver to wake) — the cost the thread was added to hide. For my use that's the better trade, but if you'd rather keep the optimization I'm happy to put it behind a feature flag or join the thread in Drop instead.

Existing tests pass.

Bus::new spawns a thread whose only job is to call unpark() so that
broadcast_inner doesn't have to. Its JoinHandle is dropped, so the thread
lives for the whole life of the Bus and can never be joined.

Unpark inline in broadcast_inner instead and drop the thread. That puts the
unpark back on the broadcast path (only when there's a blocked receiver to
wake), but a Bus no longer holds a background thread it can't clean up.
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.

1 participant