Skip to content

WebSocket does not reconnect on Chrome 149 after switching apps on mobile #6804

Closed

Description

@quintinm-dev

Environment

  • Elixir version (please paste the output of elixir -v):
1.19.4
  • Phoenix and LiveView versions (mix deps | grep -w 'phoenix\|phoenix_live_view'):
* phoenix 1.8.11 (Hex package) (mix)
* phoenix_live_view 1.2.9 (Hex package) (mix)
  • Operating system:
  • Windows
  • MacOS
  • Linux
  • Other (please specify): Android
  • Browsers (including version) you attempted to reproduce this bug on (the more the merrier):
- Chrome 149: ✔️ reproduced
- Chrome 148: ❌ not reproduced
- Firefox: ❌ not reproduced

Actual behavior

  1. Open a LiveView page on Chrome on Android
  2. Swap to a different application
  3. Swap back to Chrome

The LiveView socket never reconnects, continuously showing the phx-client-error flash.

The Chrome logs say:

WebSocket connection to 'wss://example.com/live/websocket?vsn=2.0.0' failed: Page entered Back-Forward Cache

(Refreshing the page or following an internal href/LV patch/LV navigation restores the WS connection.)

Expected behavior

The LiveView socket should reconnect.

Chrome 149 made this change which I assume is the cause https://developer.chrome.com/release-notes/149#disconnect_websockets_on_bfcache_entry

I am not well versed in the JS side of LiveView so I have not fully verified the LLM diagnosis myself yet, but supposedly Phoenix is left with a stale true value for pageHidden that suppresses re-connection, since the bad flow doesn't emit pagehide/pageshow/visibilitychange (visible).

I was unable to reproduce using the bfcache normally (via the back button). Only when swapping apps on mobile.

Activity

  1. changed the title [-]WebSocket dies on Chrome 149 after switching apps on mobile[/-] [+]WebSocket does not reconnect on Chrome 149 after switching apps on mobile[/+] on Aug 15, 2026
  2. SteffenDE commented on Aug 17, 2026

    @SteffenDE
    Member

    Thank you, I will try to investigate

  3. SteffenDE commented on Aug 18, 2026

    @SteffenDE
    Member

    I'm unable to reproduce on either Android Chrome 151 or 149. I visited phoenixframework.org and the WebSocket does properly reconnect when I switch between apps. Can you try if it happens for you on that same page?

    but supposedly Phoenix is left with a stale true value for pageHidden that suppresses re-connection, since the bad flow doesn't emit pagehide/pageshow/visibilitychange (visible)

    If the browser really doesn't emit pageshow / visibilitychange, that would be a browser bug. Those should always be emitted when the page becomes active again.

  4. quintinm-dev commented on Aug 19, 2026

    @quintinm-dev
    Author

    Oh darn sorry.

    I tried phoenixframework.org and it does not reproduce, although I don't get any error flash at all even when I turn off wifi/data.

    Let me try again to see if I can reproduce it with a minimal phoenix project I can share. The app I'm observing the issue on is a PWA so maybe it's related.

  5. brocla commented on Aug 25, 2026

    @brocla

    I can reproduce this reliably on an installed PWA, and I have on-device traces that I think confirm the pageHidden mechanism described above and narrow the trigger.

    One methodological note first, since it may explain the failed reproductions: attaching DevTools prevents Chrome from freezing the page, and freezing appears to be a precondition for the failure. I could only capture this by instrumenting the page over CDP, detaching, reproducing, and then reattaching to read a log persisted to localStorage. With the debugger attached throughout, it never reproduces.

    Environment

    Device Motorola moto g power 5G (2024), Android 15
    Chrome 151.0.7922.171
    App Installed PWA (WebAPK), not a browser tab
    phoenix 1.8.5
    phoenix_live_view 1.1.28

    Chrome 151 rather than 149, so whatever this is, it is not confined to 149.

    Trace of a failing cycle

    Recorded by listeners on document (visibilitychange, freeze, resume) and window (focus, blur, pageshow, pagehide), plus a property watch on socket.pageHidden that captures a stack trace on every write. hiddenFor is seconds since the page went hidden.

    04:42:43.076  visibilitychange  vis=hidden   conn=open              -> pageHidden = true
    04:42:48.614  sock:close        code=1006    pageHidden=true
    04:43:43.096  freeze            vis=hidden   conn=closed  hiddenFor=60
    04:46:35.274  resume            vis=visible  conn=closed  hiddenFor=232   pageHidden STILL true
                  (no visibilitychange, no pageshow, no pagehide)
    04:46:35.310  pageHidden = false                                    <- my own workaround, not framework code
    04:46:35.555  focus             vis=visible  conn=connecting
    04:46:35.854  sock:open
    

    The two events that matter:

    • resume fires. The browser is not silent when the app returns.
    • visibilitychange does not fire, in either the target phase on document or the capture phase on window. Neither does pageshow or pagehide.

    Short background/foreground cycles on the same device behave correctly — visibilitychange fires and the socket recovers on its own:

    04:46:46 hidden -> 04:46:49 visible  (3s)   visibilitychange fired, pageHidden cleared by socket.js
    04:46:54 hidden -> 04:46:55 visible  (1s)   visibilitychange fired
    04:46:59 hidden -> 04:47:00 visible  (1s)   visibilitychange fired
    

    The difference is the freeze. Short cycles are never frozen and never fail; the failing cycle was frozen after 60s hidden. That matches the reports of this only biting after a long absence.

    Terminal state, measured with the app visibly hung on screen

    pageHidden:          true          <-- stale
    document.hidden:     false
    visibilityState:     "visible"
    connectionState:     "closed"
    conn:                null
    connectClock:        2             (unchanged from before backgrounding)
    reconnectTimer.tries: 1
    channels:            ["errored", "closed"], no rejoin scheduled
    

    /health answered in under 400ms throughout, so the server was reachable the whole time.

    tries: 1 shows the reconnect timer fired exactly once and then stopped. connectClock never advancing confirms transportConnect() was never reached. The page is fully alive — setTimeout callbacks run normally — there is simply nothing left scheduled that intends to reconnect. Only killing and relaunching the app clears it.

    Why it is permanent rather than delayed

    this.reconnectTimer = new Timer(() => {
      if(this.pageHidden){
        this.log("Not reconnecting as page is hidden!")
        this.teardown()
        return
      }
      this.teardown(() => this.connect())
    }, this.reconnectAfterMs)

    The early return calls teardown() with no callback, so it neither reconnects nor reschedules itself. teardown() also noops conn.onclose and nulls this.conn, so onConnClose's scheduleTimeout() cannot fire again either. Recovery then rests entirely on the visibilitychange handler clearing pageHidden — and since v1.8.4 that handler is additionally gated on !this.closeWasClean. When the event never arrives, there is no second path.

    I want to be fair to the maintainer position that a browser not emitting visibilitychange is a browser bug — my measurements agree that Chrome is not emitting it. But resume is emitted, in the same cycle, before focus, so a framework-side recovery is available without relying on the event that goes missing.

    Suggestions

    1. Read document.hidden directly when the timer fires, instead of the cached flag. pageHidden is a cache of exactly that value, and reading it at the point of use cannot go stale from a missed event. Smallest possible change, no new listeners:

      if(document.hidden){ ... }
    2. Also clear the flag on resume, which is the Page Lifecycle event for precisely this transition and which I measured firing on the failing cycle.

    3. Reschedule while hidden. Even keeping the early return, calling this.reconnectTimer.scheduleTimeout() before returning would make the state recoverable rather than terminal, at the cost of a cheap timer and no network attempt.

    Happy to run further instrumented cycles on this device if any specific measurement would help — the harness is still set up.

  6. SteffenDE commented on Aug 25, 2026

    @SteffenDE
    Member
  7. SteffenDE commented on Aug 25, 2026

    @SteffenDE
    Member

    Thank you @brocla, that analysis was very helpful!

  8. SteffenDE commented on Aug 25, 2026

    @SteffenDE
    Member

    Fixed in v1.8.13

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions