From d0c4e1882b099e4a1db2cd023e36dfdc14cd8144 Mon Sep 17 00:00:00 2001 From: Alexandre de Souza Date: Mon, 9 Oct 2023 15:02:02 -0300 Subject: [PATCH 1/4] Reduce the sleeping time --- lib/livebook/hubs.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/livebook/hubs.ex b/lib/livebook/hubs.ex index d889f64d649a..4ab4ed7c6f15 100644 --- a/lib/livebook/hubs.ex +++ b/lib/livebook/hubs.ex @@ -129,7 +129,7 @@ defmodule Livebook.Hubs do defp disconnect_hub(hub) do Task.Supervisor.start_child(Livebook.TaskSupervisor, fn -> - Process.sleep(30_000) + Process.sleep(10_000) :ok = Provider.disconnect(hub) end) From 190a72159624bdf67401c0ca678a75464c9fe3f0 Mon Sep 17 00:00:00 2001 From: Alexandre de Souza Date: Mon, 9 Oct 2023 17:00:35 -0300 Subject: [PATCH 2/4] Use smallest sleeping time --- lib/livebook/hubs.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/livebook/hubs.ex b/lib/livebook/hubs.ex index 4ab4ed7c6f15..04d470120a07 100644 --- a/lib/livebook/hubs.ex +++ b/lib/livebook/hubs.ex @@ -129,7 +129,7 @@ defmodule Livebook.Hubs do defp disconnect_hub(hub) do Task.Supervisor.start_child(Livebook.TaskSupervisor, fn -> - Process.sleep(10_000) + Process.sleep(5_500) :ok = Provider.disconnect(hub) end) From 072189eec641e5208cacfd34ecb82e1704453936 Mon Sep 17 00:00:00 2001 From: Alexandre de Souza Date: Tue, 10 Oct 2023 17:32:17 -0300 Subject: [PATCH 3/4] Apply review comments --- lib/livebook/hubs.ex | 9 ++++++++- lib/livebook/teams/connection.ex | 6 +----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/livebook/hubs.ex b/lib/livebook/hubs.ex index 04d470120a07..abdd7d310ec6 100644 --- a/lib/livebook/hubs.ex +++ b/lib/livebook/hubs.ex @@ -128,8 +128,15 @@ 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 -> - Process.sleep(5_500) + # 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(5_000) :ok = Provider.disconnect(hub) end) diff --git a/lib/livebook/teams/connection.ex b/lib/livebook/teams/connection.ex index 6df82cf9bb95..f9b581ab935e 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} -> From b19c4e78a3f55f28e42178b2cdf063d144ecf99d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 10 Oct 2023 23:34:11 +0200 Subject: [PATCH 4/4] Update lib/livebook/hubs.ex --- lib/livebook/hubs.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/livebook/hubs.ex b/lib/livebook/hubs.ex index abdd7d310ec6..0b45e649c5ad 100644 --- a/lib/livebook/hubs.ex +++ b/lib/livebook/hubs.ex @@ -136,7 +136,7 @@ defmodule Livebook.Hubs do # 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(5_000) + Process.sleep(30_000) :ok = Provider.disconnect(hub) end)