forked from mirrors/gecko-dev
Purge HTTP disk cache using backgroundtasks
Motivation:
In the History settings preferences panel you may choose to
`Clear History when Firefox closes` - which sets the
`privacy.sanitize.sanitizeOnShutdown` pref.
If the `Cache` checkbox is also checked it sets the
`privacy.clearOnShutdown.cache` pref to true.
When both of these prefs are true `CacheObserver::ClearCacheOnShutdown()`
will return true, which will cause Firefox to clear the HTTP disk cache
during shutdown. This will block shutdown for howeverlong this takes. If the
user has a slow disk, or the user is on Windows and something is blocking the
deletion of those files, this will trigger a shutdown hang making Firefox
crash. This leads to a bad user experience as trying to start Firefox again
during this time will show the "Firefox is already running message".
Eventually a crash dump is produced. Bug 1356853 and bug 1682899 are caused
by this specific issue.
In order to avoid these crashes and a bad user experience we have
a few options:
1. Completely disable the disk cache when a user checks this box.
This option will degrade the user's browsing experience.
2. Don't delete the folder at shutdown
Whether we do this by removing the checkbox or simply not respecting
the pref value, users who already have this setting would be surprised
if the cache folder stops being deleted.
3. Use a thread pool to delete the files
While it's likely to speed up the deletion at least a little bit,
this would introduce additional complexity while not completely
fixing the issue. A slow disk will still block shutdown.
4. Delete the cache at shutdown using a separate process
This is likely the best option. It has the advantage of not blocking
shutdown while at the same time maintaining similar properties to the
existing functionality:
- The cache folder is deleted at shutdown.
- Has the same behaviour if Firefox gets killed or crashes for different issues.
- Behaves similarly if the OS is forcefully shutdown during or before we begin to purge.
- Additionaly, because we rename the folder prior to dispatching the background task, if the purging isn't completed we
- don't rebuild the cache from an incomplete folder on next restart
- are able to resume the purging after Firefox startup
A particularly special case is the Windows shutdown.
If the user shuts down windows that will try to close Firefox. If the shutdown
takes too long, the user will see the "Close anyway" button and may click
it thus preventing the cache purge to complete.
When using a background task we have a similar situation, but the button won't
even appear. So after the next Firefox restart we will check if the cache
needs to be purged.
Largely, the new behaviour will be:
- At shutdown we conditionally dispatch a background task to delete the folder
- If creating the process fails (for some reason) we fallback to the old way
of synchronously deleting the folder.
- The task will then try to delete the folder in the background
- If for some reason it fails, we will dispatch a new background task shortly
after Firefox restarts to clean up the old folders.
Differential Revision: https://phabricator.services.mozilla.com/D126339
36 lines
1 KiB
JavaScript
36 lines
1 KiB
JavaScript
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
"use strict";
|
|
|
|
add_task(async function test_startupCleanup() {
|
|
Services.prefs.setBoolPref(
|
|
"network.cache.shutdown_purge_in_background_task",
|
|
true
|
|
);
|
|
let dir = Services.dirsvc.get("ProfD", Ci.nsIFile);
|
|
dir.append("cache2.2021-11-25-08-47-04.purge.bg_rm");
|
|
Assert.equal(dir.exists(), false, `Folder ${dir.path} should not exist`);
|
|
dir.create(Ci.nsIFile.DIRECTORY_TYPE, 0o744);
|
|
Assert.equal(
|
|
dir.exists(),
|
|
true,
|
|
`Folder ${dir.path} should have been created`
|
|
);
|
|
|
|
Services.obs.notifyObservers(null, "browser-delayed-startup-finished");
|
|
|
|
await TestUtils.waitForCondition(() => {
|
|
return !dir.exists();
|
|
});
|
|
|
|
Assert.equal(
|
|
dir.exists(),
|
|
false,
|
|
`Folder ${dir.path} should have been purged by background task`
|
|
);
|
|
Services.prefs.clearUserPref(
|
|
"network.cache.shutdown_purge_in_background_task"
|
|
);
|
|
});
|