Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Endpoint: race condition with warmup/1 #5981

Open
paulswartz opened this issue Nov 22, 2024 · 0 comments · May be fixed by #5985
Open

Endpoint: race condition with warmup/1 #5981

paulswartz opened this issue Nov 22, 2024 · 0 comments · May be fixed by #5985

Comments

@paulswartz
Copy link

Environment

  • Elixir version (elixir -v): 1.17.3
  • Phoenix version (mix deps): 1.7.14
  • Operating system: Debian bookworm

Actual behavior

It appears that there's a race condition when starting the endpoint:

8ac841655ae8 01:33:12.681 [info] Running RideAlongWeb.Endpoint with Bandit 1.6.0 at :::4000 (http)
8ac841655ae8 01:33:12.685 [error] ** (RuntimeError) could not find persistent term for endpoint RideAlongWeb.Endpoint. Make sure your endpoint is started and note you cannot access endpoint functions at compile-time
8ac841655ae8     (ride_along 0.1.0) deps/phoenix/lib/phoenix/endpoint.ex:544: RideAlongWeb.Endpoint.persistent!/0
8ac841655ae8     (ride_along 0.1.0) deps/phoenix/lib/phoenix/endpoint.ex:584: RideAlongWeb.Endpoint.script_name/0
8ac841655ae8     (ride_along 0.1.0) lib/ride_along_web/endpoint.ex:1: RideAlongWeb.Endpoint.call/2
8ac841655ae8     (bandit 1.6.0) lib/bandit/pipeline.ex:127: Bandit.Pipeline.call_plug!/2
8ac841655ae8     (bandit 1.6.0) lib/bandit/pipeline.ex:36: Bandit.Pipeline.run/4
8ac841655ae8     (bandit 1.6.0) lib/bandit/http1/handler.ex:12: Bandit.HTTP1.Handler.handle_data/3
8ac841655ae8     (bandit 1.6.0) lib/bandit/delegating_handler.ex:18: Bandit.DelegatingHandler.handle_data/3
8ac841655ae8     (bandit 1.6.0) /app/deps/thousand_island/lib/thousand_island/handler.ex:417: Bandit.DelegatingHandler.handle_continue/2
8ac841655ae8 01:33:12.685 [info] Access RideAlongWeb.Endpoint at XXX

I think this is the relevant code in Phoenix.Endpoint.Supervisor:

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)

The call to warmup/1 (which sets the persistent term) happens after the Endpoint is started, so there's a small window where the HTTP server is accepting connections, but the underlying data has not been set up.

Expected behavior

All the underlying data is available when the server is accepting connections.

paulswartz added a commit to paulswartz/phoenix that referenced this issue Nov 22, 2024
SteffenDE added a commit that referenced this issue Nov 26, 2024
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.
@SteffenDE SteffenDE linked a pull request Nov 26, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant