LiveView Crashes with Undefined Message in Production Environment
Environment
- Elixir version (elixir -v): Elixir 1.11.3 (compiled with Erlang/OTP 21)
- Phoenix version (mix deps): 1.5.9
- Phoenix LiveView version (mix deps): 0.15.7
- NodeJS version (node -v): 14.7.0
- NPM version (npm -v): 6.14.7
- Operating system: Alpine
- Browsers you attempted to reproduce this bug on (the more the merrier): Chrome, Safari
- Does the problem persist after removing "assets/node_modules" and trying again? Yes/no: Yes
Actual behavior
I have an application with two endpoints and two separate ports. One for my main application and another for a side application. Each is accepting connections, however, I use the side application for a live_view form. On the development environment I have no issues when working with the form, submitting the form etc. On the prod environment, however, the application keeps refreshing back to the main form page after 20 seconds or so when idle, keep in mind the form created has multiple stages. There is no erroneous output in the elixir logs. The liveSocket debug output is the following:
It keeps crashing every 20 seconds continuously. I'm not sure how to further debug this, tried multiple things. For additional context, the live_view form has two file uploads, and multiple stages through which the file_uploads are maintained or consumed.
Additionally, the application is packaged using mix releases and dockerized.
Expected behavior
Expected form to stay on the current stage without crashing after every 20 seconds in the production environment.
I figured it might be a proxying issue or a firewall affecting the websocket. So I tried using longPolling. Getting a Jason decode issue with longpolling and uploading files using liveview.
[error] GenServer #PID<0.2517.0> terminating
** (Jason.DecodeError) unexpected byte at position 0: 0x0
(jason 1.2.2) lib/jason.ex:78: Jason.decode!/2
(phoenix 1.5.9) lib/phoenix/socket/serializers/v2_json_serializer.ex:112: Phoenix.Socket.V2.JSONSerializer.decode_text/1
(phoenix 1.5.9) lib/phoenix/socket.ex:466: Phoenix.Socket.__in__/2
(phoenix 1.5.9) lib/phoenix/transports/long_poll_server.ex:47: Phoenix.Transports.LongPoll.Server.handle_info/2
(stdlib 3.14) gen_server.erl:689: :gen_server.try_dispatch/4
(stdlib 3.14) gen_server.erl:765: :gen_server.handle_msg/6
(stdlib 3.14) proc_lib.erl:226: :proc_lib.init_p_do_apply/3
Last message: {:dispatch, #PID<0.2548.0>, <<0, 2, 2, 5, 5, 49, 49, 49, 50, 108, 118, 117, 58, 48, 99, 104, 117, 110, 107, 80, 75, 3, 4, 20, 0, 6, 0, 8, 0, 0, 0, 33, 0, 10, 181, 94, 151, 173, 1, 0, 0, 67, 8, 0, 0, 19, 0, ...>>, #Reference<0.2151097032.2965372930.81648>}
State: %{buffer: [], client_ref: {#PID<0.2546.0>, #Reference<0.2151097032.2965372930.81643>}, handler: {Phoenix.LiveView.Socket, {%{channels: %{"lv:phx-Fo_3wX4TqYj-TkDH" => {#PID<0.2522.0>, #Reference<0.2151097032.2965372930.80713>}, "lvu:0" => {#PID<0.2544.0>, #Reference<0.2151097032.2965372937.78959>}}, channels_inverse: %{#PID<0.2522.0> => {"lv:phx-Fo_3wX4TqYj-TkDH", "4"}, #PID<0.2544.0> => {"lvu:0", "11"}}}, %Phoenix.Socket{assigns: %{}, channel: nil, channel_pid: nil, endpoint: IrisWeb.Endpoints.MerchEndpoint, handler: Phoenix.LiveView.Socket, id: nil, join_ref: nil, joined: false, private: %{connect_info: %{}}, pubsub_server: Iris.PubSub, ref: nil, serializer: Phoenix.Socket.V2.JSONSerializer, topic: nil, transport: :longpoll, transport_pid: #PID<0.2517.0>}}}, last_client_poll: 1625790411575, priv_topic: "phx:lp:lFvvoe9bebGt+c9yKUUwwg==1625790401703", pubsub_server: Iris.PubSub, window_ms: 15000}
I ruled out a proxy issue or a firewall issue because I have other applications with live_view deployed in the same way and they are working as expected.
Does it happens when you are doing anything or do you trigger any action and then it fails? To clarify, I see there are some updates, are they automatic or do you trigger certain actions first? If you have a simple LiveView in the same app, does it work?
There is no specific action or trigger causing it to fail. I added a simple LiveView and it still crashed.
Unfortunately there isn’t much we can do if we can’t reproduce the issue. :( I would try comparing with the other apps where LiveView works and see the differences
I'm checking with the versioning, because that's the main difference. However, the uploads functionality that I'm using is just in the new live view versions. The other phoenix live view apps are using older versions of phoenix that are within the 1.4 major and minor versions and live view with earlier versions. Upload functionality was added to phoenix 1.5 and live view 0.15, if I'm not mistaken.
I figured it might be a proxying issue or a firewall affecting the websocket. So I tried using longPolling. Getting a Jason decode issue with longpolling and uploading files using liveview.
[error] GenServer #PID<0.2517.0> terminating ** (Jason.DecodeError) unexpected byte at position 0: 0x0 (jason 1.2.2) lib/jason.ex:78: Jason.decode!/2 (phoenix 1.5.9) lib/phoenix/socket/serializers/v2_json_serializer.ex:112: Phoenix.Socket.V2.JSONSerializer.decode_text/1 (phoenix 1.5.9) lib/phoenix/socket.ex:466: Phoenix.Socket.__in__/2 (phoenix 1.5.9) lib/phoenix/transports/long_poll_server.ex:47: Phoenix.Transports.LongPoll.Server.handle_info/2 (stdlib 3.14) gen_server.erl:689: :gen_server.try_dispatch/4 (stdlib 3.14) gen_server.erl:765: :gen_server.handle_msg/6 (stdlib 3.14) proc_lib.erl:226: :proc_lib.init_p_do_apply/3 Last message: {:dispatch, #PID<0.2548.0>, <<0, 2, 2, 5, 5, 49, 49, 49, 50, 108, 118, 117, 58, 48, 99, 104, 117, 110, 107, 80, 75, 3, 4, 20, 0, 6, 0, 8, 0, 0, 0, 33, 0, 10, 181, 94, 151, 173, 1, 0, 0, 67, 8, 0, 0, 19, 0, ...>>, #Reference<0.2151097032.2965372930.81648>} State: %{buffer: [], client_ref: {#PID<0.2546.0>, #Reference<0.2151097032.2965372930.81643>}, handler: {Phoenix.LiveView.Socket, {%{channels: %{"lv:phx-Fo_3wX4TqYj-TkDH" => {#PID<0.2522.0>, #Reference<0.2151097032.2965372930.80713>}, "lvu:0" => {#PID<0.2544.0>, #Reference<0.2151097032.2965372937.78959>}}, channels_inverse: %{#PID<0.2522.0> => {"lv:phx-Fo_3wX4TqYj-TkDH", "4"}, #PID<0.2544.0> => {"lvu:0", "11"}}}, %Phoenix.Socket{assigns: %{}, channel: nil, channel_pid: nil, endpoint: IrisWeb.Endpoints.MerchEndpoint, handler: Phoenix.LiveView.Socket, id: nil, join_ref: nil, joined: false, private: %{connect_info: %{}}, pubsub_server: Iris.PubSub, ref: nil, serializer: Phoenix.Socket.V2.JSONSerializer, topic: nil, transport: :longpoll, transport_pid: #PID<0.2517.0>}}}, last_client_poll: 1625790411575, priv_topic: "phx:lp:lFvvoe9bebGt+c9yKUUwwg==1625790401703", pubsub_server: Iris.PubSub, window_ms: 15000}
Just a note on this, the default uploader won't work with long polling, the binary protocol used for uploading files only works is only supported on Web Sockets. Long polling uses a text based protocol, this explains your JSON decoding issue, but not the original issue you reported.
I will close this for now. Unless we can reproduce it, there is not much we can do to help. If you have more information, then let us know and we will be glad to reopen.