Repository navigation
Survive a refused thread: GC helpers, collector, JIT worklist, VMTraps and libpas retry instead of crashing #569
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
f7c602c
b13245f
d376f31
ea6aaa8
674a5c6
53a8b74
0ef78c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -361,7 +361,10 @@ Heap::Heap(VM& vm, HeapType heapType) | |||||||||||||||||
| , m_sharedMutatorMarkStack(makeUnique<MarkStackArray>()) | ||||||||||||||||||
| , m_helperClient(&heapHelperPool()) | ||||||||||||||||||
| , m_threadLock(Box<Lock>::create()) | ||||||||||||||||||
| , m_threadCondition(AutomaticThreadCondition::create()) | ||||||||||||||||||
| // If the OS refuses to create the collector thread (pthread_create EAGAIN under a thread or pid | ||||||||||||||||||
| // limit), the mutator finishes the collection itself. See takeConnBecauseCollectorThreadCouldNotStart() | ||||||||||||||||||
| // and collectAsTheCollectorBecauseCollectorThreadCouldNotStart(). | ||||||||||||||||||
| , m_threadCondition(AutomaticThreadCondition::create(AutomaticThreadCondition::StartFailure::Retry)) | ||||||||||||||||||
|
|
||||||||||||||||||
| // HeapCellTypes | ||||||||||||||||||
| , auxiliaryHeapCellType(CellAttributes(DoesNotNeedDestruction, HeapCell::Auxiliary)) | ||||||||||||||||||
|
|
@@ -2213,6 +2216,7 @@ NEVER_INLINE void Heap::collectInMutatorThread() | |||||||||||||||||
| template<typename Func> | ||||||||||||||||||
| void Heap::waitForCollector(const Func& func) | ||||||||||||||||||
| { | ||||||||||||||||||
| bool collectorThreadCouldNotStart = false; | ||||||||||||||||||
| for (;;) { | ||||||||||||||||||
| bool done; | ||||||||||||||||||
| { | ||||||||||||||||||
|
|
@@ -2236,12 +2240,22 @@ void Heap::waitForCollector(const Func& func) | |||||||||||||||||
| m_mutatorDidRun = true; | ||||||||||||||||||
| // FIXME: We wouldn't need this if stopIfNecessarySlow() had a mode where it knew to just | ||||||||||||||||||
| // do the collection. | ||||||||||||||||||
| relinquishConn(); | ||||||||||||||||||
| if (!collectorThreadCouldNotStart) | ||||||||||||||||||
| relinquishConn(); | ||||||||||||||||||
|
|
||||||||||||||||||
| if (done) { | ||||||||||||||||||
| clearMutatorWaiting(); // Clean up just in case. | ||||||||||||||||||
| return; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| // We still have the conn if the collector thread could not be created. Nobody else can finish | ||||||||||||||||||
| // this collection, so this thread plays the collector too (see the function). It stops | ||||||||||||||||||
| // offering the conn for the rest of this wait: each offer retries the thread. | ||||||||||||||||||
| if (collectorThreadCouldNotStart || (m_worldState.load() & mutatorHasConnBit)) { | ||||||||||||||||||
| collectorThreadCouldNotStart = true; | ||||||||||||||||||
| collectAsTheCollectorBecauseCollectorThreadCouldNotStart(); | ||||||||||||||||||
| continue; | ||||||||||||||||||
| } | ||||||||||||||||||
|
Comment on lines
+2254
to
+2258
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔴 Extended reasoning...Normal (non-thread-limited) Verification: normal — this change introduces a race the base branch does not have. Trace: - Heap.cpp:2244 |
||||||||||||||||||
|
|
||||||||||||||||||
| // If mutatorWaitingBit is still set then we want to wait. | ||||||||||||||||||
| ParkingLot::compareAndPark(&m_worldState, oldState | mutatorWaitingBit); | ||||||||||||||||||
|
|
@@ -2324,20 +2338,71 @@ bool Heap::relinquishConn(unsigned oldState) | |||||||||||||||||
| if (!m_worldState.compareExchangeWeak(oldState, oldState & ~mutatorHasConnBit)) | ||||||||||||||||||
| return true; // Loop around. | ||||||||||||||||||
|
|
||||||||||||||||||
| finishRelinquishingConn(); | ||||||||||||||||||
| return true; | ||||||||||||||||||
| // If the conn came back because the collector thread could not be created, we are done: looping | ||||||||||||||||||
| // around would relinquish it again and retry the thread on every iteration. | ||||||||||||||||||
| return finishRelinquishingConn(); | ||||||||||||||||||
|
Comment on lines
+2341
to
+2343
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win Do not use the handoff result as the retry-loop control value.
The failure result is also consumed here and never reaches ♻️ Proposed change- // If the conn came back because the collector thread could not be created, we are done: looping
- // around would relinquish it again and retry the thread on every iteration.
- return finishRelinquishingConn();
+ // If the conn came back because the collector thread could not be created, we are done: looping
+ // around would relinquish it again and retry the thread on every iteration. Report the failure so
+ // that waitForCollector() knows it must play the collector.
+ m_collectorThreadCouldNotStart = !finishRelinquishingConn();
+ return false; // Done.📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| void Heap::finishRelinquishingConn() | ||||||||||||||||||
| // Returns false if the conn had to be taken back because no collector thread could be created. | ||||||||||||||||||
| bool Heap::finishRelinquishingConn() | ||||||||||||||||||
| { | ||||||||||||||||||
| dataLogLnIf(HeapInternal::verbose, "Relinquished the conn."); | ||||||||||||||||||
|
|
||||||||||||||||||
| sanitizeStackForVM(vm()); | ||||||||||||||||||
|
|
||||||||||||||||||
| Locker locker { *m_threadLock }; | ||||||||||||||||||
| if (!m_requests.isEmpty()) | ||||||||||||||||||
| m_threadCondition->notifyOne(locker); | ||||||||||||||||||
| bool handedOff = true; | ||||||||||||||||||
| if (!m_requests.isEmpty() && !m_threadCondition->notifyOne(locker)) { | ||||||||||||||||||
| takeConnBecauseCollectorThreadCouldNotStart(locker); | ||||||||||||||||||
| handedOff = false; | ||||||||||||||||||
| } | ||||||||||||||||||
| ParkingLot::unparkAll(&m_worldState); | ||||||||||||||||||
| return handedOff; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| // The OS refused to create the collector thread (pthread_create EAGAIN: RLIMIT_NPROC, a cgroup pids | ||||||||||||||||||
| // limit). Nothing else can serve the pending requests, so the mutator keeps the conn and runs the | ||||||||||||||||||
| // collection itself at its stopIfNecessary() polls, the same way it does after stealing the conn in | ||||||||||||||||||
| // requestCollection(). A stop request that releaseAccessSlow() left for the collector is withdrawn | ||||||||||||||||||
| // for the same reason: the collector that would have resumed the mutator does not exist. | ||||||||||||||||||
| void Heap::takeConnBecauseCollectorThreadCouldNotStart(const AbstractLocker&) | ||||||||||||||||||
| { | ||||||||||||||||||
| dataLogLnIf(HeapInternal::verbose, "Taking the conn: the collector thread could not start."); | ||||||||||||||||||
| for (;;) { | ||||||||||||||||||
| unsigned oldState = m_worldState.load(); | ||||||||||||||||||
| unsigned newState = (oldState | mutatorHasConnBit) & ~stoppedBit; | ||||||||||||||||||
| if (m_worldState.compareExchangeWeak(oldState, newState)) | ||||||||||||||||||
| return; | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| // Called by a mutator that waits for a collection while no collector thread can be created. With the | ||||||||||||||||||
| // conn, the mutator runs only the phases that stop the world; the concurrent phase is the collector's, | ||||||||||||||||||
| // and with the stochastic scheduler it lasts until marking terminates or the mutator allocates its | ||||||||||||||||||
| // headroom. A waiting mutator allocates nothing, so without a collector the collection would never | ||||||||||||||||||
| // end. So this thread hands the conn to the collector side and runs the collector's phases itself. | ||||||||||||||||||
| // The collector side hands the conn straight back when it needs the world stopped (stopTheMutator() | ||||||||||||||||||
| // sees that the mutator has heap access), and the caller's loop continues as the mutator. No real | ||||||||||||||||||
| // collector thread can appear in the meantime: only this thread's notifies start one. | ||||||||||||||||||
| void Heap::collectAsTheCollectorBecauseCollectorThreadCouldNotStart() | ||||||||||||||||||
| { | ||||||||||||||||||
| dataLogLnIf(HeapInternal::verbose, "Running the collector's phases on the mutator thread: the collector thread could not start."); | ||||||||||||||||||
| for (;;) { | ||||||||||||||||||
| unsigned oldState = m_worldState.load(); | ||||||||||||||||||
| RELEASE_ASSERT(oldState & hasAccessBit); | ||||||||||||||||||
| if (!(oldState & mutatorHasConnBit)) | ||||||||||||||||||
| break; | ||||||||||||||||||
| if (m_worldState.compareExchangeWeak(oldState, oldState & ~mutatorHasConnBit)) | ||||||||||||||||||
| break; | ||||||||||||||||||
| } | ||||||||||||||||||
| sanitizeStackForVM(vm()); | ||||||||||||||||||
| // The collector side waits for the mutator to run a pending epilogue before it stops the world. | ||||||||||||||||||
| // Here both sides are this thread, so run it first. | ||||||||||||||||||
| handleNeedCollectionEpilogue(); | ||||||||||||||||||
| CollectingScope collectingScope(*this); | ||||||||||||||||||
| collectInCollectorThread(); | ||||||||||||||||||
|
Comment on lines
+2402
to
+2403
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🔴 Critical | ⚡ Quick win Use the mutator collection path with 🤖 Prompt for AI Agents |
||||||||||||||||||
| // Either stopTheMutator() gave the conn back, or the collection finished with the conn on the | ||||||||||||||||||
| // collector side, which is the idle state requestCollection() steals from. | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| void Heap::relinquishConn() | ||||||||||||||||||
|
|
@@ -2474,8 +2539,8 @@ Heap::Ticket Heap::requestCollection(GCRequest request) | |||||||||||||||||
|
|
||||||||||||||||||
| m_requests.append(request); | ||||||||||||||||||
| m_lastGrantedTicket++; | ||||||||||||||||||
| if (!(m_worldState.load() & mutatorHasConnBit)) | ||||||||||||||||||
| m_threadCondition->notifyOne(locker); | ||||||||||||||||||
| if (!(m_worldState.load() & mutatorHasConnBit) && !m_threadCondition->notifyOne(locker)) | ||||||||||||||||||
| takeConnBecauseCollectorThreadCouldNotStart(locker); | ||||||||||||||||||
| return m_lastGrantedTicket; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,6 +43,7 @@ | |
| #include "WaiterListManager.h" | ||
| #include "Watchdog.h" | ||
| #include <wtf/Condition.h> | ||
| #include <wtf/NeverDestroyed.h> | ||
| #include <wtf/ProcessID.h> | ||
| #include <wtf/Scope.h> | ||
| #include <wtf/ThreadMessage.h> | ||
|
|
@@ -268,8 +269,14 @@ class VMTraps::SignalSender final : public ThreadSafeRefCounted<VMTraps::SignalS | |
| { | ||
| if (m_scheduled) | ||
| return; | ||
| // No thread for the sender: the trap still fires at the next stack check because | ||
| // m_stack.requestStop() lowered the limit. Only a loop with no calls runs on until a | ||
| // later request can start the queue. | ||
| WorkQueue* queue = VMTraps::tryQueue(); | ||
| if (!queue) | ||
| return; | ||
|
Comment on lines
+275
to
+277
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟠 Major | ⚡ Quick win Retry queue startup while the stop request remains pending. When Keep the request retryable when queue creation fails. For example, make 🤖 Prompt for AI Agents |
||
| m_scheduled = true; | ||
| VMTraps::queue().dispatch([protectedThis = Ref { *this }] { | ||
| queue->dispatch([protectedThis = Ref { *this }] { | ||
| protectedThis->work(); | ||
| }); | ||
| } | ||
|
|
@@ -350,14 +357,21 @@ class VMTraps::SignalSender final : public ThreadSafeRefCounted<VMTraps::SignalS | |
|
|
||
| #endif // ENABLE(SIGNAL_BASED_VM_TRAPS) | ||
|
|
||
| WorkQueue* VMTraps::tryQueue() | ||
| { | ||
| static NeverDestroyed<RefPtr<WorkQueue>> workQueue; | ||
| static Lock lock; | ||
| Locker locker { lock }; | ||
| if (!workQueue.get()) | ||
| workQueue.get() = WorkQueue::tryCreate("JSC VMTraps Signal Sender"_s); | ||
| return workQueue.get().get(); | ||
| } | ||
|
|
||
| WorkQueue& VMTraps::queue() | ||
| { | ||
| static LazyNeverDestroyed<Ref<WorkQueue>> workQueue; | ||
| static std::once_flag onceKey; | ||
| std::call_once(onceKey, [&] { | ||
| workQueue.construct(WorkQueue::create("JSC VMTraps Signal Sender"_s)); | ||
| }); | ||
| return workQueue.get(); | ||
| WorkQueue* queue = tryQueue(); | ||
| RELEASE_ASSERT(queue); | ||
|
Comment on lines
370
to
+373
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟣 pre-existing: Extended reasoning...Trigger: process at its pids/nproc limit and the VMTraps queue not yet created (first use). Verification: pre-existing — the base branch already aborts on this same route (Watchdog/TerminationDeadline → VMTraps::queue → WorkQueue::create → RunLoop::create → Thread::create's |
||
| return *queue; | ||
| } | ||
|
|
||
| void VMTraps::initializeSignals() | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🔴 Critical | 🏗️ Heavy lift
Do not infer collector-thread startup failure from
mutatorHasConnBit.mutatorHasConnBitis also set on the normal handoff path.stopTheMutator()(Line 2109) transfers the conn to the mutator whenever the mutator holds heap access, andrequestCollection()steals the conn when the collector is idle. In both cases a collector thread exists.This branch latches
collectorThreadCouldNotStart = trueon the first observation of that bit. Two effects follow for the rest of the wait:relinquishConn()is never called again (Line 2243), so the conn is no longer offered to the existing collector thread.collectAsTheCollectorBecauseCollectorThreadCouldNotStart(), which clearsmutatorHasConnBitand then runscollectInCollectorThread()asGCConductor::Collector. WhilemutatorHasConnBitis clear, another thread'srequestCollection()(Line 2542) can successfully notify the realHeapThread, so two threads can run collector phases at the same time.Latch the flag only from the actual startup-failure signal, that is a
falseresult ofAutomaticThreadCondition::notifyOne()observed byfinishRelinquishingConn()orrequestCollection(). Propagate that result intowaitForCollectorinstead of testing the conn bit.🤖 Prompt for AI Agents