Skip to content

Websocket connection doesn't reconnect after a device wakes up from sleep #6149

Closed

Description

@saleyn

Environment

  • Elixir version (elixir -v): 1.18.2
  • Phoenix version (mix deps): 1.7.20
  • Phoenix LiveView version (mix deps): 1.0.7
  • Operating system: Arch Linux
  • Browsers you attempted to reproduce this bug on (the more the merrier): Chrome, Brave
  • Does the problem persist after removing "assets/node_modules" and trying again? Yes/no: Yes

Actual behavior

I am seeing the following errors in a mobile and a desktop browser after the devices wake up from short sleep when accessing the LiveView (vsn 1.0.5) server:

Uncaught (in promise) Error: A listener indicated an asynchronous response by returning true, but the message channel closed before a response was receivedUnderstand this errorAI
app-2b0afc197cdceeb5c92611615d031f1a.js?vsn=d:28 WebSocket connection to 'wss://********.com/live/websocket?_csrf_token=MRIhNjQqCyQAACBjFSgxDi8XFXsOBDU9rwDdEAnVgvp1_lBaXyG8ZsFb&_track_static%5B0%5D=https%3A%2F%2F********.com%2Fassets%2Fapp-e7baf904e1e50202d94c8a4610601ef5.css%3Fvsn%3Dd&_track_static%5B1%5D=https%3A%2F%2F********.com%2Fassets%2Fapp-2b0afc197cdceeb5c92611615d031f1a.js%3Fvsn%3Dd&_mounts=2&_mount_attempts=0&_live_referer=https%3A%2F%2F********.com%2Fapp%2Forg%2Fuhost&vsn=2.0.0' failed: 

This is observed on a desktop and mobile phones.

Expected behavior

A Websocket should reconnect when a device wakes up and the browser still stays on a page with a LiveView that had an active socket before the device went to sleep.

Activity

  1. SteffenDE commented on Mar 24, 2025

    @SteffenDE
    Member

    Hey @saleyn,

    I've never seen that error before, but a quick search indicates that this is probably caused by a browser extension (https://stackoverflow.com/questions/72494154/a-listener-indicated-an-asynchronous-response-by-returning-true-but-the-messag). Can you please try in a private browser window - or just in general with extensions disabled?

  2. saleyn commented on Mar 25, 2025

    @saleyn
    ContributorAuthor

    I can confirm that the issue is steadily reproduciable in various browsers (tried Chrome, Brave, Safari), without any extensions, on desktop, Android, iPhone devices.

  3. saleyn commented on Mar 26, 2025

    @saleyn
    ContributorAuthor

    Shouldn't this invocation of new socket transport be trapped in a try/catch block? Maybe this is the culprit that breaks the script and fails to schedule a reconnection timer?

    Image

    Error:

    app-2b0afc197cdceeb5…d031f1a.js?vsn=d:28 WebSocket connection to 'wss://*********.com/live/websocket?_csrf_token=WjwuCHQ-NDN_GSYlTRARAAQFCXg…js%3Fvsn%3Dd&_mounts=1&_mount_attempts=0&_live_referer=undefined&vsn=2.0.0' failed: 
    
  4. SteffenDE commented on Mar 26, 2025

    @SteffenDE
    Member

    That code in Phoenix has always looked like this and seemed to work just fine. Can you maybe try older Phoenix + LiveView versions to see if those make a difference?

  5. saleyn commented on Mar 26, 2025

    @saleyn
    ContributorAuthor

    I'll try. Though is there a reason why that code block above doesn't trap the exception?

  6. SteffenDE commented on Mar 26, 2025

    @SteffenDE
    Member

    I assume it's because the only case according to the spec where it should throw an exception is a SyntaxError: https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/WebSocket#exceptions. Any connection error should be handled by the error event.

  7. saleyn commented on Mar 27, 2025

    @saleyn
    ContributorAuthor

    Same result with LiveView 1.0.5. Is this a Phoenix bug or the LiveView?

    I enabled the socket's debug mode, and here are more details. It looks like the attempt to create a reconnect timer throws an exception, and the new timer is never getting scheduled.

    app-2b0afc197cdceeb5…d031f1a.js?vsn=d:28 WebSocket connection to 'wss://*******.com/live/websocket?_csrf_token=QxEcZBQfJDUNfRJjajcYXj0tMn0…js%3Fvsn%3Dd&_mounts=0&_mount_attempts=0&_live_referer=undefined&vsn=2.0.0' failed:

    0  transportConnect @ app-2b0afc197cdceeb5…d031f1a.js?vsn=d:28
    1  connect @ app-2b0afc197cdceeb5…d031f1a.js?vsn=d:28
    2  (anonymous) @ app-2b0afc197cdceeb5…d031f1a.js?vsn=d:28
    3  (anonymous) @ app-2b0afc197cdceeb5…d031f1a.js?vsn=d:28
    4  waitForSocketClosed @ app-2b0afc197cdceeb5…d031f1a.js?vsn=d:28
    5  (anonymous) @ app-2b0afc197cdceeb5…d031f1a.js?vsn=d:28
    6  waitForBufferDone @ app-2b0afc197cdceeb5…d031f1a.js?vsn=d:28
    7  teardown @ app-2b0afc197cdceeb5…d031f1a.js?vsn=d:28
    8  (anonymous) @ app-2b0afc197cdceeb5…d031f1a.js?vsn=d:28
    9  (anonymous) @ app-2b0afc197cdceeb5…d031f1a.js?vsn=d:27

    Top two frames in the stack that show the origin of the exception:

    Frame0:

            transportConnect() {
                this.connectClock++,
                this.closeWasClean = !1,
                this.conn = new this.transport(this.endPointURL()),   // <-- Exception Here
                this.conn.binaryType = this.binaryType,
                this.conn.timeout = this.longpollerTimeout,
                this.conn.onopen = () => this.onConnOpen(),
                this.conn.onerror = e => this.onConnError(e),
                this.conn.onmessage = e => this.onConnMessage(e),
                this.conn.onclose = e => this.onConnClose(e)
            }

    Frame1:

            connect(e) {
                e && (console && console.log("passing params to connect is deprecated. Instead pass :params to the Socket constructor"),
                this.params = fo(e)),
                !this.conn && (this.longPollFallbackMs && this.transport !== ho ? this.connectWithFallback(ho, this.longPollFallbackMs) : this.transportConnect())    // <-- Here
            }

    Frame2:

                this.reconnectTimer = new Sp( () => {
                    this.teardown( () => this.connect())   // <-- Here
                }

    Frame3:

            teardown(e, r, t) {
                if (!this.conn)
                    return e && e();
                this.waitForBufferDone( () => {
                    this.conn && (r ? this.conn.close(r, t || "") : this.conn.close()),
                    this.waitForSocketClosed( () => {
                        this.conn && (this.conn.onopen = function() {}
                        ,
                        this.conn.onerror = function() {}
                        ,
                        this.conn.onmessage = function() {}
                        ,
                        this.conn.onclose = function() {}
                        ,
                        this.conn = null),
                        e && e()   // <-- Here
                    }
                    )
                }
                )
            }
  8. SteffenDE commented on Mar 28, 2025

    @SteffenDE
    Member

    Same result with LiveView 1.0.5. Is this a Phoenix bug or the LiveView?

    The code responsible for reconnects is in Phoenix: https://github.com/phoenixframework/phoenix/tree/main/assets/js/phoenix

    I tried to reproduce this with a Ubuntu VM, but wasn't able to :(

  9. saleyn commented on Mar 28, 2025

    @saleyn
    ContributorAuthor

    Should I move this issue to the Phoenix repo?

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions