Skip to content

Truly independent web browser

LibWeb: Hold outstanding rejected promises weakly for real

Andreas Kling96745c42d009
diff --git a/Libraries/LibWeb/HTML/UniversalGlobalScope.cpp b/Libraries/LibWeb/HTML/UniversalGlobalScope.cpp
index 49b669f291b..d128fca1193 100644
--- a/Libraries/LibWeb/HTML/UniversalGlobalScope.cpp
+++ b/Libraries/LibWeb/HTML/UniversalGlobalScope.cpp
@@ -32,7 +32,6 @@ static JS::Realm& relevant_realm_for_universal_global_scope(DOM::EventTarget& gl
void UniversalGlobalScopeMixin::visit_edges(GC::Cell::Visitor& visitor)
{
- visitor.ignore(m_outstanding_rejected_promises_weak_set);
visitor.visit(m_about_to_be_notified_rejected_promises_list);
}
@@ -43,8 +42,8 @@ void UniversalGlobalScopeMixin::push_onto_outstanding_rejected_promises_weak_set
bool UniversalGlobalScopeMixin::remove_from_outstanding_rejected_promises_weak_set(GC::Ptr<JS::Promise> promise)
{
- return m_outstanding_rejected_promises_weak_set.remove_first_matching([&](GC::Ptr<JS::Promise> promise_in_set) {
- return promise == promise_in_set;
+ return m_outstanding_rejected_promises_weak_set.remove_first_matching([&](auto const& promise_in_set) {
+ return promise == promise_in_set.ptr();
});
}
@@ -86,6 +85,9 @@ void UniversalGlobalScopeMixin::notify_about_rejected_promises(Badge<EventLoop>)
auto& self = as<UniversalGlobalScopeMixin>(global);
auto& realm = relevant_realm_for_universal_global_scope(global);
+ // NB: The weak set drops the promises that died once here, rather than each time a promise is added to it.
+ self.m_outstanding_rejected_promises_weak_set.remove_all_matching([](auto const& promise_in_set) { return !promise_in_set; });
+
// 1. For each promise p in list:
for (auto const& promise : list->elements()) {
@@ -114,7 +116,7 @@ void UniversalGlobalScopeMixin::notify_about_rejected_promises(Badge<EventLoop>)
// 4. If p's [[PromiseIsHandled]] internal slot is false, add p to settings object's outstanding rejected promises weak set.
if (!promise->is_handled())
- self.m_outstanding_rejected_promises_weak_set.append(*promise);
+ self.push_onto_outstanding_rejected_promises_weak_set(promise);
// This algorithm results in promise rejections being marked as handled or not handled. These concepts parallel handled and not handled script errors.
// If a rejection is still not handled after this, then the rejection may be reported to a developer console.
diff --git a/Libraries/LibWeb/HTML/UniversalGlobalScope.h b/Libraries/LibWeb/HTML/UniversalGlobalScope.h
index db70239158a..ad5ab105af2 100644
--- a/Libraries/LibWeb/HTML/UniversalGlobalScope.h
+++ b/Libraries/LibWeb/HTML/UniversalGlobalScope.h
@@ -10,6 +10,7 @@
#include <AK/Forward.h>
#include <LibGC/HeapVector.h>
+#include <LibGC/Weak.h>
#include <LibWeb/Forward.h>
#include <LibWeb/HTML/Scripting/ImportMap.h>
@@ -43,7 +44,7 @@ protected:
private:
GC::Ptr<GC::HeapVector<GC::Ref<JS::Promise>>> m_about_to_be_notified_rejected_promises_list;
- Vector<GC::Ptr<JS::Promise>> m_outstanding_rejected_promises_weak_set;
+ Vector<GC::Weak<JS::Promise>> m_outstanding_rejected_promises_weak_set;
ImportMap m_import_map;
};
diff --git a/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp b/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp
index 62c69cb66c1..36b85b01937 100644
--- a/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp
+++ b/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp
@@ -121,7 +121,6 @@ void WindowOrWorkerGlobalScopeMixin::visit_edges(JS::Cell::Visitor& visitor)
visitor.visit(m_crypto);
visitor.visit(m_cache_storage);
visitor.visit(m_trusted_type_policy_factory);
- visitor.ignore(m_outstanding_rejected_promises_weak_set);
visitor.visit(m_about_to_be_notified_rejected_promises_list);
}
@@ -1376,8 +1375,8 @@ void WindowOrWorkerGlobalScopeMixin::push_onto_outstanding_rejected_promises_wea
bool WindowOrWorkerGlobalScopeMixin::remove_from_outstanding_rejected_promises_weak_set(GC::Ptr<JS::Promise> promise)
{
- return m_outstanding_rejected_promises_weak_set.remove_first_matching([&](GC::Ptr<JS::Promise> promise_in_set) {
- return promise == promise_in_set;
+ return m_outstanding_rejected_promises_weak_set.remove_first_matching([&](auto const& promise_in_set) {
+ return promise == promise_in_set.ptr();
});
}
@@ -1419,6 +1418,9 @@ void WindowOrWorkerGlobalScopeMixin::notify_about_rejected_promises(Badge<EventL
auto& self = as<WindowOrWorkerGlobalScopeMixin>(global);
auto& realm = relevant_realm(self);
+ // NB: The weak set drops the promises that died once here, rather than each time a promise is added to it.
+ self.m_outstanding_rejected_promises_weak_set.remove_all_matching([](auto const& promise_in_set) { return !promise_in_set; });
+
// 1. For each promise p in list:
for (auto const& promise : list->elements()) {
@@ -1447,7 +1449,7 @@ void WindowOrWorkerGlobalScopeMixin::notify_about_rejected_promises(Badge<EventL
// 4. If p's [[PromiseIsHandled]] internal slot is false, add p to settings object's outstanding rejected promises weak set.
if (!promise->is_handled())
- self.m_outstanding_rejected_promises_weak_set.append(*promise);
+ self.push_onto_outstanding_rejected_promises_weak_set(promise);
// This algorithm results in promise rejections being marked as handled or not handled. These concepts parallel handled and not handled script errors.
// If a rejection is still not handled after this, then the rejection may be reported to a developer console.
diff --git a/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.h b/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.h
index 0677c89e2ab..a2c75ef2c50 100644
--- a/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.h
+++ b/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.h
@@ -16,6 +16,7 @@
#include <AK/Utf16View.h>
#include <AK/Variant.h>
#include <LibCore/Forward.h>
+#include <LibGC/Weak.h>
#include <LibJS/Runtime/Value.h>
#include <LibWeb/Export.h>
#include <LibWeb/Forward.h>
@@ -202,7 +203,7 @@ private:
// https://html.spec.whatwg.org/multipage/webappapis.html#outstanding-rejected-promises-weak-set
// The outstanding rejected promises weak set must not create strong references to any of its members, and implementations are free to limit its size, e.g. by removing old entries from it when new ones are added.
- Vector<GC::Ptr<JS::Promise>> m_outstanding_rejected_promises_weak_set;
+ Vector<GC::Weak<JS::Promise>> m_outstanding_rejected_promises_weak_set;
// https://html.spec.whatwg.org/multipage/webappapis.html#concept-global-import-map
// A global object has an import map, initially an empty import map.
diff --git a/Tests/LibWeb/Text/expected/rejectionhandled-after-promise-moved.txt b/Tests/LibWeb/Text/expected/rejectionhandled-after-promise-moved.txt
new file mode 100644
index 00000000000..91fa3205793
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/rejectionhandled-after-promise-moved.txt
@@ -0,0 +1,2 @@
+unhandledrejection: moved
+rejectionhandled: moved
diff --git a/Tests/LibWeb/Text/input/rejectionhandled-after-promise-moved.html b/Tests/LibWeb/Text/input/rejectionhandled-after-promise-moved.html
new file mode 100644
index 00000000000..8ab02b31019
--- /dev/null
+++ b/Tests/LibWeb/Text/input/rejectionhandled-after-promise-moved.html
@@ -0,0 +1,27 @@
+<!DOCTYPE html>
+<script src="include.js"></script>
+<script>
+ asyncTest(async done => {
+ const events = [];
+ window.addEventListener("unhandledrejection", event => {
+ event.preventDefault();
+ events.push(`unhandledrejection: ${event.reason}`);
+ });
+ window.addEventListener("rejectionhandled", event => {
+ events.push(`rejectionhandled: ${event.reason}`);
+ });
+
+ // NB: Only the property of the holder refers to the promise, so a collection that moves cells can move it while
+ // it is in the outstanding rejected promises weak set.
+ const holder = { promise: Promise.reject("moved") };
+ await waitForCondition(() => events.length === 1);
+
+ await internals.gcAsync();
+
+ holder.promise.catch(() => {});
+ await waitForCondition(() => events.length === 2);
+
+ events.forEach(println);
+ done();
+ });
+</script>