Bug 1856462 Comment 35 Edit History

Note: The actual edited comment in the bug view page will always show the original commenter’s name and original timestamp.

Okay, I believe I've tracked down the source of the problem, and I'm concerned enough that I'm increasing the severity to S2.

I don't think literally anything special needs to happen to cause this behavior. I believe it's just a race condition that anyone with a default configuration could hit. Potentially a large number of our users could be encountering this. Anecdotally, I have already found one person that I know personally that also seems to be experiencing this problem, though perhaps not as reliably.

I believe that this is what is going on:
 1. The Background Update Task starts ([here](https://searchfox.org/mozilla-central/rev/41edcdf7fe44678c5913a603a286b1fc3979d540/toolkit/mozapps/update/BackgroundTask_backgroundupdate.sys.mjs#59)) and instantiates an `AppUpdater` ([here](https://searchfox.org/mozilla-central/rev/41edcdf7fe44678c5913a603a286b1fc3979d540/toolkit/mozapps/update/BackgroundTask_backgroundupdate.sys.mjs#106)) and calls `AppUpdater.check()` ([here](https://searchfox.org/mozilla-central/rev/41edcdf7fe44678c5913a603a286b1fc3979d540/toolkit/mozapps/update/BackgroundTask_backgroundupdate.sys.mjs#168)).
 2. The `AppUpdater` determines that there is not currently an update check in-progress and [starts one](https://searchfox.org/mozilla-central/rev/41edcdf7fe44678c5913a603a286b1fc3979d540/toolkit/mozapps/update/AppUpdater.sys.mjs#302), which finds an available update and calls [`AppUpdater.#downloadUpdate`](https://searchfox.org/mozilla-central/rev/41edcdf7fe44678c5913a603a286b1fc3979d540/toolkit/mozapps/update/AppUpdater.sys.mjs#383).
 3. This immediately calls `this.#setStatus(AppUpdater.STATUS.DOWNLOADING)` ([here](https://searchfox.org/mozilla-central/rev/41edcdf7fe44678c5913a603a286b1fc3979d540/toolkit/mozapps/update/AppUpdater.sys.mjs#434)) before it [calls into the Application Update Service to download the update](https://searchfox.org/mozilla-central/rev/41edcdf7fe44678c5913a603a286b1fc3979d540/toolkit/mozapps/update/AppUpdater.sys.mjs#436).
 4. Even before the Application Update Service can download the update, the `#setStatus` call [notifies listeners](https://searchfox.org/mozilla-central/rev/41edcdf7fe44678c5913a603a286b1fc3979d540/toolkit/mozapps/update/AppUpdater.sys.mjs#705) of the status change.
 5. The Background Update Task's listener enters [this block](https://searchfox.org/mozilla-central/rev/41edcdf7fe44678c5913a603a286b1fc3979d540/toolkit/mozapps/update/BackgroundTask_backgroundupdate.sys.mjs#130-160), which results in `resolve()` being called, `_attemptBackgroundUpdate` returning, and `runBackgroundTask` returning shortly after that. Naturally, after that, the background task [exits](https://searchfox.org/mozilla-central/rev/41edcdf7fe44678c5913a603a286b1fc3979d540/toolkit/components/backgroundtasks/BackgroundTasksManager.sys.mjs#267).

`aus.downloadUpdate()` thus races against shutdown. I think that ostensibly [this](https://searchfox.org/mozilla-central/rev/41edcdf7fe44678c5913a603a286b1fc3979d540/toolkit/mozapps/update/BackgroundTask_backgroundupdate.sys.mjs#430-432) is supposed to address this race, but it seems pretty clear from the evidence that it isn't working reliably.
Okay, I believe I've tracked down the source of the problem, and I'm concerned enough that I'm increasing the severity to S2.

I don't think literally anything special needs to happen to cause this behavior. I believe it's just a race condition that anyone with a default configuration could hit. Potentially a large number of our users could be encountering this. Anecdotally, I have already found one person that I know personally that also seems to be experiencing this problem, though perhaps not as reliably.

I believe that this is what is going on:
 1. The Background Update Task starts ([here](https://searchfox.org/mozilla-central/rev/41edcdf7fe44678c5913a603a286b1fc3979d540/toolkit/mozapps/update/BackgroundTask_backgroundupdate.sys.mjs#59)) and instantiates an `AppUpdater` ([here](https://searchfox.org/mozilla-central/rev/41edcdf7fe44678c5913a603a286b1fc3979d540/toolkit/mozapps/update/BackgroundTask_backgroundupdate.sys.mjs#106)) and calls `AppUpdater.check()` ([here](https://searchfox.org/mozilla-central/rev/41edcdf7fe44678c5913a603a286b1fc3979d540/toolkit/mozapps/update/BackgroundTask_backgroundupdate.sys.mjs#168)).
 2. The `AppUpdater` determines that there is not currently an update check in-progress and [starts one](https://searchfox.org/mozilla-central/rev/41edcdf7fe44678c5913a603a286b1fc3979d540/toolkit/mozapps/update/AppUpdater.sys.mjs#302), which finds an available update and calls [`AppUpdater.#downloadUpdate`](https://searchfox.org/mozilla-central/rev/41edcdf7fe44678c5913a603a286b1fc3979d540/toolkit/mozapps/update/AppUpdater.sys.mjs#383).
 3. This immediately calls `this.#setStatus(AppUpdater.STATUS.DOWNLOADING)` ([here](https://searchfox.org/mozilla-central/rev/41edcdf7fe44678c5913a603a286b1fc3979d540/toolkit/mozapps/update/AppUpdater.sys.mjs#434)) _before_ it [calls into the Application Update Service to download the update](https://searchfox.org/mozilla-central/rev/41edcdf7fe44678c5913a603a286b1fc3979d540/toolkit/mozapps/update/AppUpdater.sys.mjs#436).
 4. Even before the Application Update Service can download the update, the `#setStatus` call [notifies listeners](https://searchfox.org/mozilla-central/rev/41edcdf7fe44678c5913a603a286b1fc3979d540/toolkit/mozapps/update/AppUpdater.sys.mjs#705) of the status change.
 5. The Background Update Task's listener enters [this block](https://searchfox.org/mozilla-central/rev/41edcdf7fe44678c5913a603a286b1fc3979d540/toolkit/mozapps/update/BackgroundTask_backgroundupdate.sys.mjs#130-160), which results in `resolve()` being called, `_attemptBackgroundUpdate` returning, and `runBackgroundTask` returning shortly after that. Naturally, after that, the background task [exits](https://searchfox.org/mozilla-central/rev/41edcdf7fe44678c5913a603a286b1fc3979d540/toolkit/components/backgroundtasks/BackgroundTasksManager.sys.mjs#267).

`aus.downloadUpdate()` thus races against shutdown. I think that ostensibly [this](https://searchfox.org/mozilla-central/rev/41edcdf7fe44678c5913a603a286b1fc3979d540/toolkit/mozapps/update/BackgroundTask_backgroundupdate.sys.mjs#430-432) is supposed to address this race, but it seems pretty clear from the evidence that it isn't working reliably.

Back to Bug 1856462 Comment 35