Skip to content

Truly independent web browser

LibWeb: Add a way to shut down the agent of a thread

Andreas Kling62683e7b27e0
diff --git a/Libraries/LibWeb/Bindings/MainThreadVM.cpp b/Libraries/LibWeb/Bindings/MainThreadVM.cpp
index 29c3d1e4fe3..3a03319adcb 100644
--- a/Libraries/LibWeb/Bindings/MainThreadVM.cpp
+++ b/Libraries/LibWeb/Bindings/MainThreadVM.cpp
@@ -10,10 +10,12 @@
#include <AK/NeverDestroyed.h>
#include <LibCore/AnonymousBuffer.h>
+#include <LibCore/ThreadEventQueue.h>
#include <LibGC/CellAllocator.h>
#include <LibGC/DeferGC.h>
#include <LibGC/Heap.h>
#include <LibGfx/SystemTheme.h>
+#include <LibJS/HostClassBuilder.h>
#include <LibJS/Module.h>
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/Array.h>
@@ -40,13 +42,16 @@
#include <LibWeb/ContentSecurityPolicy/BlockingAlgorithms.h>
#include <LibWeb/ContentSecurityPolicy/Directives/KeywordSources.h>
#include <LibWeb/ContentSecurityPolicy/Directives/Names.h>
+#include <LibWeb/Crypto/CryptoKey.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Element.h>
+#include <LibWeb/Fetch/Fetching/BodyStreamPullSource.h>
#include <LibWeb/HTML/CustomElements/CustomElementDefinition.h>
#include <LibWeb/HTML/EventNames.h>
#include <LibWeb/HTML/HTMLFormElement.h>
#include <LibWeb/HTML/HTMLSlotElement.h>
#include <LibWeb/HTML/Location.h>
+#include <LibWeb/HTML/MessageEvent.h>
#include <LibWeb/HTML/PromiseRejectionEvent.h>
#include <LibWeb/HTML/Scripting/ClassicScript.h>
#include <LibWeb/HTML/Scripting/Environments.h>
@@ -62,8 +67,13 @@
#include <LibWeb/HTML/WindowProxy.h>
#include <LibWeb/HTML/WorkletGlobalScope.h>
#include <LibWeb/HighResolutionTime/TimeOrigin.h>
+#include <LibWeb/Loader/ResourceLoader.h>
#include <LibWeb/Page/Page.h>
+#include <LibWeb/PerformanceTimeline/PerformanceObserver.h>
+#include <LibWeb/PermissionsAPI/PermissionStore.h>
#include <LibWeb/Platform/EventLoopPlugin.h>
+#include <LibWeb/Platform/ThreadPoolWork.h>
+#include <LibWeb/ServiceWorker/ServiceWorkerContainer.h>
#include <LibWeb/TrustedTypes/TrustedScript.h>
#include <LibWeb/WebAssembly/WebAssembly.h>
#include <LibWeb/WebIDL/AbstractOperations.h>
@@ -729,6 +739,44 @@ void initialize_main_thread_vm(HTML::AgentType type)
};
}
+extern "C" void web_rust_shut_down_agent(JSVM*);
+
+void shut_down_agent()
+{
+ auto& vm = main_thread_vm();
+
+ // What the agent keeps outside its heap refers to cells in it, so that goes while the heap is alive.
+ Platform::discard_thread_pool_completions();
+ Fetch::Fetching::disarm_all_body_stream_wakes();
+ HTML::clear_message_event_ports_caches();
+ PerformanceTimeline::clear_supported_entry_types_caches();
+ Crypto::clear_crypto_key_caches();
+ ServiceWorker::clear_service_worker_container_ready_promise_caches();
+ PermissionsAPI::PermissionStore::the().forget_statuses();
+
+ // An event that is still queued can hold roots as well, and a stopped request only lets go of its roots in one. As
+ // the task queues discard everything by now, no script runs from these. What a deferred invocation of LibWeb is for
+ // has to go undone, as the worker that it was for has run its closing steps.
+ Platform::EventLoopPlugin::set_discard_deferred_invocations_on_this_thread(true);
+ auto& event_queue = Core::ThreadEventQueue::current();
+ for (;;) {
+ if (ResourceLoader::is_initialized())
+ ResourceLoader::the().stop_all_requests();
+ if (!event_queue.has_pending_events())
+ break;
+ event_queue.process();
+ }
+ Platform::EventLoopPlugin::set_discard_deferred_invocations_on_this_thread(false);
+
+ web_rust_shut_down_agent(JS::HostABI::vm_to_abi(vm));
+ ResourceLoader::shut_down();
+
+ // NB: The VM stays reachable while it is destroyed, as the finalizers of the cells in its heap use it.
+ vm.unref();
+ vm.unref();
+ (void)main_thread_vm_ptr().leak_ref();
+}
+
JS::VM& main_thread_vm()
{
VERIFY(main_thread_vm_ptr());
diff --git a/Libraries/LibWeb/Bindings/MainThreadVM.h b/Libraries/LibWeb/Bindings/MainThreadVM.h
index ae096b9e027..46adc29ef69 100644
--- a/Libraries/LibWeb/Bindings/MainThreadVM.h
+++ b/Libraries/LibWeb/Bindings/MainThreadVM.h
@@ -45,6 +45,11 @@ HTML::Script* active_script();
WEB_API void initialize_main_thread_vm(HTML::AgentType);
WEB_API JS::VM& main_thread_vm();
+// Ends the agent of the current thread: releases what it keeps outside its heap, runs the events queued on the thread,
+// and destroys its VM and heap. The caller has run its closing steps and given up its roots. None of this ends the
+// thread or the process, and the agent's thread has to be the one that calls it.
+WEB_API void shut_down_agent();
+
WEB_API NonnullOwnPtr<JS::ExecutionContext> create_a_new_javascript_realm(JS::VM&, HTML::CrossOriginIsolationMode agent_cluster_cross_origin_isolation_mode, Function<GC::Ref<JS::Object>(JS::Realm&)> create_global_object, Function<GC::Ref<JS::Object>(JS::Realm&)> create_global_this_value);
// Creates a bare Window-backed realm for tests and tools that do not need a full Document.
diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/broadcast_channel.rs b/Libraries/LibWeb/Bindings/Rust/src/html/broadcast_channel.rs
index a6c25eac53f..d15262f430b 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/html/broadcast_channel.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/html/broadcast_channel.rs
@@ -142,6 +142,14 @@ thread_local! {
};
}
+/// Forgets the channels of the thread's agent, whose references into its heap have to go before the heap does.
+pub(crate) fn forget_broadcast_channels_of_agent() {
+ BROADCAST_CHANNELS.with_borrow_mut(|channels| {
+ channels.channels.clear();
+ channels.live_after_pruning = 0;
+ });
+}
+
fn relevant_settings_object_origin(realm: Gc<Realm>) -> Origin {
unsafe extern "C" fn on_origin(ctx: *mut c_void, ffi_origin: *const FfiOrigin) {
// SAFETY: ctx is the origin below, and LibWeb lends the origin for the call.
diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/exports.rs b/Libraries/LibWeb/Bindings/Rust/src/html/exports.rs
index 766aea367a8..6483d06b148 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/html/exports.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/html/exports.rs
@@ -415,6 +415,21 @@ pub unsafe extern "C" fn web_rust_message_port_disentangle_ports_of_realm(vm: *m
MessagePort::disentangle_ports_of_realm(vm, realm);
}
+/// Releases what the thread's agent keeps outside its heap, for an agent that goes away.
+///
+/// # Safety
+///
+/// `vm` must be the live VM of the thread, whose heap has to stay alive through the call.
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn web_rust_shut_down_agent(vm: *mut c_void) {
+ // SAFETY: The caller passes the live VM.
+ let vm = unsafe { &*vm.cast::<Vm>() };
+ MessagePort::shut_down_ports_of_agent(vm);
+ crate::html::broadcast_channel::forget_broadcast_channels_of_agent();
+ crate::web_assembly::Agent::shut_down();
+ crate::indexed_db::internal::storage::shut_down_storage();
+}
+
/// Steps 6 to 9 of BroadcastChannel's postMessage(), for a message that another process posted, whose channel name
/// C++ hands over.
///
diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/message_port.rs b/Libraries/LibWeb/Bindings/Rust/src/html/message_port.rs
index 1b460caf9e5..dec2a301403 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/html/message_port.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/html/message_port.rs
@@ -226,6 +226,21 @@ impl MessagePort {
}
}
+ /// Disentangles every port of the thread's agent and forgets them, for an agent that goes away. A port roots itself
+ /// while it can receive messages, and that root has to go before the heap does.
+ pub(crate) fn shut_down_ports_of_agent(vm: &Vm) {
+ let ports = MarkedVec::new(vm);
+ MESSAGE_PORTS.with_borrow_mut(|message_ports| {
+ for port in message_ports.ports.drain(..).filter_map(|port| port.get()) {
+ ports.push(port);
+ }
+ message_ports.live_after_pruning = 0;
+ });
+ for port in ports.values().iter() {
+ port.disentangle();
+ }
+ }
+
// https://html.spec.whatwg.org/multipage/web-messaging.html#ports-and-garbage-collection
// When a MessagePort object o is entangled, user agents must either act as if o's entangled MessagePort object has a
// strong reference to o, or as if o's relevant global object has a strong reference to o.
diff --git a/Libraries/LibWeb/Bindings/Rust/src/indexed_db/internal/storage.rs b/Libraries/LibWeb/Bindings/Rust/src/indexed_db/internal/storage.rs
index 0a30474bcbf..92d5b0b3cc9 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/indexed_db/internal/storage.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/indexed_db/internal/storage.rs
@@ -21,6 +21,7 @@ use crate::storage_api::storage_key::StorageKey;
unsafe extern "C" {
fn gc_root_create(cell: *mut c_void) -> *mut c_void;
+ fn gc_root_destroy(root: *mut c_void);
}
// https://w3c.github.io/IndexedDB/#connection-queues
@@ -78,13 +79,21 @@ pub(crate) fn storage(vm: &Vm) -> StorageRef {
databases: GcRefCell::new(Vec::new()),
connection_queues: GcRefCell::new(Vec::new()),
});
- // NB: The storage lives as long as the thread, which roots its cell.
+ // NB: The storage lives as long as the agent of the thread, which roots its cell.
// SAFETY: The cell is a live cell of the heap.
let root = NonNull::new(unsafe { gc_root_create(storage.as_ptr().cast()) }).expect("a root is allocated");
STORAGE.set(Some((storage, root)));
storage
}
+/// Releases the storage of the current thread's agent, which goes away with its heap.
+pub(crate) fn shut_down_storage() {
+ if let Some((_, root)) = STORAGE.take() {
+ // SAFETY: The root is the one storage() created, and the heap it is in is alive.
+ unsafe { gc_root_destroy(root.as_ptr()) };
+ }
+}
+
impl Storage {
pub(crate) fn for_each_database(&self, mut visitor: impl FnMut(DatabaseRef)) {
for databases_of_key in self.databases.borrow().iter() {
diff --git a/Libraries/LibWeb/Bindings/Rust/src/web_assembly/mod.rs b/Libraries/LibWeb/Bindings/Rust/src/web_assembly/mod.rs
index c1e50bce5ab..272e6e51442 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/web_assembly/mod.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/web_assembly/mod.rs
@@ -210,7 +210,7 @@ thread_local! {
impl Agent {
/// The agent of the current thread, which the first use on it creates. Each thread is one agent with one VM and one
- /// heap, which the agent outlives.
+ /// heap, which outlive it: shut_down() ends it before they go.
pub(crate) fn the(vm: &Vm) -> &'static Agent {
if let Some(agent) = AGENT.get() {
return agent;
@@ -255,6 +255,18 @@ impl Agent {
agent
}
+ /// Ends the agent of the current thread, if it has one, while its heap is alive. The store and the root of the
+ /// JavaScript exception tag point into the heap, and whatever cells of the store remain go with the heap.
+ pub(crate) fn shut_down() {
+ let Some(agent) = AGENT.take() else {
+ return;
+ };
+ // SAFETY: The root belongs to the agent, which is going away, and the heap it is in is alive.
+ unsafe { gc_root_destroy(agent._js_tag_root.as_ptr()) };
+ // SAFETY: the() leaked this box, and AGENT held the only way to reach the agent.
+ drop(unsafe { Box::from_raw(std::ptr::from_ref(agent).cast_mut()) });
+ }
+
/// The agent of the current thread, which must have one.
pub(crate) fn current() -> &'static Agent {
AGENT.get().expect("the thread has an agent")
diff --git a/Libraries/LibWeb/Bindings/WrapperWorld.h b/Libraries/LibWeb/Bindings/WrapperWorld.h
index b80d78bdf49..2d7e682d27e 100644
--- a/Libraries/LibWeb/Bindings/WrapperWorld.h
+++ b/Libraries/LibWeb/Bindings/WrapperWorld.h
@@ -126,6 +126,8 @@ public:
m_entries.for_each_live_value([&](auto& cache) { callback(*cache); });
}
+ void clear() { m_entries.clear(); }
+
private:
// The per-key cache is held behind an OwnPtr so this map can be embedded as a
// member with only a forward declaration of Value (e.g. WindowProxy in
diff --git a/Libraries/LibWeb/Crypto/CryptoKey.cpp b/Libraries/LibWeb/Crypto/CryptoKey.cpp
index 5931465a2f8..90bafd41524 100644
--- a/Libraries/LibWeb/Crypto/CryptoKey.cpp
+++ b/Libraries/LibWeb/Crypto/CryptoKey.cpp
@@ -57,6 +57,15 @@ static CryptoKeyCacheEntry& cache_for(CryptoKey& key)
return caches.last();
}
+}
+
+void clear_crypto_key_caches()
+{
+ crypto_key_caches().clear();
+}
+
+namespace {
+
static JS::ThrowCompletionOr<GC::Ref<JS::Uint8Array>> create_uint8_array_from_bytes(JS::Realm& realm, ReadonlyBytes bytes)
{
auto array = TRY(JS::Uint8Array::create(realm, bytes.size()));
diff --git a/Libraries/LibWeb/Crypto/CryptoKey.h b/Libraries/LibWeb/Crypto/CryptoKey.h
index e283b68cc1b..b07ca17db97 100644
--- a/Libraries/LibWeb/Crypto/CryptoKey.h
+++ b/Libraries/LibWeb/Crypto/CryptoKey.h
@@ -161,4 +161,7 @@ struct CryptoKeyPair {
GC::Ref<CryptoKey> private_key;
};
+// For an agent that goes away, whose weak references the cache would keep beyond the end of its heap.
+void clear_crypto_key_caches();
+
}
diff --git a/Libraries/LibWeb/Fetch/Fetching/BodyStreamPullSource.cpp b/Libraries/LibWeb/Fetch/Fetching/BodyStreamPullSource.cpp
index 6b02f5f56a6..31f40fc60a3 100644
--- a/Libraries/LibWeb/Fetch/Fetching/BodyStreamPullSource.cpp
+++ b/Libraries/LibWeb/Fetch/Fetching/BodyStreamPullSource.cpp
@@ -38,6 +38,7 @@ public:
m_armed_sources.ensure(token, [&] { return GC::make_root(source); });
}
void disarm(BodyStreamWakeToken token) { m_armed_sources.remove(token); }
+ void disarm_all() { m_armed_sources.clear(); }
void notify(BodyStreamWakeToken token, Engine::WakeTicket ticket)
{
@@ -53,6 +54,11 @@ private:
HashMap<BodyStreamWakeToken, GC::Root<BodyStreamPullSource>> m_armed_sources;
};
+void disarm_all_body_stream_wakes()
+{
+ BodyStreamWakeRegistry::the().disarm_all();
+}
+
NonnullRefPtr<BodyStreamWakeSink> BodyStreamWakeSink::create()
{
return adopt_ref(*new BodyStreamWakeSink(BodyStreamWakeRegistry::the().allocate_token()));
diff --git a/Libraries/LibWeb/Fetch/Fetching/BodyStreamPullSource.h b/Libraries/LibWeb/Fetch/Fetching/BodyStreamPullSource.h
index 81472573431..88d7e7b951b 100644
--- a/Libraries/LibWeb/Fetch/Fetching/BodyStreamPullSource.h
+++ b/Libraries/LibWeb/Fetch/Fetching/BodyStreamPullSource.h
@@ -23,6 +23,9 @@ class BodyStreamPullSource;
AK_TYPEDEF_DISTINCT_ORDERED_ID(u64, BodyStreamWakeToken);
// Delivers a channel's wakes to the pull source registered under its token, on the loop that created it.
+// Lets go of every pull source that is waiting for a wake on this thread, for an agent that goes away.
+void disarm_all_body_stream_wakes();
+
class BodyStreamWakeSink final : public Engine::FetchByteChannelConsumerSink {
public:
static NonnullRefPtr<BodyStreamWakeSink> create();
diff --git a/Libraries/LibWeb/HTML/MessageEvent.cpp b/Libraries/LibWeb/HTML/MessageEvent.cpp
index f39f973dbda..872b2a2bcbb 100644
--- a/Libraries/LibWeb/HTML/MessageEvent.cpp
+++ b/Libraries/LibWeb/HTML/MessageEvent.cpp
@@ -27,6 +27,11 @@ static Bindings::WrapperWorldWeakValueCacheMap<MessageEvent, JS::Array>& message
return *caches;
}
+void clear_message_event_ports_caches()
+{
+ message_event_ports_caches().clear();
+}
+
static void clear_ports_cache(MessageEvent& event)
{
message_event_ports_caches().cache_for(event).clear();
diff --git a/Libraries/LibWeb/HTML/MessageEvent.h b/Libraries/LibWeb/HTML/MessageEvent.h
index 4ee0287a68e..386c5ffca93 100644
--- a/Libraries/LibWeb/HTML/MessageEvent.h
+++ b/Libraries/LibWeb/HTML/MessageEvent.h
@@ -88,4 +88,7 @@ private:
Vector<GC::Ref<MessagePort>> m_ports;
};
+// For an agent that goes away, whose weak references the cache would keep beyond the end of its heap.
+void clear_message_event_ports_caches();
+
}
diff --git a/Libraries/LibWeb/Loader/ResourceLoader.cpp b/Libraries/LibWeb/Loader/ResourceLoader.cpp
index 6e53ad101e1..45a025a1867 100644
--- a/Libraries/LibWeb/Loader/ResourceLoader.cpp
+++ b/Libraries/LibWeb/Loader/ResourceLoader.cpp
@@ -60,6 +60,19 @@ void ResourceLoader::initialize(GC::Heap& heap)
resource_loader() = adopt_ref(*new ResourceLoader(heap));
}
+void ResourceLoader::shut_down()
+{
+ resource_loader() = nullptr;
+}
+
+void ResourceLoader::stop_all_requests()
+{
+ // NB: A stopped request removes itself from the set.
+ auto active_requests = m_active_requests;
+ for (auto const& request : active_requests)
+ request->stop();
+}
+
bool ResourceLoader::is_initialized()
{
return resource_loader() != nullptr;
diff --git a/Libraries/LibWeb/Loader/ResourceLoader.h b/Libraries/LibWeb/Loader/ResourceLoader.h
index e5b5fdade57..083e0a5c5f3 100644
--- a/Libraries/LibWeb/Loader/ResourceLoader.h
+++ b/Libraries/LibWeb/Loader/ResourceLoader.h
@@ -32,6 +32,9 @@ class WEB_API ResourceLoader : public Core::EventReceiver {
public:
static void initialize(GC::Heap&);
static bool is_initialized();
+
+ // Forgets the loader, once its requests are stopped.
+ static void shut_down();
static ResourceLoader& the();
static void set_proxy_autoconfig_url(URL::URL);
@@ -46,6 +49,9 @@ public:
using OnCachedBodyAvailable = GC::Function<void(Core::ImmutableBytes data)>;
using OnComplete = GC::Function<void(bool success, Requests::RequestTimingInfo const& timing_info, Optional<StringView> error_message)>;
+ // A request keeps roots into the heap of the agent that made it.
+ void stop_all_requests();
+
RefPtr<Fetch::Engine::NetworkTransportHandle> load(LoadRequest&, GC::Root<OnHeadersReceived>, GC::Root<OnDataReceived>, GC::Root<OnCachedBodyAvailable>, GC::Root<OnComplete>, Requests::RequestClient::TransferLease = Requests::RequestClient::TransferLease::No);
RefPtr<Requests::RequestClient> request_client_for(Optional<HTTP::NetworkIsolationKey> const&) const;
diff --git a/Libraries/LibWeb/PerformanceTimeline/PerformanceObserver.cpp b/Libraries/LibWeb/PerformanceTimeline/PerformanceObserver.cpp
index 7177e9c0204..8f4e20d9469 100644
--- a/Libraries/LibWeb/PerformanceTimeline/PerformanceObserver.cpp
+++ b/Libraries/LibWeb/PerformanceTimeline/PerformanceObserver.cpp
@@ -35,6 +35,11 @@ static Bindings::WrapperWorldWeakValueCacheMap<JS::Object, JS::Object>& supporte
return *caches;
}
+void clear_supported_entry_types_caches()
+{
+ supported_entry_types_caches().clear();
+}
+
static Bindings::WrapperWorldWeakValueCache<JS::Object>& supported_entry_types_cache_for(JS::Object& global_object)
{
return supported_entry_types_caches().cache_for(global_object);
diff --git a/Libraries/LibWeb/PerformanceTimeline/PerformanceObserver.h b/Libraries/LibWeb/PerformanceTimeline/PerformanceObserver.h
index 64647467e04..6844496d7b9 100644
--- a/Libraries/LibWeb/PerformanceTimeline/PerformanceObserver.h
+++ b/Libraries/LibWeb/PerformanceTimeline/PerformanceObserver.h
@@ -80,4 +80,7 @@ private:
Vector<PerformanceObserverInit> m_options_list;
};
+// For an agent that goes away, whose weak references the cache would keep beyond the end of its heap.
+void clear_supported_entry_types_caches();
+
}
diff --git a/Libraries/LibWeb/PermissionsAPI/PermissionStore.h b/Libraries/LibWeb/PermissionsAPI/PermissionStore.h
index 8aaffa536c6..4b0eba50c17 100644
--- a/Libraries/LibWeb/PermissionsAPI/PermissionStore.h
+++ b/Libraries/LibWeb/PermissionsAPI/PermissionStore.h
@@ -56,6 +56,9 @@ public:
// The sites this process holds decisions for.
Vector<String> cached_origins_for_testing() const;
+ // For an agent that goes away, whose statuses have to go with its heap.
+ void forget_statuses() { m_statuses.clear(); }
+
private:
PermissionStore() = default;
diff --git a/Libraries/LibWeb/Platform/EventLoopPlugin.cpp b/Libraries/LibWeb/Platform/EventLoopPlugin.cpp
index db5c6168a4b..a3fc3571573 100644
--- a/Libraries/LibWeb/Platform/EventLoopPlugin.cpp
+++ b/Libraries/LibWeb/Platform/EventLoopPlugin.cpp
@@ -35,9 +35,18 @@ void EventLoopPlugin::spin_until(GC::Root<GC::Function<bool()>> goal_condition)
});
}
+static thread_local bool s_discard_deferred_invocations_on_this_thread { false };
+
+void EventLoopPlugin::set_discard_deferred_invocations_on_this_thread(bool discard)
+{
+ s_discard_deferred_invocations_on_this_thread = discard;
+}
+
void EventLoopPlugin::deferred_invoke(GC::Root<GC::Function<void()>> function)
{
Core::deferred_invoke([function = move(function)]() {
+ if (s_discard_deferred_invocations_on_this_thread)
+ return;
function->function()();
});
}
diff --git a/Libraries/LibWeb/Platform/EventLoopPlugin.h b/Libraries/LibWeb/Platform/EventLoopPlugin.h
index 7ad60d29215..3ec935cc41d 100644
--- a/Libraries/LibWeb/Platform/EventLoopPlugin.h
+++ b/Libraries/LibWeb/Platform/EventLoopPlugin.h
@@ -24,6 +24,10 @@ public:
void spin_until(GC::Root<GC::Function<bool()>> goal_condition);
void deferred_invoke(ESCAPING GC::Root<GC::Function<void()>>);
+
+ // For an agent that goes away on this thread: what was deferred until now goes unrun.
+ static void set_discard_deferred_invocations_on_this_thread(bool);
+
void quit();
};
diff --git a/Libraries/LibWeb/ServiceWorker/ServiceWorkerContainer.cpp b/Libraries/LibWeb/ServiceWorker/ServiceWorkerContainer.cpp
index 36ad54e9b51..7785930144a 100644
--- a/Libraries/LibWeb/ServiceWorker/ServiceWorkerContainer.cpp
+++ b/Libraries/LibWeb/ServiceWorker/ServiceWorkerContainer.cpp
@@ -41,6 +41,11 @@ static Vector<ServiceWorkerContainerReadyPromiseCacheEntry>& service_worker_cont
return *caches;
}
+void clear_service_worker_container_ready_promise_caches()
+{
+ service_worker_container_ready_promise_caches().clear();
+}
+
static void prune_service_worker_container_ready_promise_caches()
{
service_worker_container_ready_promise_caches().remove_all_matching([](auto const& entry) {
diff --git a/Libraries/LibWeb/ServiceWorker/ServiceWorkerContainer.h b/Libraries/LibWeb/ServiceWorker/ServiceWorkerContainer.h
index cc26fd2b72b..78aaa77a074 100644
--- a/Libraries/LibWeb/ServiceWorker/ServiceWorkerContainer.h
+++ b/Libraries/LibWeb/ServiceWorker/ServiceWorkerContainer.h
@@ -62,4 +62,7 @@ private:
GC::Ref<HTML::EnvironmentSettingsObject> m_service_worker_client;
};
+// For an agent that goes away, whose weak references the cache would keep beyond the end of its heap.
+void clear_service_worker_container_ready_promise_caches();
+
}