From 651f068e06459f16b58e1c40c15c1e029dead30f Mon Sep 17 00:00:00 2001 From: "Aleksei.Glushko" Date: Mon, 11 Mar 2024 10:47:03 +0100 Subject: [PATCH] [K/N] Fix a race on mark queue destruction in CMS GC Merge-request: KT-MR-14076 --- .../runtime/src/gc/cms/cpp/ConcurrentMark.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/kotlin-native/runtime/src/gc/cms/cpp/ConcurrentMark.cpp b/kotlin-native/runtime/src/gc/cms/cpp/ConcurrentMark.cpp index 7754e34fc87..3ca6220c5a9 100644 --- a/kotlin-native/runtime/src/gc/cms/cpp/ConcurrentMark.cpp +++ b/kotlin-native/runtime/src/gc/cms/cpp/ConcurrentMark.cpp @@ -78,11 +78,9 @@ void gc::mark::ConcurrentMark::runMainInSTW() { } } while (!tryTerminateMark(everSharedBatches)); - for (auto& thread : *lockedMutatorsList_) { - thread.gc().impl().gc().mark().markQueue().destroy(); - } - - endMarkingEpoch(); + // By this point mutator mark queues may not be populated anymore. + // However, some threads may still try to enqueue a marked object, before they observe the barrier disablement. + // Thus, mark queue destruction takes place only later below. gc::processWeaks(gcHandle(), mm::SpecialRefRegistry::instance()); @@ -94,6 +92,11 @@ void gc::mark::ConcurrentMark::runMainInSTW() { } barriers::disableBarriers(); + + for (auto& thread : *lockedMutatorsList_) { + thread.gc().impl().gc().mark().markQueue().destroy(); + } + endMarkingEpoch(); } void gc::mark::ConcurrentMark::runOnMutator(mm::ThreadData&) {