Skip to content

Commit

Permalink
run endpoint config warmup before starting socket (#5985)
Browse files Browse the repository at this point in the history
There was a slight time window in between starting the socket listener
and actually setting the config in persistent_term that could rely in a
crash on incoming requests. This commit fixes this by calling the warmup
function as a child in the endpoint supervisor, after the config is started,
but before the socket listener is started.

Fixes #5981.
  • Loading branch information
SteffenDE authored Nov 27, 2024
1 parent d34efa8 commit 092605f
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/phoenix/endpoint/supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ defmodule Phoenix.Endpoint.Supervisor do
with {:ok, pid} = ok <- Supervisor.start_link(__MODULE__, {otp_app, mod, opts}, name: mod) do
# We don't use the defaults in the checks below
conf = Keyword.merge(Application.get_env(otp_app, mod, []), opts)
warmup(mod)
log_access_url(mod, conf)
browser_open(mod, conf)

Expand Down Expand Up @@ -83,6 +82,7 @@ defmodule Phoenix.Endpoint.Supervisor do

children =
config_children(mod, secret_conf, default_conf) ++
warmup_children(mod) ++
pubsub_children(mod, conf) ++
socket_children(mod, conf, :child_spec) ++
server_children(mod, conf, server?) ++
Expand Down Expand Up @@ -156,6 +156,10 @@ defmodule Phoenix.Endpoint.Supervisor do
[{Phoenix.Config, args}]
end

defp warmup_children(mod) do
[%{id: :warmup, start: {__MODULE__, :warmup, [mod]}}]
end

defp server_children(mod, config, server?) do
cond do
server? ->
Expand Down Expand Up @@ -346,6 +350,11 @@ defmodule Phoenix.Endpoint.Supervisor do
rescue
e -> Logger.error("Could not warm up static assets: #{Exception.message(e)}")
end

# To prevent a race condition where the socket listener is already started
# but the config not warmed up, we run warmup/1 as a child in the supervision
# tree. As we don't actually want to start a process, we return :ignore here.
:ignore
end

defp warmup_persistent(endpoint) do
Expand Down

0 comments on commit 092605f

Please sign in to comment.