Closed Bug 1944288 Opened 1 year ago Closed 4 months ago

Firefox for Android still actively responds to WebSocket messages when on background

Categories

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

defect

Tracking

()

RESOLVED WORKSFORME

People

(Reporter: saschanaz, Assigned: acreskey)

References

Details

(Whiteboard: [necko-triaged][necko-priority-queue])

(Filing as defect as this affects battery usage. Not sure where to file this, but let me start with necko team)

  1. Set up a local Deno server with the following script (sends websocket message every second):
Deno.serve((req) => {
  if (req.headers.get("upgrade") != "websocket") {
    return new Response(`
      <!DOCTYPE html>
      <div id="log"></div>
      <script>
        const onmessage = () => {
          log.prepend(document.createElement("br"))
          log.prepend(Date.now())
        }
        const setup = () => {
          log.prepend(document.createElement("br"))
          log.prepend(Date.now() + " connecting websocket");
          socket = new WebSocket("/");
          socket.onmessage = onmessage;
          socket.onclose = setup;
          socket.onerror = setup;
        }
        setup();
      </script>
    `, {
      headers: {
        "content-type": "text/html"
      }
    });
  }

  const userAgent = req.headers.get("user-agent")

  const { socket, response } = Deno.upgradeWebSocket(req);

  socket.addEventListener("open", () => {
    console.log(`a client connected! ${userAgent}`);
  });

  const handle = setInterval(() => {
    socket.send("ping");
  }, 1000)

  socket.addEventListener("close", () => {
    console.log(`a client disconnected! ${userAgent}`)
    clearTimeout(handle);
  });

  return response;
});
  1. Access the page on Firefox for Android and Chrome for Android
  2. Either turn off the screen or switch to other apps (e.g. start Slacking 🙂)

Expected: Throttling should happen as in Chrome. The page either:

  1. After some time logs the same timestamp multiple times, which means websocket messages are throttled instead of being processed real time)
  2. The page reloads (not sure why?)

Actual: The log says the processing happens realtime for all messages

Both browsers disconnects from the server if the screen is off for some time. Turning on the screen triggers reconnection only on Firefox.

This probably affects Mastodon and Misskey where new posts are streamed through websocket.

The same throttling doesn't happen on Chrome when switching to other tabs, interesting.

Severity: -- → S3
Priority: -- → P2
Whiteboard: [necko-triaged][necko-priority-new]

Need to find out how often Chrome processes network inputs, and how they do so

Whiteboard: [necko-triaged][necko-priority-new] → [necko-triaged][necko-priority-next]
Whiteboard: [necko-triaged][necko-priority-next] → [necko-triaged][necko-priority-queue]
Assignee: nobody → acreskey

Running the same local Deno server with the latest Fenix nightly on my Samsung A54 this is what I'm:

Firefox connects and we see the log of timestamps.

When I switch apps on the device, the websocket connection is broken within 5-10 seconds:

from the server:

a client disconnected! Mozilla/5.0 (Android 16; Mobile; rv:149.0) Gecko/149.0 Firefox/149.0

This is the same behaviour I see in Chrome:

a client disconnected! Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Mobile Safari/537.36

Locking the screen also has the same effect -> client is disconnected.
In both Fenix 149 and Chrome mobile.

:saschanaz, can you re-run this test and tell me what you're seeing?

Flags: needinfo?(krosylight)

On my older Pixel 6, (Android 12), I'm seeing that the websocket connection to both Fenix 149 and Chrome persists after changing apps.
Constant stream of updates from the server:

[1771433484295] sending ping
[1771433485298] sending ping
[1771433486299] sending ping
[1771433487301] sending ping

I would say that I'm not seeing evidence of throttling in either browser on this device.

edit:
After an extended period, both Fenix and Chrome will ultimately disconnect from the server if the screen is locked.

On Android 16 on Galaxy S22 Ultra I can confirm that Firefox 147/149 now disconnects on switching app.

But something is broken, when it reconnects after switching back, somehow it creates two connections instead of one. Switching back and forth again, it becomes 4. And then 8. This doesn't happen on Chrome.

Flags: needinfo?(krosylight)

(In reply to Kagami Rosylight [:saschanaz] (they/them) from comment #6)

On Android 16 on Galaxy S22 Ultra I can confirm that Firefox 147/149 now disconnects on switching app.

But something is broken, when it reconnects after switching back, somehow it creates two connections instead of one. Switching back and forth again, it becomes 4. And then 8. This doesn't happen on Chrome.

Yes - I see the same difference between Fenix and Chrome when backgrounded against this the test server.

But I believe it's because the test page creates new Websocket connections for both socket.onclose and socket.onerror

        const setup = () => {
          log.prepend(document.createElement("br"))
          log.prepend(Date.now() + " connecting websocket");
          socket = new WebSocket("/");
          socket.onmessage = onmessage;
          socket.onclose = setup;
          socket.onerror = setup;
        }

And Fenix is closing the connection with an error (and a close always follows, which is correct).
Chrome handles this as a close, without an error.

As far as I can tell, Fenix firing both onerror and onclose isn't incorrect per the spec, but please log in a separate bug if you feel otherwise.

I propose we close this bug as 'worksforme'

Flags: needinfo?(krosylight)

Fair. Feel free to open the new bug yourself.

Status: NEW → RESOLVED
Closed: 4 months ago
Flags: needinfo?(krosylight)
Resolution: --- → WORKSFORME

Actually did it myself 😛

You need to log in before you can comment on or make changes to this bug.