Skip to content

Commit 3ba2362

Browse files
josevalimjonatanklosko
authored andcommitted
Fix LIVEBOOK_PORT=0 for iframes
1 parent 8097351 commit 3ba2362

File tree

5 files changed

+32
-56
lines changed

5 files changed

+32
-56
lines changed

lib/livebook/application.ex

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,12 @@ defmodule Livebook.Application do
4747
if serverless?() do
4848
[]
4949
else
50+
{_type, module, key} = Livebook.Config.identity_provider()
51+
5052
iframe_server_specs() ++
51-
identity_provider() ++
5253
[
54+
{module, name: LivebookWeb.ZTA, identity_key: key},
5355
{DNSCluster, query: Application.get_env(:livebook, :dns_cluster_query) || :ignore},
54-
# Start the Endpoint (http/https)
5556
# We skip the access url as we do our own logging below
5657
{LivebookWeb.Endpoint, log_access_url: false}
5758
] ++ app_specs()
@@ -367,47 +368,16 @@ defmodule Livebook.Application do
367368
[
368369
scheme: :http,
369370
plug: LivebookWeb.IframeEndpoint,
370-
port: port
371+
port: port,
372+
thousand_island_options: [supervisor_options: [name: LivebookWeb.IframeEndpoint]]
371373
] ++ Keyword.take(http, [:ip])
372374

373-
spec = Bandit.child_spec(iframe_opts)
374-
spec = update_in(spec.start, &{__MODULE__, :start_iframe, [port, &1]})
375-
[spec]
375+
[{Bandit, iframe_opts}]
376376
else
377377
[]
378378
end
379379
end
380380

381-
@doc false
382-
def start_iframe(port, {m, f, a}) do
383-
case apply(m, f, a) do
384-
{:ok, pid} ->
385-
{:ok, pid}
386-
387-
{:error, {:shutdown, {_, _, {{_, {:error, :eaddrinuse}}, _}}}} ->
388-
iframe_port_in_use(port)
389-
390-
{:error, {:shutdown, {_, _, {:listen_error, _, :eaddrinuse}}}} ->
391-
iframe_port_in_use(port)
392-
393-
{:error, _} = error ->
394-
error
395-
end
396-
end
397-
398-
@dialyzer {:no_return, iframe_port_in_use: 1}
399-
400-
defp iframe_port_in_use(port) do
401-
Livebook.Config.abort!(
402-
"Failed to start Livebook iframe server because port #{port} is already in use"
403-
)
404-
end
405-
406-
defp identity_provider() do
407-
{_type, module, key} = Livebook.Config.identity_provider()
408-
[{module, name: LivebookWeb.ZTA, identity_key: key}]
409-
end
410-
411381
defp serverless?() do
412382
Application.get_env(:livebook, :serverless, false)
413383
end

lib/livebook/utils.ex

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -619,21 +619,6 @@ defmodule Livebook.Utils do
619619
defp memory_unit(:MB), do: 1_000_000
620620
defp memory_unit(:KB), do: 1_000
621621

622-
@doc """
623-
Gets the port for an existing listener.
624-
"""
625-
@spec get_port(module, :http | :https, :inet.port_number()) :: :inet.port_number()
626-
def get_port(endpoint, scheme, default) do
627-
try do
628-
endpoint.server_info(scheme)
629-
rescue
630-
_ -> default
631-
else
632-
{:ok, {_, port}} when is_integer(port) -> port
633-
_ -> default
634-
end
635-
end
636-
637622
@doc """
638623
Converts the given IP address into a valid hostname.
639624

lib/livebook_web/endpoint.ex

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,10 @@ defmodule LivebookWeb.Endpoint do
140140
base =
141141
case struct_url() do
142142
%URI{scheme: "https", port: 0} = uri ->
143-
%{uri | port: Livebook.Utils.get_port(__MODULE__, :https, 433)}
143+
%{uri | port: port(:https, 433)}
144144

145145
%URI{scheme: "http", port: 0} = uri ->
146-
%{uri | port: Livebook.Utils.get_port(__MODULE__, :http, 80)}
146+
%{uri | port: port(:http, 80)}
147147

148148
%URI{} = uri ->
149149
uri
@@ -162,4 +162,15 @@ defmodule LivebookWeb.Endpoint do
162162
def access_url do
163163
URI.to_string(access_struct_url())
164164
end
165+
166+
defp port(scheme, default) do
167+
try do
168+
server_info(scheme)
169+
rescue
170+
_ -> default
171+
else
172+
{:ok, {_, port}} when is_integer(port) -> port
173+
_ -> default
174+
end
175+
end
165176
end

lib/livebook_web/iframe_endpoint.ex

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,18 @@ defmodule LivebookWeb.IframeEndpoint do
3535
iframe_port = Livebook.Config.iframe_port()
3636

3737
case livebook_port do
38-
0 -> Livebook.Utils.get_port(__MODULE__, :http, iframe_port)
39-
_ -> iframe_port
38+
0 ->
39+
try do
40+
ThousandIsland.listener_info(__MODULE__)
41+
rescue
42+
_ -> iframe_port
43+
else
44+
{:ok, {_ip, port}} -> port
45+
_ -> iframe_port
46+
end
47+
48+
_ ->
49+
iframe_port
4050
end
4151
end
4252
end

mix.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"telemetry": {:hex, :telemetry, "1.2.1", "68fdfe8d8f05a8428483a97d7aab2f268aaff24b49e0f599faa091f1d4e7f61c", [:rebar3], [], "hexpm", "dad9ce9d8effc621708f99eac538ef1cbe05d6a874dd741de2e689c47feafed5"},
4848
"telemetry_metrics": {:hex, :telemetry_metrics, "0.6.1", "315d9163a1d4660aedc3fee73f33f1d355dcc76c5c3ab3d59e76e3edf80eef1f", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "7be9e0871c41732c233be71e4be11b96e56177bf15dde64a8ac9ce72ac9834c6"},
4949
"telemetry_poller": {:hex, :telemetry_poller, "1.0.0", "db91bb424e07f2bb6e73926fcafbfcbcb295f0193e0a00e825e589a0a47e8453", [:rebar3], [{:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "b3a24eafd66c3f42da30fc3ca7dda1e9d546c12250a2d60d7b81d264fbec4f6e"},
50-
"thousand_island": {:hex, :thousand_island, "1.1.0", "dcc115650adc61c5e7de12619f0cb94b2b8f050326e7f21ffbf6fdeb3d291e4c", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "7745cf71520d74e119827ff32c2da6307e822cf835bebed3b2c459cc57f32d21"},
50+
"thousand_island": {:hex, :thousand_island, "1.2.0", "4f548ae771ab5f96bc7e199f9824c0c2ce6d365f8c93f5f64dbbb33988e484bf", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "521671fea179672addb6af46455fc2a77be1edda4c0ed351633e0ef37a4b3584"},
5151
"websock": {:hex, :websock, "0.5.1", "c496036ce95bc26d08ba086b2a827b212c67e7cabaa1c06473cd26b40ed8cf10", [:mix], [], "hexpm", "b9f785108b81cd457b06e5f5dabe5f65453d86a99118b2c0a515e1e296dc2d2c"},
5252
"websock_adapter": {:hex, :websock_adapter, "0.5.4", "7af8408e7ed9d56578539594d1ee7d8461e2dd5c3f57b0f2a5352d610ddde757", [:mix], [{:bandit, ">= 0.6.0", [hex: :bandit, repo: "hexpm", optional: true]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.6", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:websock, "~> 0.5", [hex: :websock, repo: "hexpm", optional: false]}], "hexpm", "d2c238c79c52cbe223fcdae22ca0bb5007a735b9e933870e241fce66afb4f4ab"},
5353
}

0 commit comments

Comments
 (0)