LibWeb: Remove tasks for destroyed documents instead of running them

Previously, destroyed-document tasks were forced to be runnable to
prevent them from leaking in the task queue. Instead, discard them
during task selection so their callbacks never run with stale state.

This used to cause issues with a couple of `spin_until()`s in the past,
but since we've removed some of them that had to do with the document
lifecycle, let's see if we can stick closer to the spec now.
This commit is contained in:
Jelle Raaijmakers
2026-03-19 20:21:11 +01:00
committed by Andreas Kling
parent 71ba774083
commit c8baa6e179
Notes: github-actions[bot] 2026-03-19 20:25:47 +00:00
6 changed files with 19 additions and 36 deletions

View File

@@ -126,13 +126,10 @@ EventLoop& EnvironmentSettingsObject::responsible_event_loop()
// https://whatpr.org/html/9893/webappapis.html#check-if-we-can-run-script
RunScriptDecision can_run_script(JS::Realm const& realm)
{
// 1. If the global object specified by realm is a Window object whose Document object is not fully active, then return "do not run".
if (auto const* window = as_if<HTML::Window>(realm.global_object())) {
auto const& document = window->associated_document();
// AD-HOC: We allow tasks for destroyed documents to run so that microtasks queued during the fetch of a new
// document in a navigation can still be processed, even after the previous document, the one that
// initiated the fetch, has been destroyed.
if (!document.has_been_destroyed() && !document.is_fully_active())
// 1. If the global object specified by realm is a Window object whose Document object is not fully active, then
// return "do not run".
if (auto const* window = as_if<Window>(realm.global_object())) {
if (!window->associated_document().is_fully_active())
return RunScriptDecision::DoNotRun;
}