Skip to content

Truly independent web browser

LibWeb: Prune dead entries when unregistering documents and channels

Shannon Bootha844b9aadc8b
diff --git a/Libraries/LibWeb/HTML/BroadcastChannel.cpp b/Libraries/LibWeb/HTML/BroadcastChannel.cpp
index 319d6778c6b..2ef764f3b71 100644
--- a/Libraries/LibWeb/HTML/BroadcastChannel.cpp
+++ b/Libraries/LibWeb/HTML/BroadcastChannel.cpp
@@ -54,7 +54,8 @@ void BroadcastChannelRepository::unregister_channel(GC::Ref<BroadcastChannel> ch
return;
auto& relevant_channels = maybe_channels.value();
- relevant_channels.remove_first_matching([&](auto c) { return c == channel; });
+ // When called from the channel's finalizer, the GC may already have cleared our weak pointer to it.
+ relevant_channels.remove_all_matching([&](auto const& c) { return !c || c == channel; });
if (relevant_channels.is_empty())
m_channels.remove(storage_key);
}
diff --git a/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp b/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp
index dfded7a4465..a12c4bfd17c 100644
--- a/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp
+++ b/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp
@@ -1657,8 +1657,9 @@ void EventLoop::add_stats(JsonObject& values) const
void EventLoop::unregister_document(Badge<DOM::Document>, DOM::Document& document)
{
forget_rendering_stats(document);
- bool did_remove = m_documents.remove_first_matching([&](auto& entry) { return entry.ptr().ptr() == &document; });
- VERIFY(did_remove);
+ // This runs from the document's finalizer, so the GC may already have cleared our weak pointer to it, as well as to
+ // any other document dying in the same collection.
+ m_documents.remove_all_matching([&](auto const& entry) { return !entry || entry.ptr().ptr() == &document; });
}
void EventLoop::document_navigable_did_change(Badge<DOM::Document>)