Skip to content

Conversation

aryan9600
Copy link

Add support for non Send+Sync closures for thread parking and unparking callbacks when using a LocalRuntime. Since a LocalRuntime will always run its tasks on the same thread, its safe to accept a non Send+Sync closure.

Motivation

Since LocalRuntime can run non Send+Sync functions, we should accept thread parking/unparking callbacks that do not implement Send and Sync.

Solution

Add two methods on LocalOptions, on_thread_park and on_thread_unpark that accept a Fn() + 'static. These callbacks are then converted into Fn() + Send + Sync + 'static. This requires unsafe code, but unsafe is acceptable here because of the behaviour of LocalRuntime

fixes #7370

@aryan9600 aryan9600 force-pushed the non-send-thread-park branch from be90205 to eeb45d3 Compare June 25, 2025 11:16
@aryan9600 aryan9600 force-pushed the non-send-thread-park branch 2 times, most recently from c124e08 to 4f8b562 Compare June 26, 2025 05:34
@mox692 mox692 added A-tokio Area: The main tokio crate M-runtime Module: tokio/runtime labels Jun 27, 2025
@ADD-SP ADD-SP added the C-enhancement Category: A PR with an enhancement or bugfix. label Jun 29, 2025
Copy link
Member

Choose a reason for hiding this comment

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

We need a test to make sure callbacks will not be executed when using the Handle::block_on.

Copy link
Author

Choose a reason for hiding this comment

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

afaiu, we can only test this for on_thread_unpark. we can't test this for on_thread_park because if the task parks itself inside Handle::block_on, there's nothing that can call unpark() for the task.

Copy link
Member

Choose a reason for hiding this comment

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

afaiu, we can only test this for on_thread_unpark. we can't test this for on_thread_park because if the task parks itself inside Handle::block_on, there's nothing that can call unpark() for the task.

Emm, what do you mean by "task parks itself"?

@ADD-SP ADD-SP added the R-loom-current-thread Run loom current-thread tests on this PR label Jun 29, 2025
@aryan9600 aryan9600 force-pushed the non-send-thread-park branch from 4f8b562 to fde0d15 Compare August 25, 2025 16:52
@github-actions github-actions bot removed the R-loom-current-thread Run loom current-thread tests on this PR label Aug 25, 2025
Add support for non `Send`+`Sync` closures for thread parking and unparking
callbacks when using a `LocalRuntime`. Since a `LocalRuntime` will always
run its tasks on the same thread, its safe to accept a non `Send`+`Sync`
closure.

Signed-off-by: Sanskar Jaiswal <[email protected]>
@aryan9600 aryan9600 force-pushed the non-send-thread-park branch from fde0d15 to 453dbd2 Compare August 25, 2025 17:26
@aryan9600 aryan9600 requested a review from ADD-SP August 25, 2025 17:28
@ADD-SP ADD-SP added the S-waiting-on-author Status: awaiting some action (such as code changes) from the PR or issue author. label Oct 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-tokio Area: The main tokio crate C-enhancement Category: A PR with an enhancement or bugfix. M-runtime Module: tokio/runtime S-waiting-on-author Status: awaiting some action (such as code changes) from the PR or issue author.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Non-Send version of callbacks such as on_thread_park

4 participants