Skip to content

Fix new scheduler loop #747

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

Merged
merged 1 commit into from
Apr 9, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/rt/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ impl Machine {
continue;
}

let mut sched = rt.sched.lock().unwrap();

// One final check for available tasks while the scheduler is locked.
if let Some(task) = iter::repeat_with(|| self.find_task(rt))
.find(|s| !s.is_retry())
Expand All @@ -258,19 +260,19 @@ impl Machine {
continue;
}

let mut sched = rt.sched.lock().unwrap();

// If another thread is already blocked on the reactor, there is no point in keeping
// the current thread around since there is too little work to do.
if sched.polling {
thread::sleep(Duration::from_micros(10));
continue;
break;
}
Comment on lines +263 to 267
Copy link

@win-t win-t Apr 9, 2020

Choose a reason for hiding this comment

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

I'm not sure, terminating the thread is a good idea, what will happen when the load is increasing? no new thread will be created

simple sleep like this will end up with single thread with no way to recover the worker thread

        task::spawn(async {
            loop {
                task::sleep(Duration::from_secs(1)).await;
                io::stdout().write_all(b"Hello World\n").await.unwrap();
            }
        })
        .await;

Copy link

@win-t win-t Apr 9, 2020

Choose a reason for hiding this comment

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

I want to bring back auto spawning thread here #740 (PR: #744) , but it's just as a fallback if the users forget to use spawn_blocking (or especially when they don't own full control of the code, like using a library)

Copy link
Member Author

Choose a reason for hiding this comment

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

We can not go back to the sleep & continue, it regresses performance even further, so we need to find a better solution.

Copy link
Member Author

Choose a reason for hiding this comment

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

The correct fix as far as I understand is to bring back the dynamic restarting of machines: #748


// Unlock the schedule poll the reactor until new I/O events arrive.
sched.polling = true;
drop(sched);

rt.reactor.poll(None).unwrap();

let mut sched = rt.sched.lock().unwrap();
// Lock the scheduler again and re-register the machine.
sched = rt.sched.lock().unwrap();
sched.polling = false;

runs = 0;
Expand Down