hubertlepicki

hubertlepicki

Phoenix Sockets getting stuck at "closed", "connecting" or "disconnecting" state

I am using Phoenix with Apollo and @absinthe/socket-apollo-link to listen to GraphQL subscriptions. The users spend a lot of time on the app with tab open, without refreshing the page, often they have the app open for weeks.

Disconnects happen during that time, for various reasons. When we deploy new version is one case, but users’ devices go to sleep, they disconnect from wifi, or their network is having issues.

We have been getting reports, since about updating to Phoenix 1.6.4 on the page being “stuck”. I managed to reproduce it by just leaving the page open for 24 hours too, several WebSocket disconnection happened and, when I went to my computer the phoenixSocket.isConnected() was false and phoenixSocket.connectionStatus() was connecting.

Since then, I was able to reproduce the issue, fairly reliably by doing the following, with two hosts on my local network.

  1. I launch the app on Host A, on port 4000.
  2. From Host B, I open the SSH connection and port forwarding to Host A. So that port 4000 on Host B is forwarding TCP connection to Host B on port 4000.
  3. On Host B, I load the page over at localhost:4000. This goes to local forwarded port, hits Host A, renders page, connections establish properly.
  4. I disconnect wifi on Host B for 15 seconds or so. This is short enough so that SSH session doesn’t block, but the Phoenix socket disconnects.
  5. I turn on the wifi on Host B.
  6. I can see the page in Chrome on Host B being stuck. Connection is usually “closed”, but calling photnixSocket.connect() has no effect.

I can re-set the connection by first calling disconnect() and then immediately connect().

I suspect this may be an issue in Phoenix.js or somewhere in my code, but I wonder if someone else had similar issues?

Most Liked

hubertlepicki

hubertlepicki

Hey Chris, no, Chrome 96.

I suspect we had a websocket issue on production that since was resolved, and at the same time we did have the LongPoll fallback misconfigured. I bumped down the heartbeat from 30s to 5s, enabled LongPoll and I also monitor the socket this way every 5s:

function checkSocketWorks(phoenixSocket) {
  if (phoenixSocket.isConnected() == false) {
    connDropped = true;
    /* Phoenix.js seems to hang in 'disconnected', 'connecting', or 'disconnecting' states
     * when the network is unreliable but not totally down. In such case isConnected()
     * correctly returns false, but you cannot reconnect the connection before dropping it
     * manually, something disconnec() does */
    phoenixSocket.disconnect();
    phoenixSocket.connect();
  }
}

The above changes seem to have fixed it for us.

Do you want me to prepare either a video / isolated code example to reproduce the issue? I suspect it may be some race condition in the socket JS implementation that we’re hitting.

Where Next?

Popular in Questions Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54250 245
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

We're in Beta

About us Mission Statement