From 2898ee37f6a66d68a5ca3ca3f4cb45a4d850c5c0 Mon Sep 17 00:00:00 2001 From: Alexandre de Souza Date: Tue, 10 Oct 2023 18:51:38 -0300 Subject: [PATCH] Don't reconnect when WebSocket returns server error (#2264) --- lib/livebook/hubs.ex | 7 +++++++ lib/livebook/teams/connection.ex | 6 +----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/livebook/hubs.ex b/lib/livebook/hubs.ex index d889f64d6..0b45e649c 100644 --- a/lib/livebook/hubs.ex +++ b/lib/livebook/hubs.ex @@ -128,7 +128,14 @@ defmodule Livebook.Hubs do end defp disconnect_hub(hub) do + # We use a task supervisor because the hub connection itself + # calls delete_hub (which calls this function), otherwise we deadlock. Task.Supervisor.start_child(Livebook.TaskSupervisor, fn -> + # Since other processes may have been communicating + # with the hub, we don't want to terminate abruptly and + # make them crash, so we give it some time to shut down. + # + # The default backoff is 5.5s, so we round it down to 5s. Process.sleep(30_000) :ok = Provider.disconnect(hub) end) diff --git a/lib/livebook/teams/connection.ex b/lib/livebook/teams/connection.ex index 6df82cf9b..f9b581ab9 100644 --- a/lib/livebook/teams/connection.ex +++ b/lib/livebook/teams/connection.ex @@ -52,7 +52,7 @@ defmodule Livebook.Teams.Connection do reason = LivebookProto.Error.decode(error).details send(data.listener, {:server_error, reason}) - {:keep_state_and_data, {{:timeout, :reconnect}, @backoff, nil}} + {:keep_state, data} end end @@ -60,10 +60,6 @@ defmodule Livebook.Teams.Connection do {:keep_state_and_data, {:next_event, :internal, :connect}} end - def handle_event({:timeout, :reconnect}, nil, _state, _data) do - {:keep_state_and_data, {:next_event, :internal, :connect}} - end - def handle_event(:info, {:loop_ping, ref}, @no_state, %__MODULE__{ref: ref} = data) do case WebSocket.send(data.http_conn, data.websocket, data.ref, :ping) do {:ok, conn, websocket} ->