Closed Bug 1905505 Opened 1 year ago Closed 1 year 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

(firefox-esr115 wontfix, firefox-esr128129+ fixed, firefox127 wontfix, firefox128 wontfix, firefox129 fixed)

VERIFIED FIXED
129 Branch
Tracking Status
firefox-esr115 --- wontfix
firefox-esr128 129+ fixed
firefox127 --- wontfix
firefox128 --- wontfix
firefox129 --- fixed

People

(Reporter: robwu, Assigned: robwu)

References

Details

(Whiteboard: [addons-jira])

Attachments

(4 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
Status: ASSIGNED → RESOLVED
Closed: 1 year ago
Resolution: --- → FIXED
Target Milestone: --- → 129 Branch

[Tracking Requested - why for this release]: This bug causes extensions to shut down unexpectedly in some cases. This bug can be triggered easily because the conditions needed to trigger this bug are not unusual.

FYI the fix in 129 has been verified externally by the original reporter at https://discourse.mozilla.org/t/one-of-my-manifest-v3-addons-is-not-waking-up-when-it-should/131349/41

Status: RESOLVED → VERIFIED

Go ahead and nominate this for ESR128 approval whenever you're comfortable doing so.

Flags: needinfo?(rob)

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.

Original Revision: https://phabricator.services.mozilla.com/D215297

Attachment #9413597 - Flags: approval-mozilla-esr128?

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.

Original Revision: https://phabricator.services.mozilla.com/D215298

Attachment #9413598 - Flags: approval-mozilla-esr128?

esr128 Uplift Approval Request

  • User impact if declined: This bug causes extensions to shut down unexpectedly in some cases. This bug can be triggered easily because the conditions needed to trigger this bug are not unusual.
  • Code covered by automated testing: yes
  • Fix verified in Nightly: yes
  • Needs manual QE test: no
  • Steps to reproduce for manual QE testing: Not needed, covered by automated unit tests.
  • Risk associated with taking this patch: Low
  • Explanation of risk level: Change specific to extensions, issue has been thorougly examined. The fix is targeted and minimizes unwanted side effects.
  • String changes made/needed: None
  • Is Android affected?: yes
Flags: needinfo?(rob)
Attachment #9413598 - Flags: approval-mozilla-esr128? → approval-mozilla-esr128+
Attachment #9413597 - Flags: approval-mozilla-esr128? → approval-mozilla-esr128+
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: