Closed Bug 2092362 Opened 14 days ago Closed 7 days ago

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)

Firefox 160
All
macOS
defect

Tracking

()

RESOLVED FIXED
163 Branch
Tracking Status
firefox163 --- fixed

People

(Reporter: marlon_rando, Assigned: vhilla)

References

Details

Attachments

(2 files, 1 external file, 1 obsolete file)

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):

  1. Suppose mEntries contains duplicate top-level entries separated by another entry: [Top0, Top1, Top0], where Top0 contains child ChildA and Top1 contains child ChildB (with the same DocshellID(), and ChildA != ChildB).
  2. WalkContiguousEntriesInOrder(ChildA) calls FindLeftmostAdjacentContiguousEntryFor(ChildA, SearchDirection::Right):
    • FindAdjacentEntryFor(ChildA, Right) looks up GetIndexOfEntry(Top0), which returns the first match at index 0.
    • It inspects mEntries[0 + 1] (Top1) and returns ChildB (ChildB != ChildA).
    • Because ChildA->GetID() != ChildB->GetID(), FindLeftmostAdjacentContiguousEntryFor stops at ChildB and returns it to WalkContiguousEntriesInOrder.
  3. On the next iteration of the while loop in WalkContiguousEntriesInOrder (nsSHistory.cpp:649):
    • FindAdjacentEntryFor(ChildB, Right) looks up GetIndexOfEntry(Top1) (index 1).
    • It inspects mEntries[1 + 1] (Top0 at index 2) and returns ChildA (ChildA != ChildB).
    • WalkContiguousEntriesInOrder receives ChildA again 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 (9994f9e54bec on 160 beta/release) changed CanonicalBrowsingContext::SetActiveSessionHistoryEntry so AdoptBFCacheEntry only shares SHEntrySharedParentState when InnerWindowId() matches and !aKeepSharedState, causing adjacent entries of the same document/subframe to have distinct shared-state IDs (aEntry->GetID() != current->GetID()).
  • Bug 2075414 (87bcf6fc6afd) restricted AdoptChildSHEntry to static OriginalFrameSrc() loads, causing dynamically navigated iframes to commit new entries via RecvHistoryCommit.
  • Together, these allow FindLeftmostAdjacentContiguousEntryFor to terminate on ChildB instead of advancing past the duplicate Top0, exposing the 2-step cycle in WalkContiguousEntriesInOrder.

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.

OS: Unspecified → macOS
Hardware: Unspecified → All
Keywords: regression
Regressed by: 2075414
See Also: → 2042897
Attached patch fix-nshistory-or-whatever (obsolete) — — Splinter Review

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

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

  1. 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).
  2. 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.
  3. 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?

Attachment #9682284 - Attachment is obsolete: true

: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.

Flags: needinfo?(vhilla)

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.

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?

Flags: needinfo?(vhilla) → needinfo?(marlon_rando)

Bug 2042897 is older than bug 2075414. Perhaps it surfaced this specific hang, but I don't see evidence for it causing duplicate entries.

Keywords: regression
No longer regressed by: 2075414
Severity: -- → S3

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: nobody → vhilla
Status: UNCONFIRMED → ASSIGNED
Ever confirmed: true

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?

Flags: needinfo?(marlon_rando)

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

Yes, thanks!

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.

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

The failure isn't a problem, it's waiting on code review.

Duplicate of this bug: 2048227
Status: ASSIGNED → RESOLVED
Closed: 7 days ago
Resolution: --- → FIXED
Target Milestone: --- → 163 Branch
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: