Open Bug 2003986 Opened 1 year ago Updated 14 days ago

Make websockets work with bfcache

Categories

(Core :: Networking: WebSockets, defect, P2)

defect
Points:
5

Tracking

()

People

(Reporter: wpt-sync, Assigned: jesup)

References

Details

(Whiteboard: [wpt][necko-triaged][necko-priority-next])

Attachments

(1 external file)

Syncing wpt PR 56457 found new untriaged test failures in CI

Tests Affected

New Tests That Don't Pass

CI Results

Gecko CI (Treeherder)
GitHub PR Head

Notes

These updates will be on mozilla-central once bug 2003811 lands.

Note: this bug is for tracking fixing the issues and is not
owned by the wpt sync bot.

This bug is linked to the relevant tests by an annotation in
https://github.com/web-platform-tests/wpt-metadata. These annotations
can be edited using the wpt interop dashboard
https://jgraham.github.io/wptdash/

If this bug is split into multiple bugs, please also update the
annotations, otherwise we are unable to track which wpt issues are
already triaged. Resolving as duplicate or closing this issue should
be cause the bot to automatically update or remove the annotation.

Severity: -- → S3
Points: --- → 5
Rank: 3
Priority: -- → P2
See Also: → 2009177
Summary: New wpt failures in /websockets/back-forward-cache-with-open-websocket-connection-and-close-it-in-pagehide.window.html → Make websockets work with bfcache
Whiteboard: [wpt] → [wpt][necko-triaged][necko-priority-next]

Automated triage. I could not reproduce/verify this with the tooling available to me (it needs the wpt harness with a WebSocket server plus a cross-document navigation), so no patch — but here is the root cause.

The test closes the WebSocket in pagehide, but by then Gecko's bfcache decision is already final. Three separate things block it:

  1. WebSockets block bfcache only via the generic loadgroup BFCacheStatus::REQUEST bit. WebSocketImpl is a plain nsIRequest (not nsIChannel/nsIIdentChannel) added to the document loadgroup in WebSocket.cpp:1919-1927, so nsDocShell::RecordSingleChannelId sets mSingleChannelId = Some(0) and CanonicalBrowsingContext::AllowedInBFCache ORs in REQUEST (CanonicalBrowsingContext.cpp:3461-3467).

  2. nsDocShell::InternalLoad calls Document::CanSavePresentation and on failure DisallowBFCaching() (nsDocShell.cpp:8883-8895) at navigation start — before pagehide. That latches mBFCacheDisallowed, so the document can never become eligible again for this navigation.

  3. AllowedInBFCache is evaluated in the parent at process-switch time (ProcessIsolation.cpp:833) and the frameloader is stashed synchronously (nsFrameLoaderOwner.cpp:148); pagehide only fires afterwards, from the async SendDeactivateDocuments (BrowsingContext.cpp:1291). Nothing re-validates after pagehide — cf. the existing // XXXBFCache check again that the page can enter bfcache. at nsDocShell.cpp:1365.

So closing the socket in pagehide cannot help today. Simply dropping WebSockets from the blocking set isn't enough either — the page would then be bfcached even when the socket stays open, regressing back-forward-cache-with-open-websocket-connection.window.js.

What a fix appears to need:

  • A dedicated BFCacheStatus bit for WebSockets (like ACTIVE_WEBTRANSPORT) so they stop collapsing into the generic REQUEST bit, treated as deferred rather than blocking at decision time.
  • A post-pagehide re-check that evicts the entry if any WebSocket is still CONNECTING/OPEN. Note Document::RemoveFromBFCacheSync() is currently a no-op inside a pagehide handler: it is gated on BrowsingContext::mIsInBFCache, which is not set until the second PreOrderWalk (BrowsingContext.cpp:1303), after pagehide fired in the first (:1288). mIsEnteringBFCache covers exactly that window but nothing bfcache-related reads it. This gap also affects MessagePort/BroadcastChannel/IndexedDB/WebTransport/LockManager eviction, so it may be worth fixing on its own.
  • The re-check must accept CLOSING rather than wait for the loadgroup request to disappear: close() only removes it after the full closing handshake completes (≥1 network round trip, WebSocket.cpp:689-698), long after the pagehide handler returns.

Related: bug 2009177 is doing the equivalent work for WebTransport.


If you'd like to provide feedback on this comment, please use the 👍 or 👎 reaction.

Assignee: nobody → rjesup

The document loadgroup check blocks BFCache for as long as a WebSocket's
request is in the loadgroup, which lasts until the closing handshake
finishes, so closing it from a pagehide handler still keeps the page out.
Track WebSockets with a dedicated BFCacheStatus::ACTIVE_WEBSOCKET instead,
set while the WebSocket is connecting or open and cleared as soon as it
starts closing, and skip WebSocket requests in the loadgroup check.

I think this should become a patch on top of bug 1896663 instead, behind the pref inside that stack, so that we don't have bifurcation on this when we don't need to have it (and just have websocket+bfcache work with the pref, with the obvious goal of making it default behavior at some point).

See Also: → 1896663
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: