Parent process 100% CPU hang and memory spike in nsSHistory::WalkContiguousEntriesInOrder due to multi-step cycle when mEntries has duplicates (followup to Bug 2042897)
Categories
(Core :: DOM: Navigation, defect)
Tracking
()
| Tracking | Status | |
|---|---|---|
| firefox163 | --- | fixed |
People
(Reporter: marlon_rando, Assigned: vhilla)
References
Details
Attachments
(2 files, 1 external file, 1 obsolete file)
|
3.98 MB,
text/plain
|
Details | |
|
4.64 KB,
patch
|
Details | Diff | Splinter Review |
note: i've experienced this hang two or three times now, and it annoywed me so much that i asked some free tool to pinpoint the issue. this is what they said
Summary
Starting in Firefox 160.0 Release, the Firefox parent process intermittently hangs at 100% CPU on the main thread while memory usage climbs rapidly to 5+ GB during normal browsing (particularly involving sites with iframes and session history commits).
Sampling the hung parent process shows the main thread stuck inside nsSHistory::WalkContiguousEntriesInOrder (docshell/shistory/nsSHistory.cpp:650), called from CanonicalBrowsingContext::HistoryCommitIndexAndLength during ContentParent::RecvHistoryCommit.
Symbolicated Stack Trace (macOS .hang sample, Firefox 160.0)
ContentParent::RecvHistoryCommit(...)
-> CanonicalBrowsingContext::HistoryCommitIndexAndLength(...)
-> nsSHistory::WalkContiguousEntriesInOrder(...) [docshell/shistory/nsSHistory.cpp:650]
-> nsSHistory::FindLeftmostAdjacentContiguousEntryFor(...)
-> nsSHistory::FindAdjacentEntryFor(...)
Root Cause Analysis
In Bug 2042897, a 1-step self-loop guard (nextEntry != aEntry and child != aEntry) was added to nsSHistory::FindAdjacentEntryFor (docshell/shistory/nsSHistory.cpp:2712-2730) to handle cases where mEntries contains duplicate top-level entries:
// As in the ancestors-empty case above, if mEntries has duplicates
// (see bug 2042897) the adjacent entry can resolve back to aEntry itself;
// returning it would make callers loop forever, so skip it.
if (child && child != aEntry &&
child->DocshellID() == aEntry->DocshellID()) {
return child;
}
However, a 1-step check (child != aEntry) does not prevent a 2-step (or N-step) cycle (ChildA -> ChildB -> ChildA):
- Suppose
mEntriescontains duplicate top-level entries separated by another entry:[Top0, Top1, Top0], whereTop0contains childChildAandTop1contains childChildB(with the sameDocshellID(), andChildA != ChildB). WalkContiguousEntriesInOrder(ChildA)callsFindLeftmostAdjacentContiguousEntryFor(ChildA, SearchDirection::Right):FindAdjacentEntryFor(ChildA, Right)looks upGetIndexOfEntry(Top0), which returns the first match at index0.- It inspects
mEntries[0 + 1](Top1) and returnsChildB(ChildB != ChildA). - Because
ChildA->GetID() != ChildB->GetID(),FindLeftmostAdjacentContiguousEntryForstops atChildBand returns it toWalkContiguousEntriesInOrder.
- On the next iteration of the
whileloop inWalkContiguousEntriesInOrder(nsSHistory.cpp:649):FindAdjacentEntryFor(ChildB, Right)looks upGetIndexOfEntry(Top1)(index1).- It inspects
mEntries[1 + 1](Top0at index2) and returnsChildA(ChildA != ChildB). WalkContiguousEntriesInOrderreceivesChildAagain and loops forever (ChildA -> ChildB -> ChildA -> ...), spinning the parent process main thread at 100% CPU and allocating unbounded memory.
Why this surfaced in Firefox 160
- Bug 2046558 (
9994f9e54becon 160 beta/release) changedCanonicalBrowsingContext::SetActiveSessionHistoryEntrysoAdoptBFCacheEntryonly sharesSHEntrySharedParentStatewhenInnerWindowId()matches and!aKeepSharedState, causing adjacent entries of the same document/subframe to have distinct shared-state IDs (aEntry->GetID() != current->GetID()). - Bug 2075414 (
87bcf6fc6afd) restrictedAdoptChildSHEntryto staticOriginalFrameSrc()loads, causing dynamically navigated iframes to commit new entries viaRecvHistoryCommit. - Together, these allow
FindLeftmostAdjacentContiguousEntryForto terminate onChildBinstead of advancing past the duplicateTop0, exposing the 2-step cycle inWalkContiguousEntriesInOrder.
Suggested Fix
Track visited SessionHistoryEntry* pointers (e.g., via nsTHashSet<SessionHistoryEntry*>) inside nsSHistory::WalkContiguousEntriesInOrder (and FindClosestAdjacentContiguousEntryFor / FindLeftmostAdjacentContiguousEntryFor) so any multi-step cycle breaks out immediately instead of looping infinitely.
| Reporter | ||
Updated•14 days ago
|
| Reporter | ||
Updated•14 days ago
|
| Reporter | ||
Comment 1•14 days ago
|
||
what matters is the observer of the phenomena and their ability to "know" something was broken
the rest is your job. i used my eyes
| Reporter | ||
Comment 2•14 days ago
|
||
Here is the updated Bugzilla comment without the testing claim, framed cleanly as a patch based on code inspection of the crash/hang stack:
Based on the hang stack and code inspection, the parent-process hang and memory spike are caused by a multi-step cycle during contiguous session history traversal in nsSHistory.cpp.
Root Cause
- When duplicate entries exist in mEntries (e.g., following the about:blank / iframe session history changes from Bug 2075414 and Bug 2046558), FindAdjacentEntryFor only guards against an immediate 1-step self-loop (child != aEntry, added in Bug 2042897).
- If the adjacent entries form a 2+ step cycle (A -> B -> A), both the outer traversal loops (WalkContiguousEntriesInOrder and WalkClosestContiguousEntriesFrom) and the inner helper loops (FindClosestAdjacentContiguousEntryFor and FindLeftmostAdjacentContiguousEntryFor) spin indefinitely.
- While walking left in WalkContiguousEntriesInOrder, previousEntries.AppendElement(previousEntry) grows unboundedly on every iteration, causing both the 100% CPU hang and rapid multi-GB RAM growth.
Fix
Track visited SessionHistoryEntry* pointers with an nsTHashSet across WalkContiguousEntriesInOrder, WalkClosestContiguousEntriesFrom, FindClosestAdjacentContiguousEntryFor, and FindLeftmostAdjacentContiguousEntryFor, breaking out of the loop if an already-visited entry is encountered.
Attaching a patch for this shortly.
Would you like a matching Mercurial commit message (Bug XXXXXXX - ...) or a crashtest / mochitest skeleton to include with the patch?
Comment 3•14 days ago
|
||
:vhilla, since you are the author of the regressor, bug 2075414, could you take a look? Also, could you set the severity field?
For more information, please visit BugBot documentation.
| Reporter | ||
Comment 4•14 days ago
|
||
Here is the optional note formatted in clean, copy-pasteable Markdown for Bugzilla or Phabricator:
Note on Reproducibility
I haven't been able to reduce a deterministic test case yet, as the duplicate mEntries topology depends on multi-process iframe navigation timing. However, the parent-process hang stack directly matches WalkContiguousEntriesInOrder and FindLeftmostAdjacentContiguousEntryFor entering a multi-step cycle (A -> B -> A), which bypasses the single-step child != aEntry guard added in Bug 2042897.
| Assignee | ||
Comment 5•14 days ago
|
||
Are you using some special Firefox setup? I can't symbolicate the hang, 1FD8B78C-F063-3FD0-A825-B3788220228A is an unknown id. Mac OS 10.14 isn't supported for 160.
Can you perhaps provide a symbolicated version and explain what Firefox you're running?
| Assignee | ||
Comment 6•14 days ago
|
||
Bug 2042897 is older than bug 2075414. Perhaps it surfaced this specific hang, but I don't see evidence for it causing duplicate entries.
| Assignee | ||
Updated•14 days ago
|
| Reporter | ||
Comment 7•14 days ago
|
||
it's not special in the way you're thinking and i'm doubtful it's caused by the adaptations made for it to function on 10.14, as i had no problem on 159. this issue arose specifically in 160. i don't think there are changes in widget/cocoa between 159 and 160 that would cause this to happen
i have rebuilt the same source with --disable-install-strip and removed the patch in hopes of discovering the problem again. i will keep you posted.
it wouldn't surprise me at all if this change caused the problem though.
| Assignee | ||
Comment 8•14 days ago
|
||
Updated•14 days ago
|
| Assignee | ||
Comment 9•14 days ago
|
||
| Reporter | ||
Comment 10•13 days ago
|
||
thanks to @glandium's outstanding focus on reproducibility in this build tree, i was actually able to grab the older clang and rebuild it, and it confirms what was originally stated. here is the symbolication:
GagansiMacPro:mozilla-unified Gagan$ xcrun atos -o obj-x86_64-apple-darwin18.7.0-release/dist/bin/XUL -arch x86_64 -l 0x10cb07000 0x111e8d2bf 0x111e8ce78 0x111e8c6d7 0x111d43bbc 0x1111f3526 0x1111f1c30 0x1111f207e 0x1111f2e78 0x1111808a5 0x10d7a6f84 0x10d7a9041 0x10d798b26 0x10d79167a 0x10d79332c 0x10d7990b3 0x10d86475e 0x10d8640d6 0x10d863d21 0x10d864c2f 0x110c8f168 0x110bb780a 0x1118071b1 0x1118143e2 0x1117fad5e 0x11187d9a8 0x11189c1e7 0x10da7497c 0x10d075f99 0x10d076229 0x10d0ac170 0x11189c151 0x10d07706b 0x10d0b16f4 0x10d077049 0x11189c0ea 0x10d0ac21d 0x11189c0b3 0x1118a3197 0x11189c134 0x10d075fff 0x10d070103 0x10d0acfa0 0x10d69afe5 0x10d69b397 0x1118a2f7c 0x10d075f5e 0x10d0ad4e4 0x10d69b306 0x10d6a1ede 0x10d69b763 0x10d07600a 0x10d0acf70
XRE_main(int, char**, mozilla::BootstrapConfig const&) (in XUL) (nsAppRunner.cpp:6888)
XREMain::XRE_main(int, char**, mozilla::BootstrapConfig const&) (in XUL) (nsAppRunner.cpp:6816)
XREMain::XRE_mainRun() (in XUL) (nsAppRunner.cpp:6561)
nsAppStartup::Run() (in XUL) (nsAppStartup.cpp:308)
nsAppShell::Run() (in XUL) (nsAppShell.mm:997)
-[GeckoNSApplication run] (in XUL) (nsAppShell.mm:185)
-[GeckoNSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] (in XUL) (nsAppShell.mm:275)
nsAppShell::ProcessGeckoEvents(void*) (in XUL) (nsAppShell.mm:668)
nsBaseAppShell::NativeEventCallback() (in XUL) (nsBaseAppShell.cpp:88)
NS_ProcessPendingEvents(nsIThread*, unsigned int) (in XUL) (nsThreadUtils.cpp:436)
nsThread::ProcessNextEvent(bool, bool*) (in XUL) (StaticPtr.h:45)
mozilla::detail::RunnableFunction<mozilla::TaskController::TaskController()::$_0>::Run() (in XUL) (TaskController.cpp:692)
mozilla::TaskController::ExecuteNextTaskOnlyMainThreadInternal(mozilla::detail::BaseAutoLock<mozilla::Mutex&> const&) (in XUL) (TaskController.cpp:0)
mozilla::TaskController::DoExecuteNextTaskOnlyMainThreadInternal(mozilla::detail::BaseAutoLock<mozilla::Mutex&> const&) (in XUL) (TaskController.cpp:243)
mozilla::RunnableTask::Run() (in XUL) (RefPtr.h:65)
mozilla::ipc::MessageChannel::MessageTask::Run() (in XUL) (Mutex.h:81)
mozilla::ipc::MessageChannel::RunMessage(mozilla::ipc::ActorLifecycleProxy*, mozilla::ipc::MessageChannel::MessageTask&) (in XUL) (unique_ptr.h:289)
mozilla::ipc::MessageChannel::DispatchMessage(mozilla::ipc::ActorLifecycleProxy*, std::__1::unique_ptr<IPC::Message, std::__1::default_delete<IPC::Message> >) (in XUL) (MessageChannel.h:651)
mozilla::ipc::MessageChannel::DispatchAsyncMessage(mozilla::ipc::ActorLifecycleProxy*, IPC::Message const&) (in XUL) (MessageChannel.h:651)
mozilla::dom::PContentParent::OnMessageReceived(IPC::Message const&) (in XUL) (PContentParent.cpp:13344)
mozilla::dom::ContentParent::RecvHistoryCommit(mozilla::dom::MaybeDiscarded<mozilla::dom::BrowsingContext> const&, unsigned long long const&, nsID const&, unsigned int const&, bool const&, bool const&, unsigned int const&) (in XUL) (ContentParent.cpp:0)
mozilla::dom::CanonicalBrowsingContext::SessionHistoryCommit(unsigned long long, nsID const&, unsigned int, bool, bool, unsigned int) (in XUL) (CanonicalBrowsingContext.cpp:1411)
mozilla::dom::CanonicalBrowsingContext::HistoryCommitIndexAndLength(nsID const&, CallerWillNotifyHistoryIndexAndLengthChanges const&) (in XUL) (function.h:241)
mozilla::dom::BrowsingContext::PreOrderWalkVoid(std::__1::function<void (mozilla::dom::BrowsingContext*)> const&) (in XUL) (nsTArray.h:2914)
std::__1::__function::__func<mozilla::dom::CanonicalBrowsingContext::HistoryCommitIndexAndLength(nsID const&, CallerWillNotifyHistoryIndexAndLengthChanges const&)::$_0, void (mozilla::dom::BrowsingContext*)>::operator()(mozilla::dom::BrowsingContext*&&) (in XUL) (function.h:241)
nsSHistory::WalkContiguousEntriesInOrder(mozilla::dom::SessionHistoryEntry*, std::__1::function<bool (mozilla::dom::SessionHistoryEntry*)> const&) (in XUL) (nsSHistory.cpp:650)
nsScriptSecurityManager::CheckSameOriginURI(nsIURI*, nsIURI*, bool, bool) (in XUL) (nsScriptSecurityManager.cpp:219)
NS_SecurityCompareURIs(nsIURI*, nsIURI*, bool) (in XUL) (RefPtr.h:49)
nsCOMPtr<nsIStandardURL>::nsCOMPtr<nsIURI>(nsQueryInterface<nsIURI>) (in XUL) (nsCOMPtr.h:823)
non-virtual thunk to mozilla::net::nsStandardURL::AddRef() (in XUL) (nsStandardURL.cpp:0)
nsSHistory::WalkContiguousEntriesInOrder(mozilla::dom::SessionHistoryEntry*, std::__1::function<bool (mozilla::dom::SessionHistoryEntry*)> const&) (in XUL) (SessionHistoryEntry.cpp:1467)
NS_IsAboutBlankAllowQueryAndFragment(nsIURI*) (in XUL) (nsNetUtil.cpp:2799)
mozilla::net::nsStandardURL::SchemeIs(char const*, bool*) (in XUL) (nsStandardURL.cpp:0)
NS_IsAboutBlankAllowQueryAndFragment(nsIURI*) (in XUL) (nsNetUtil.cpp:2797)
nsSHistory::WalkContiguousEntriesInOrder(mozilla::dom::SessionHistoryEntry*, std::__1::function<bool (mozilla::dom::SessionHistoryEntry*)> const&) (in XUL) (nsSHistory.cpp:2715)
mozilla::net::nsStandardURL::Release() (in XUL) (Atomics.h:563)
nsSHistory::WalkContiguousEntriesInOrder(mozilla::dom::SessionHistoryEntry*, std::__1::function<bool (mozilla::dom::SessionHistoryEntry*)> const&) (in XUL) (nsSHistory.cpp:2713)
nsSHistory::FindAdjacentEntryFor(mozilla::dom::SessionHistoryEntry*, nsSHistory::SearchDirection) (in XUL) (RefPtr.h:48)
nsSHistory::WalkContiguousEntriesInOrder(mozilla::dom::SessionHistoryEntry*, std::__1::function<bool (mozilla::dom::SessionHistoryEntry*)> const&) (in XUL) (RefPtr.h:49)
NS_SecurityCompareURIs(nsIURI*, nsIURI*, bool) (in XUL) (nsNetUtil.cpp:2564)
NS_GetRealPort(nsIURI*) (in XUL) (nsNetUtil.cpp:0)
mozilla::net::nsStandardURL::GetScheme(nsTSubstring<char>&) (in XUL) (nsTSubstring.h:1321)
nsTSubstring<char>::Assign(nsTSubstring<char>&&) (in XUL) (nsTSubstring.cpp:490)
nsTSubstring<char>::Assign(nsTSubstring<char>&&, std::nothrow_t const&) (in XUL) (nsTSubstring.cpp:534)
nsSHistory::FindAdjacentEntryFor(mozilla::dom::SessionHistoryEntry*, nsSHistory::SearchDirection) (in XUL) (nsSHistory.cpp:2639)
NS_SecurityCompareURIs(nsIURI*, nsIURI*, bool) (in XUL) (nsNetUtil.cpp:2549)
mozilla::net::nsStandardURL::GetAsciiHost(nsTSubstring<char>&) (in XUL) (nsTSubstring.h:1321)
nsTSubstring<char>::Assign(nsTSubstring<char>&&, std::nothrow_t const&) (in XUL) (nsTSubstring.cpp:529)
nsTSubstring<char>::Assign(char const*, unsigned long, std::nothrow_t const&) (in XUL) (Result.h:137)
nsTSubstring<char>::StartBulkWriteImpl(unsigned long, unsigned long, bool, unsigned long, unsigned long, unsigned long) (in XUL) (nsCharTraits.h:0)
NS_SecurityCompareURIs(nsIURI*, nsIURI*, bool) (in XUL) (nsNetUtil.cpp:2564)
mozilla::net::nsStandardURL::GetScheme(nsTSubstring<char>&) (in XUL) (nsTStringRepr.h:475)
i hope you find this satisfactory?
| Reporter | ||
Comment 11•13 days ago
|
||
oh you already fixed it.
this one feels special, not gonna lie. fresh change, saw it with my eyes.
just getting validation everywhere i go.
payin' tribute to the OG Davy with these pupes
| Assignee | ||
Comment 12•13 days ago
|
||
Yes, thanks!
| Reporter | ||
Comment 13•13 days ago
|
||
no vincent, thank you for being an excellent developer by not blaming the unsupported platform, immediately using your skills and time to investigate, and solving the problem without waiting for my symbolication.
good man, good man.
| Reporter | ||
Comment 14•11 days ago
|
||
is this scheduled to land soon? i see harbormaster reported a failure and am wondering if that's part of the reason for it not being pushed to central
| Assignee | ||
Comment 15•9 days ago
|
||
The failure isn't a problem, it's waiting on code review.
Comment 17•8 days ago
|
||
Comment 18•7 days ago
|
||
| bugherder | ||
Description
•