Make websockets work with bfcache
Categories
(Core :: Networking: WebSockets, defect, P2)
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
- /websockets/back-forward-cache-with-open-websocket-connection-and-close-it-in-pagehide.window.html [wpt.fyi]
- Testing BFCache support for a page with an open WebSocket connection, but close it in pagehide.:
PRECONDITION_FAILED(Chrome:PASS, Safari:FAIL)
- Testing BFCache support for a page with an open WebSocket connection, but close it in pagehide.:
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.
Updated•10 months ago
|
Comment 1•2 months ago
|
||
| bug-fix | ||
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:
-
WebSockets block bfcache only via the generic loadgroup
BFCacheStatus::REQUESTbit.WebSocketImplis a plainnsIRequest(notnsIChannel/nsIIdentChannel) added to the document loadgroup inWebSocket.cpp:1919-1927, sonsDocShell::RecordSingleChannelIdsetsmSingleChannelId = Some(0)andCanonicalBrowsingContext::AllowedInBFCacheORs inREQUEST(CanonicalBrowsingContext.cpp:3461-3467). -
nsDocShell::InternalLoadcallsDocument::CanSavePresentationand on failureDisallowBFCaching()(nsDocShell.cpp:8883-8895) at navigation start — before pagehide. That latchesmBFCacheDisallowed, so the document can never become eligible again for this navigation. -
AllowedInBFCacheis evaluated in the parent at process-switch time (ProcessIsolation.cpp:833) and the frameloader is stashed synchronously (nsFrameLoaderOwner.cpp:148);pagehideonly fires afterwards, from the asyncSendDeactivateDocuments(BrowsingContext.cpp:1291). Nothing re-validates after pagehide — cf. the existing// XXXBFCache check again that the page can enter bfcache.atnsDocShell.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
BFCacheStatusbit for WebSockets (likeACTIVE_WEBTRANSPORT) so they stop collapsing into the genericREQUESTbit, 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. NoteDocument::RemoveFromBFCacheSync()is currently a no-op inside a pagehide handler: it is gated onBrowsingContext::mIsInBFCache, which is not set until the secondPreOrderWalk(BrowsingContext.cpp:1303), after pagehide fired in the first (:1288).mIsEnteringBFCachecovers 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
CLOSINGrather 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 | ||
Updated•2 months ago
|
| Assignee | ||
Comment 2•18 days ago
|
||
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.
Comment 3•14 days ago
|
||
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).
Description
•