DeferredTask's finalize method may wait unnecessarily long due to idle dispatch
Categories
(Toolkit :: Async Tooling, defect, P1)
Tracking
()
People
(Reporter: robwu, Assigned: robwu)
References
(Blocks 1 open bug, Regression)
Details
(Keywords: perf, regression)
Attachments
(2 files)
While developing and running a new test locally, I consistently observed that it took a noticeable amount of time before the test shut down. I managed to measure the immediate reason for this. Apparently, DeferredTask waits for at least 5-8 seconds during a call to finalize (measured and pinpointed to the delay between scheduling and running PromiseUtils.idleDispatch
in DeferredTask.jsm) when the test has finished.
The culprit for my observation was that the XPIDatabase.jsm
uses an armed DeferredTask
, and XPIDatabase.shutdown
awaits the finalization of that task. This in its turn blocks XPIProvider.shutdown, which blocks shutdown via AddonManager.jsm
, aka AsyncShutdown + "AddonManager: Waiting for providers to shut down".
I have observed a delay of a few seconds, but in theory this could be even longer. Anything that blocks shutdown should not sit idle, but run ASAP when shutdown commences.
There are multiple occurrences of DeferredTask + arm + finalize that block AsyncShutdown, and this bug might have contributed to part of those known AsyncShutdown timeout crashes:
- bug 1542045 - use of
finalize
in AsyncShutdown "Flush WebExtension StartupCache" - bug 1315306 - use of
finalize
in shutdown , and some crash reports that match the condition. - bug 1453520 - use of
finalize
(anything that uses JSONFile may be affected) - bug 1504628 - the example above that lead to this bug report
Assignee | ||
Comment 1•5 years ago
|
||
The current logic was introduced in bug 1391707.
There was clearly an intent to minimize the impact on finalize
, according to the comment at https://searchfox.org/mozilla-central/rev/d7537a9cd2974efa45141d974e5fc7c727f1a78f/toolkit/modules/DeferredTask.jsm#312-313
// If we're being finalized, execute the task immediately, so we don't
// risk blocking async shutdown longer than necessary.
However, the logic did not account for potentially long delays from previously scheduled tasks that block shutdown at https://searchfox.org/mozilla-central/rev/d7537a9cd2974efa45141d974e5fc7c727f1a78f/toolkit/modules/DeferredTask.jsm#258-261
I'm going to attach a patch that addresses this issue.
Assignee | ||
Comment 2•5 years ago
|
||
DeferredTask's finalize() awaits the completion of any (previously)
scheduled tasks before returning. To minimize the time spent in
finalize(), the idle dispatch timer should not be a part of the task
execution, but part of the (cancelable) timer.
Assignee | ||
Comment 3•5 years ago
|
||
Task.jsm and function* tasks do not exist any more.
Depends on D49936
Updated•5 years ago
|
Comment 5•5 years ago
|
||
bugherder |
https://hg.mozilla.org/mozilla-central/rev/df2ae0b114e0
https://hg.mozilla.org/mozilla-central/rev/ceca727602a1
Comment 6•5 years ago
|
||
I'm a bit on the fence about whether we should consider uplifting this to Beta or not. Is there any particular signature we can look at to determine if this helped with the AsyncShutdown crashes?
Assignee | ||
Comment 7•5 years ago
|
||
I looked up signatures of callers of DeferredTask
that uses finalize
in a blocker. The links are below. If anything, this bug is only a part of the cause, by adding just enough time to other resource-consuming tasks to exceed the AsyncShutdown deadline. Some of the crashes with higher frequency involve I/O, and it is much more likely for the crash to have been caused by slow I/O than the delay from this bug (usually, idle tasks are run within a few dozen milliseconds, but I somehow managed to consistently observe a delay of several seconds while running a relatively simple xpcshell test on a powerful Linux system).
- "DownloadAutoSaveView: writing data"
- "Flush WebExtension StartupCache"
- "TelemetryController: shutting down" (filtered by relevant condition: results)
- "JSON store: writing data"
- "Flush AddonRepository"
- "XPIProvider shutdown"
- "UpdateManager: writing update xml data"
While it is possible for some of the crashes to go away with this fix, it is probably not enough to warrant an uplift.
Comment 8•5 years ago
|
||
Thanks for the info!
Updated•3 years ago
|
Description
•