phoenixframework / phoenix

Peace of mind from prototype to production
https://www.phoenixframework.org
MIT License
21.45k stars 2.88k forks source link

Endpoint: race condition with warmup/1 #5981

Open paulswartz opened 10 hours ago

paulswartz commented 10 hours ago

Environment

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: https://github.com/phoenixframework/phoenix/blob/222d9aba8595f81148d454d60d2072d11a969a09/lib/phoenix/endpoint/supervisor.ex#L13-L16

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.