Truly independent web browser
LibWeb: Release the GC roots of off-thread load tasks as they run
Andreas Klingf8a7c26ee710
diff --git a/Libraries/LibWeb/HTML/HTMLLinkElement.cpp b/Libraries/LibWeb/HTML/HTMLLinkElement.cppindex a6a4228a863..140f5050024 100644--- a/Libraries/LibWeb/HTML/HTMLLinkElement.cpp+++ b/Libraries/LibWeb/HTML/HTMLLinkElement.cpp@@ -1145,8 +1145,11 @@ void HTMLLinkElement::process_stylesheet_resource(bool success, Fetch::Infrastrulink->cancel_pending_stylesheet_processing();return;}+ // NB: The task's function keeps the element and the document alive as cells it holds, not as GC+ // roots: the roots of a function that died would keep them alive through the collection that+ // finds it dead.link->queue_an_element_task(Task::Source::Networking,- [link = move(link), loaded_document = move(loaded_document), fetch_generation, location = move(location), base_url = move(base_url), parsed = move(parsed), origin_clean] {+ [link = GC::Ref { *link }, loaded_document = GC::Ref { *loaded_document }, fetch_generation, location = move(location), base_url = move(base_url), parsed = move(parsed), origin_clean] {if (fetch_generation != link->m_current_fetch_generation || !link->m_stylesheet_processing_pending)return;if (&link->document() != loaded_document.ptr() || !loaded_document->is_fully_active()diff --git a/Libraries/LibWeb/HTML/Scripting/Fetching.cpp b/Libraries/LibWeb/HTML/Scripting/Fetching.cppindex 97d57f72098..e7a25bddb5a 100644--- a/Libraries/LibWeb/HTML/Scripting/Fetching.cpp+++ b/Libraries/LibWeb/HTML/Scripting/Fetching.cpp@@ -284,7 +284,10 @@ static void prepare_bytecode_cache_off_thread(EnvironmentSettingsObject& settingauto* preparation = new BytecodeCachePreparation { move(bytecode),[settings_object = GC::make_root(settings_object), on_prepared = move(on_prepared)](RefPtr<JS::DecodedBytecodeCache> bytecode_cache) mutable {queue_global_task(Task::Source::Networking, settings_object->global_object(), GC::create_function(GC::Heap::the(), [on_prepared = move(on_prepared), bytecode_cache = move(bytecode_cache)]() mutable {- on_prepared(move(bytecode_cache));+ // NB: The callback holds GC roots. Release them as the task runs: the roots of a function that died+ // would keep what they root alive through the collection that finds it dead.+ auto steps = move(on_prepared);+ steps(move(bytecode_cache));}));} };auto& main_thread_event_loop = Core::EventLoop::current();@@ -318,7 +321,10 @@ static void compile_off_thread(EnvironmentSettingsObject& settings_object, Nonnuauto* callback = new Function<void(OffThreadCompiledProgram)>([settings_object = GC::make_root(settings_object), on_compiled = move(on_compiled), source_code = move(source_code)](OffThreadCompiledProgram result) mutable {queue_global_task(Task::Source::Networking, settings_object->global_object(), GC::create_function(GC::Heap::the(), [on_compiled = move(on_compiled), source_code = move(source_code), result = move(result)]() mutable {- on_compiled(move(result), move(source_code));+ // NB: The callback holds GC roots. Release them as the task runs: the roots of a function that died+ // would keep what they root alive through the collection that finds it dead.+ auto steps = move(on_compiled);+ steps(move(result), move(source_code));}));});