Open Bug 1905505 Opened 2 days ago Updated 7 hours ago

Event page terminates immediately at event page wakeup when an extension event is triggered while the event page is suspended

Categories

(WebExtensions :: General, defect, P1)

defect

Tracking

(Not tracked)

ASSIGNED

People

(Reporter: robwu, Assigned: robwu)

References

Details

(Whiteboard: [addons-jira])

Attachments

(2 files)

The event page lifecycle is managed by a timer (IdleManager) that terminates an event page as soon as there is no detected activity.

The timer starts when the event page wakes up (resetTimer()), and is guaranteed to stop when the background stops for any reason (regular termination, process crash, extension unload) (by calling clearState()).

Whenever any activity occurs, signaled through the internal "background-script-reset-idle" event, resetTimer() is called again, which extends the lifetime by rescheduling the timer. This design assumes that background-script-reset-idle is only called when the background script exists. If the background is already stopped, calling resetTimer() will just schedule a timer that eventually calls terminateBackground().

The first step of terminateBackground() is to await bgStartupPromise, which means that if anything causes terminateBackground() to be called while the background is inactive, that it will immediately terminate the background script at the next startup of the event page.

I audited all entries that trigger "background-script-reset-idle":

This is not a theoretical problem, it has repeatedly been observed in the wild, e.g. at https://discourse.mozilla.org/t/one-of-my-manifest-v3-addons-is-not-waking-up-when-it-should/131349/36

This patch adds a bunch of checks + early returns to make sure that the
idle timer is NOT started when the background context is in the STOPPED
state. Encountering these errors is an implementation flaw, and the
errors should never been seen in practice.

The logs added in this commit should never be seen in the wild.

This patch updates every use of EventManager in child/ext-*.js files
that can referenced from extension contexts of envType "addon_child".

Pre patch, these could trigger "background-script-reset-idle" and
result in triggering "background-script-reset-idle" in the parent
despite the background being stopped, and consequently trigger the
reported bug. The previous patch fixes the negative consequence of this
unexpected event, this patch fixes an underlying cause by removing the
triggers.

Forcing resetIdleOnEvent to false does not have any negative impact on
these modules, because all of these events are dependencies of an
EventManager in the parent, which already activates the functionality
associated with resetIdleOnEvent=true.

To minimize the likelihood of affecting unit tests, this patch keeps the
EventManager of test.onMessage at resetIdleOnEvent=true as before, and
defers to bug 1901294 for changing it to false.

See Also: → 1447806
Severity: -- → S2
Priority: -- → P1
Pushed by rob@robwu.nl:
https://hg.mozilla.org/integration/autoland/rev/7fedd86509e3
Avoid scheduling background shutdown when it is already stopped r=rpl
https://hg.mozilla.org/integration/autoland/rev/bec11d4cf346
Remove unnecessary resetIdleOnEvent from child modules r=rpl
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: