Skip to content

Commit eaf0efa

Browse files
committed
[JSC] Stop a heap's local Collector threads when the heap shuts down
https://bugs.webkit.org/show_bug.cgi?id=328418 rdar://190202961 Reviewed by Marcus Plutowski. Heap::shutDown() waits for the Heap's Leave request to be processed by the Collector. However, this can happen before the collector thread has returned from m_stopIfNecessaryTimer->scheduleSoon(): 1. The collector thread is conducting a collection and calls Heap::stopTheMutator(). The mutator holds heap access, so instead of stopping it, the collector thread hands it the conn by setting mutatorHasConnBit. 2. The mutator thread, inside waitForCollector(), sees it has the conn, finishes the collection and processes its own Leave request. 3. The collector thread hasn't yet returned from m_stopIfNecessaryTimer->scheduleSoon(), which uses Heap state. 4. The mutator thread continues through ~VM, which unregisters the VM from JSRunLoopTimer::Manager. ~Heap then destroys m_stopIfNecessaryTimer before m_localCollector, so ~Collector joins the collector thread too late. 5. The collector thread accesses the freed m_stopIfNecessaryTimer, or hits the RELEASE_ASSERT in JSRunLoopTimer::Manager::scheduleTimer() since the VM has unregistered. Before 323479@main, Heap::shutDown() joined the collector thread before tearing anything down. Restore that: Collector::shutDown() stops the collect-continuously thread and the collector thread, and Heap::shutDown() calls it for a local Collector right after leaving. ~Collector now asserts that the threads are already stopped instead of stopping them itself. This will need to be fixed for the --useGlobalGC=true case too, but that depends on how stopping the mutators of several heaps will work, so it will come later. For now, this fixes the default --useGlobalGC=false case. Test: JSTests/stress/many-agents-teardown.js * JSTests/stress/many-agents-teardown.js: Added. (i.setTimeout): * Source/JavaScriptCore/heap/Collector.cpp: (JSC::Collector::~Collector): (JSC::Collector::shutDown): (JSC::Collector::stopCollectingContinuously): * Source/JavaScriptCore/heap/Collector.h: * Source/JavaScriptCore/heap/Heap.cpp: (JSC::Heap::shutDown): Canonical link: https://commits.webkit.org/324477@main
1 parent 67cd519 commit eaf0efa

4 files changed

Lines changed: 44 additions & 2 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//@ memoryHog!
2+
//@ runDefault("--collectContinuously=1", "--collectContinuouslyPeriodMS=0.1")
3+
4+
// Destroys many agent VMs while each one's collector thread is collecting continuously. Agents start
5+
// other agents so that hundreds of VMs are alive and being torn down at once, and every agent's pending
6+
// timers keep the process-wide JSRunLoopTimer state busy while those VMs are destroyed.
7+
8+
const rootAgents = 16;
9+
const leafAgentsPerRoot = 32;
10+
const durationMS = 40;
11+
12+
const leafSource = `
13+
let values = [];
14+
for (let i = 0; i < 50; i++)
15+
setTimeout(() => { values.push(i); }, i);
16+
`;
17+
18+
const rootSource = `
19+
const start = $.agent.monotonicNow();
20+
for (let i = 0; i < ${leafAgentsPerRoot} && $.agent.monotonicNow() - start < ${durationMS}; i++)
21+
$.agent.start(${JSON.stringify(leafSource)});
22+
`;
23+
24+
for (let i = 0; i < rootAgents; i++)
25+
$.agent.start(rootSource);
26+
$.agent.sleep(durationMS);

‎Source/JavaScriptCore/heap/Collector.cpp‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,19 @@ Collector& Collector::globalCollector()
160160

161161
Collector::~Collector()
162162
{
163-
ASSERT(m_heaps.isEmpty());
163+
RELEASE_ASSERT(m_heaps.isEmpty());
164+
RELEASE_ASSERT(!m_collectContinuouslyThread);
165+
{
166+
Locker locker { *m_threadLock };
167+
RELEASE_ASSERT(m_threadShouldStop);
168+
}
169+
m_raceMarkStack->clear();
170+
}
171+
172+
void Collector::shutDown()
173+
{
164174
stopCollectingContinuously();
165175
stopThread();
166-
m_raceMarkStack->clear();
167176
}
168177

169178
void Collector::stopThread()
@@ -1112,6 +1121,7 @@ void Collector::stopCollectingContinuously()
11121121
m_collectContinuouslyCondition.notifyOne();
11131122
}
11141123
RefPtr { m_collectContinuouslyThread }->waitForCompletion();
1124+
m_collectContinuouslyThread = nullptr;
11151125
}
11161126

11171127
} // namespace JSC

‎Source/JavaScriptCore/heap/Collector.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ class Collector {
164164
void startCollectingContinuously();
165165
void stopCollectingContinuously();
166166

167+
void shutDown();
167168
void stopThread();
168169

169170
enum class RunCurrentPhaseResult {

‎Source/JavaScriptCore/heap/Heap.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,11 @@ void Heap::shutDown()
466466
dataLogIf(Options::logGC(), "left the collector, ");
467467
}
468468

469+
if (m_localCollector) {
470+
m_localCollector->shutDown();
471+
dataLogIf(Options::logGC(), "stopped the collector's threads, ");
472+
}
473+
469474
if (Options::dumpHeapStatisticsAtVMDestruction()) [[unlikely]]
470475
dumpHeapStatisticsAtVMDestruction();
471476

0 commit comments

Comments
 (0)