mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-28 11:49:44 +00:00
LibJS+LibGC: Run FinalizationRegistry cleanup host hook *after* GC
Before this change, it was possible for a second GC to get triggered in the middle of a first GC, due to allocations happening in the FinalizationRegistry cleanup host hook. To avoid this causing problems, we add a "post-GC task" mechanism and use that to invoke the host hook once all other GC activity is finished, and we've unset the "collecting garbage" flag. Note that the test included here only fails reliably when running with the -g flag (collect garbage after each allocation). Fixes #3051
This commit is contained in:
parent
b7a554d1f2
commit
51a91771b8
Notes:
github-actions[bot]
2025-01-23 11:11:21 +00:00
Author: https://github.com/awesomekling
Commit: 51a91771b8
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/3342
4 changed files with 55 additions and 15 deletions
|
@ -240,23 +240,35 @@ AK::JsonObject Heap::dump_graph()
|
|||
void Heap::collect_garbage(CollectionType collection_type, bool print_report)
|
||||
{
|
||||
VERIFY(!m_collecting_garbage);
|
||||
TemporaryChange change(m_collecting_garbage, true);
|
||||
|
||||
Core::ElapsedTimer collection_measurement_timer;
|
||||
if (print_report)
|
||||
collection_measurement_timer.start();
|
||||
{
|
||||
TemporaryChange change(m_collecting_garbage, true);
|
||||
|
||||
if (collection_type == CollectionType::CollectGarbage) {
|
||||
if (m_gc_deferrals) {
|
||||
m_should_gc_when_deferral_ends = true;
|
||||
return;
|
||||
Core::ElapsedTimer collection_measurement_timer;
|
||||
if (print_report)
|
||||
collection_measurement_timer.start();
|
||||
|
||||
if (collection_type == CollectionType::CollectGarbage) {
|
||||
if (m_gc_deferrals) {
|
||||
m_should_gc_when_deferral_ends = true;
|
||||
return;
|
||||
}
|
||||
HashMap<Cell*, HeapRoot> roots;
|
||||
gather_roots(roots);
|
||||
mark_live_cells(roots);
|
||||
}
|
||||
HashMap<Cell*, HeapRoot> roots;
|
||||
gather_roots(roots);
|
||||
mark_live_cells(roots);
|
||||
finalize_unmarked_cells();
|
||||
sweep_dead_cells(print_report, collection_measurement_timer);
|
||||
}
|
||||
finalize_unmarked_cells();
|
||||
sweep_dead_cells(print_report, collection_measurement_timer);
|
||||
|
||||
auto tasks = move(m_post_gc_tasks);
|
||||
for (auto& task : tasks)
|
||||
task();
|
||||
}
|
||||
|
||||
void Heap::enqueue_post_gc_task(AK::Function<void()> task)
|
||||
{
|
||||
m_post_gc_tasks.append(move(task));
|
||||
}
|
||||
|
||||
void Heap::gather_roots(HashMap<Cell*, HeapRoot>& roots)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue