Skip to content

[JSC] Stop a heap's local Collector threads when the heap shuts down - #77844

Merged
webkit-commit-queue merged 1 commit into
WebKit:mainfrom
dhecht:eng/328418
Oct 22, 2026
Merged

webkit-commit-queue merged 1 commit into
WebKit:mainfrom
dhecht:eng/328418

Conversation

@dhecht

@dhecht dhecht commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

eaf0efa

[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

49909f2

Misc iOS, visionOS, tvOS & watchOS macOS Linux Windows Apple Internal
✅ 🧪 style ✅ 🛠 ios ✅ 🛠 mac ✅ 🛠 wpe ✅ 🛠 win ✅ 🛠 ios-apple
✅ 🛠 ios-sim ✅ 🛠 mac-cmake-debug ✅ 🧪 wpe-wk2 ✅ 🧪 win-tests loading 🛠 mac-apple
✅ 🧪 webkitperl ✅ 🧪 ios-wk2 ✅ 🧪 api-mac ✅ 🧪 api-wpe loading 🛠 vision-apple
✅ 🧪 ios-wk2-wpt ✅ 🧪 api-mac-cmake-debug ✅ 🧪 jsc-wpe
✅ 🧪 jsc-x86-64 ✅ 🧪 api-ios ✅ 🧪 mac-wk2 ✅ 🛠 gtk3-gcc
✅ 🛠 🧪 jsc-debug-arm64 ✅ 🛠 ios-safer-cpp ✅ 🧪 mac-cmake-debug-wk2 ✅ 🛠 gtk
✅ 🛠 vision ✅ 🧪 gtk-wk2
✅ 🛠 vision-sim ✅ 🧪 mac-intel-wk2 ✅ 🧪 api-gtk
✅ 🛠 🧪 unsafe-merge ✅ 🧪 vision-wk2 ✅ 🛠 mac-safer-cpp ✅ 🛠 playstation
✅ 🛠 tv ✅ 🧪 mac-site-isolation
✅ 🛠 tv-sim
✅ 🛠 watch
✅ 🛠 watch-sim

@dhecht dhecht self-assigned this Oct 22, 2026
@dhecht
dhecht marked this pull request as ready for review October 22, 2026 15:04
@dhecht
dhecht requested a review from a team as a code owner October 22, 2026 15:04
@dhecht dhecht added the unsafe-merge-queue Applied to send a pull request to merge-queue, but skip building and testing label Oct 22, 2026
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
@webkit-commit-queue

Copy link
Copy Markdown
Collaborator

Committed 324477@main (eaf0efa): https://commits.webkit.org/324477@main

Reviewed commits have been landed. Closing PR #77844 and removing active labels.

@webkit-commit-queue
webkit-commit-queue merged commit eaf0efa into WebKit:main Oct 22, 2026
@webkit-commit-queue webkit-commit-queue removed the unsafe-merge-queue Applied to send a pull request to merge-queue, but skip building and testing label Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants