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

fix(madsim): drop the future within the scope of TaskEnterGuard when a task is aborted #231

Merged
merged 5 commits into from
Oct 12, 2024

Conversation

BugenZhao
Copy link
Contributor

Signed-off-by: Bugen Zhao [email protected]

Signed-off-by: Bugen Zhao <[email protected]>
Signed-off-by: Bugen Zhao <[email protected]>
Signed-off-by: Bugen Zhao <[email protected]>
@BugenZhao BugenZhao changed the title drop the future within the scope of TaskEnterGuard when a task is aborted fix(madsim): drop the future within the scope of TaskEnterGuard when a task is aborted Oct 10, 2024
// cancelled task or killed node: drop the future
continue;
drop
Copy link
Contributor

@kwannoel kwannoel Oct 10, 2024

Choose a reason for hiding this comment

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

Ohhhh I see. The continue triggered us to directly proceed to the end of the loop, and call the destructor of runnable implicitly. Then it calls the drop for the struct, which spawned the thread.

Seems acceptable to me to drop it because task was cancelled / node was killed.

Copy link
Contributor

@kwannoel kwannoel left a comment

Choose a reason for hiding this comment

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

LGTM once pass CI. @wangrunji0408 could you take a look too?

@kwannoel
Copy link
Contributor

I considered opposite cases like:

    #[test]
    fn spawn_futures() {
        let runtime = madsim::runtime::Runtime::new();
        runtime.block_on(async move {
            let rt = Runtime::new().unwrap();
            let join_handle = rt.spawn(async move {
                Handle.spawn(async move {
                    madsim::time::sleep(Duration::from_secs(1)).await;
                    println!("hi");
                });
                Handle.spawn(async move {
                    madsim::time::sleep(Duration::from_secs(1)).await;
                    println!("hi2")
                });
            });
            madsim::time::sleep(Duration::from_secs(10)).await;
            join_handle.await.unwrap();
        });
        panic!("break");
    }

In such cases, we will always have task context, since their parent future will be executing with task context, and the spawning steps happen sequentially. The newly spawned futures will be handled by Spawner, which will schedule these tasks using its sender, and let the main event loop inside the executor execute these.

So I think we just need to handle the case of node kill / task abort as was done in this PR.

@kwannoel
Copy link
Contributor

Many thanks @BugenZhao!

Signed-off-by: Bugen Zhao <[email protected]>
@@ -623,7 +630,7 @@ impl Spawner {
F::Output: 'static,
{
if self.info.killed.load(Ordering::Relaxed) {
panic!("spawning task on a killed node");
tracing::warn!("spawning task on a killed node, the task will never run");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also relax this check so that spawn during drop by killing a node won't panic.

Signed-off-by: Bugen Zhao <[email protected]>
@BugenZhao BugenZhao marked this pull request as ready for review October 10, 2024 08:44
@kwannoel
Copy link
Contributor

kwannoel commented Oct 10, 2024

Let's also bump the version of madsim to v0.2.31 before merging.

@BugenZhao
Copy link
Contributor Author

Given that this PR has been tested in risingwavelabs/risingwave#18852 and RisingWave might be the largest project to adopt madsim, I believe we can safely merge this PR now.

@BugenZhao BugenZhao merged commit 7efb111 into main Oct 12, 2024
24 checks passed
@BugenZhao BugenZhao deleted the bz/enter-while-drop branch October 12, 2024 03:28
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.

2 participants