How to wait until idle for TaskRunner?

128 views
Skip to first unread message

Clifford Cheng

unread,
Dec 16, 2021, 2:24:33 PM12/16/21
to Chromium-dev
Hi there,

I am trying to put in a wait until idle in a test for a TaskRunner() (not SingleThreadTaskRunner()) before proceeding.
Here is what I have done.
TaskRunner()->PostTask(
  FROM_HERE,
  base::BindLambdaForTesting([&] () { base::RunLoop().RunUntilIdle(); }));

However, the check in thread_task_runner_handle.cc always failed.
I am wondering if there is any way for a TaskRunner() to wait until idle?
 
Thanks,
Clifford

Joe Mason

unread,
Dec 16, 2021, 3:12:58 PM12/16/21
to cliffo...@chromium.org, Chromium-dev
RunUntilIdle is dangerous in tests because its behaviour depends on exactly which tasks are posted during the test. See the warning comments at https://source.chromium.org/chromium/chromium/src/+/main:base/run_loop.h;l=90;drc=9066bfa0526ed454eb71d261c48b48535194de4f for more details.

Can you give more details about what you're trying to do? I can give you some better advice once I understand the situation better:

* What type of TaskRunner is TaskRunner()? (Mainly, is it a SequencedTaskRunner or something that can run tasks in parallel?)
* What tasks do you expect to be posted to TaskRunner() before the PostTask line you gave?
* What thread do you need to block until the TaskRunner is idle, and why?

Thanks,
Joe

--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
---
You received this message because you are subscribed to the Google Groups "Chromium-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-dev/a5c45dc2-fb9a-4362-b3fd-74ca12da2d3en%40chromium.org.

Clifford Cheng

unread,
Dec 16, 2021, 3:53:12 PM12/16/21
to Chromium-dev, joenot...@google.com, Chromium-dev, Clifford Cheng
Thanks Joe.

This is the TaskRunner that I am trying to wait for idle.
The reason I need to do this is because in some web app browser tests, the shortcuts are deleted even before their creation.
This results in a crash / an error and also causes flakiness. You can refer to this bug for more details.

A possible fix (and this what I am trying to do) is to wait until that TaskRunner has become idle (so shortcuts are created) before proceeding to the next step in the test.
But it doesn't seem to have an easy way for this TaskRunner to wait until idle.

To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev+unsubscribe@chromium.org.

Daniel Cheng

unread,
Dec 16, 2021, 5:42:17 PM12/16/21
to cliffo...@chromium.org, Chromium-dev, joenot...@google.com
If the shortcuts are created by a task posted to that TaskRunner, how about just doing something like:

base::RunLoop run_loop;
shortcut_task_runner->PostTask(base::BindLambdaForTesting([&] { run_loop.Quit(); });
run_loop.Run();

That ensures that everything that's already been posted will run by the time that Run() returns.

Daniel

To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev...@chromium.org.

--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
---
You received this message because you are subscribed to the Google Groups "Chromium-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-dev/3a540c1b-22d4-4b51-b4e8-10b157f3c7d6n%40chromium.org.

Clifford Cheng

unread,
Dec 16, 2021, 7:00:57 PM12/16/21
to Chromium-dev, Daniel Cheng, Chromium-dev, Joe Mason, Clifford Cheng
I think that may work. Let me try it out and see.
Thanks!

To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev+unsubscribe@chromium.org.

--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
---
You received this message because you are subscribed to the Google Groups "Chromium-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev+unsubscribe@chromium.org.

Gabriel Charette

unread,
Dec 17, 2021, 10:48:32 AM12/17/21
to dch...@chromium.org, cliffo...@chromium.org, Chromium-dev, joenot...@google.com
On Thu, Dec 16, 2021 at 5:41 PM Daniel Cheng <dch...@chromium.org> wrote:
If the shortcuts are created by a task posted to that TaskRunner, how about just doing something like:

base::RunLoop run_loop;
shortcut_task_runner->PostTask(base::BindLambdaForTesting([&] { run_loop.Quit(); });
run_loop.Run();

Short-form:

base::RunLoop run_loop;
shortcut_task_runner->PostTask(run_loop.QuitClosure());
run_loop.Run();
 

Reply all
Reply to author
Forward
0 new messages