The minimal FastAPI example provided in demos does not run when deploying to Heroku. On opening the UI page, it continuously drops connection and attempts to reconnect/reload the page. HTTP traffic in the developer inspector shows numerous 400 and 403 errors. In the Heroku logs, this section at the start provides some insight into what goes wrong.
2023-03-08T01:47:39.801963+00:00 app[web.1]: Invalid session sKcqqGWMs5We2nd4AAAA (further occurrences of this error will be logged with level INFO)
2023-03-08T01:47:39.802089+00:00 app[web.1]: INFO: 47.4.204.57:0 - "GET /socket.io/?client_id=c97149b0-b41b-4039-9236-eff508184235&EIO=4&transport=polling&t=OQ-vnY5&sid=sKcqqGWMs5We2nd4AAAA HTTP/1.1" 400 Bad Request
2023-03-08T01:47:39.819944+00:00 app[web.1]: INFO: 47.4.204.57:0 - "GET /favicon.ico HTTP/1.1" 200 OK
2023-03-08T01:47:39.848485+00:00 app[web.1]: INFO: 47.4.204.57:0 - "POST /socket.io/?client_id=c97149b0-b41b-4039-9236-eff508184235&EIO=4&transport=polling&t=OQ-vnYw&sid=sKcqqGWMs5We2nd4AAAA HTTP/1.1" 400 Bad Request
2023-03-08T01:47:39.246193+00:00 heroku[router]: at=info method=GET path="/_nicegui/components/audio" host=redistests.herokuapp.com request_id=2bd42433-7cea-482e-a321-7ef842a1070e fwd="47.4.204.57" dyno=web.1 connect=0ms service=2ms status=200 bytes=547 protocol=https
2023-03-08T01:47:39.344598+00:00 heroku[router]: at=info method=GET path="/_nicegui/components/markdown" host=redistests.herokuapp.com request_id=3286ea04-1ea0-44c0-97cc-33f9a1c79acb fwd="47.4.204.57" dyno=web.1 connect=0ms service=2ms status=200 bytes=608 protocol=https
2023-03-08T01:47:39.820923+00:00 heroku[router]: at=info method=GET path="/_nicegui/static/favicon.ico" host=redistests.herokuapp.com request_id=d7af46fe-6178-4051-840c-5467e9f459be fwd="47.4.204.57" dyno=web.1 connect=0ms service=3ms status=200 bytes=4165 protocol=https
2023-03-08T01:47:39.801076+00:00 heroku[router]: at=info method=GET path="/ws/socket.io/?client_id=c97149b0-b41b-4039-9236-eff508184235&EIO=4&transport=polling&t=OQ-vnY5&sid=sKcqqGWMs5We2nd4AAAA" host=redistests.herokuapp.com request_id=2d09cdda-1287-4ac3-b5c2-3aaf95bff91c fwd="47.4.204.57" dyno=web.1 connect=0ms service=1ms status=400 bytes=244 protocol=https
2023-03-08T01:47:39.847481+00:00 heroku[router]: at=info method=POST path="/ws/socket.io/?client_id=c97149b0-b41b-4039-9236-eff508184235&EIO=4&transport=polling&t=OQ-vnYw&sid=sKcqqGWMs5We2nd4AAAA" host=redistests.herokuapp.com request_id=533db99c-b821-4dde-ac00-7c24a5066c3d fwd="47.4.204.57" dyno=web.1 connect=0ms service=1ms status=400 bytes=307 protocol=https
2023-03-08T01:47:39.797175+00:00 heroku[router]: at=info method=POST path="/ws/socket.io/?client_id=c97149b0-b41b-4039-9236-eff508184235&EIO=4&transport=polling&t=OQ-vnY3&sid=sKcqqGWMs5We2nd4AAAA" host=redistests.herokuapp.com request_id=4ae7bc3f-33a6-426e-80b9-d80c18966f82 fwd="47.4.204.57" dyno=web.1 connect=0ms service=1ms status=200 bytes=261 protocol=https
2023-03-08T01:47:40.495837+00:00 heroku[router]: at=info method=POST path="/ws/socket.io/?client_id=c97149b0-b41b-4039-9236-eff508184235&EIO=4&transport=polling&t=OQ-vnib&sid=FFkJ4Sg4VDW6gwwXAAAC" host=redistests.herokuapp.com request_id=52880af8-a8d7-472d-8275-42474b7bc526 fwd="47.4.204.57" dyno=web.1 connect=4ms service=1ms status=400 bytes=307 protocol=https
2023-03-08T01:47:40.429999+00:00 app[web.1]: INFO: 47.4.204.57:0 - "GET /socket.io/?client_id=c97149b0-b41b-4039-9236-eff508184235&EIO=4&transport=polling&t=OQ-vnhz HTTP/1.1" 200 OK
2023-03-08T01:47:40.496634+00:00 app[web.1]: INFO: 47.4.204.57:0 - "POST /socket.io/?client_id=c97149b0-b41b-4039-9236-eff508184235&EIO=4&transport=polling&t=OQ-vnib&sid=FFkJ4Sg4VDW6gwwXAAAC HTTP/1.1" 400 Bad Request
2023-03-08T01:47:40.532555+00:00 app[web.1]: INFO: 47.4.204.57:0 - "POST /socket.io/?client_id=c97149b0-b41b-4039-9236-eff508184235&EIO=4&transport=polling&t=OQ-vnjZ&sid=FFkJ4Sg4VDW6gwwXAAAC HTTP/1.1" 400 Bad Request
2023-03-08T01:47:40.531598+00:00 heroku[router]: at=info method=POST path="/ws/socket.io/?client_id=c97149b0-b41b-4039-9236-eff508184235&EIO=4&transport=polling&t=OQ-vnjZ&sid=FFkJ4Sg4VDW6gwwXAAAC" host=redistests.herokuapp.com request_id=91afae6d-ef25-4be9-ae51-05b729018b11 fwd="47.4.204.57" dyno=web.1 connect=0ms service=1ms status=400 bytes=307 protocol=https
2023-03-08T01:47:40.428989+00:00 heroku[router]: at=info method=GET path="/ws/socket.io/?client_id=c97149b0-b41b-4039-9236-eff508184235&EIO=4&transport=polling&t=OQ-vnhz" host=redistests.herokuapp.com request_id=8816cfc7-f900-42a6-b5d5-075b51a5deb8 fwd="47.4.204.57" dyno=web.1 connect=0ms service=1ms status=200 bytes=309 protocol=https
2023-03-08T01:47:41.900103+00:00 app[web.1]: INFO: 47.4.204.57:0 - "GET /socket.io/?client_id=c97149b0-b41b-4039-9236-eff508184235&EIO=4&transport=polling&t=OQ-vo2y HTTP/1.1" 200 OK
2023-03-08T01:47:41.941858+00:00 app[web.1]: INFO: 47.4.204.57:0 - "POST /socket.io/?client_id=c97149b0-b41b-4039-9236-eff508184235&EIO=4&transport=polling&t=OQ-vo3b&sid=xJfBSOynX3QpEfXQAAAA HTTP/1.1" 200 OK
2023-03-08T01:47:41.942782+00:00 app[web.1]: INFO: 47.4.204.57:0 - "GET /socket.io/?client_id=c97149b0-b41b-4039-9236-eff508184235&EIO=4&transport=polling&t=OQ-vo3c&sid=xJfBSOynX3QpEfXQAAAA HTTP/1.1" 200 OK
2023-03-08T01:47:41.991965+00:00 app[web.1]: Invalid session xJfBSOynX3QpEfXQAAAA (further occurrences of this error will be logged with level INFO)
2023-03-08T01:47:41.992138+00:00 app[web.1]: INFO: 47.4.204.57:0 - "POST /socket.io/?client_id=c97149b0-b41b-4039-9236-eff508184235&EIO=4&transport=polling&t=OQ-vo4J&sid=xJfBSOynX3QpEfXQAAAA HTTP/1.1" 400 Bad Request
2023-03-08T01:47:42.036446+00:00 app[web.1]: INFO: 47.4.204.57:0 - "POST /socket.io/?client_id=c97149b0-b41b-4039-9236-eff508184235&EIO=4&transport=polling&t=OQ-vo50&sid=xJfBSOynX3QpEfXQAAAA HTTP/1.1" 200 OK
2023-03-08T01:47:42.040276+00:00 app[web.1]: ERROR: Exception in ASGI application
2023-03-08T01:47:42.040277+00:00 app[web.1]: Traceback (most recent call last):
2023-03-08T01:47:42.040277+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.11/site-packages/uvicorn/protocols/websockets/websockets_impl.py", line 238, in run_asgi
2023-03-08T01:47:42.040278+00:00 app[web.1]: result = await self.app(self.scope, self.asgi_receive, self.asgi_send)
2023-03-08T01:47:42.040278+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-03-08T01:47:42.040280+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.11/site-packages/uvicorn/middleware/proxy_headers.py", line 78, in __call__
2023-03-08T01:47:42.040281+00:00 app[web.1]: return await self.app(scope, receive, send)
2023-03-08T01:47:42.040281+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-03-08T01:47:42.040281+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.11/site-packages/fastapi/applications.py", line 271, in __call__
2023-03-08T01:47:42.040282+00:00 app[web.1]: await super().__call__(scope, receive, send)
2023-03-08T01:47:42.040282+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.11/site-packages/starlette/applications.py", line 118, in __call__
2023-03-08T01:47:42.040282+00:00 app[web.1]: await self.middleware_stack(scope, receive, send)
2023-03-08T01:47:42.040283+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.11/site-packages/starlette/middleware/errors.py", line 149, in __call__
2023-03-08T01:47:42.040283+00:00 app[web.1]: await self.app(scope, receive, send)
2023-03-08T01:47:42.040283+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 79, in __call__
2023-03-08T01:47:42.040283+00:00 app[web.1]: raise exc
2023-03-08T01:47:42.040284+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 68, in __call__
2023-03-08T01:47:42.040284+00:00 app[web.1]: await self.app(scope, receive, sender)
2023-03-08T01:47:42.040284+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.11/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in __call__
2023-03-08T01:47:42.040284+00:00 app[web.1]: raise e
2023-03-08T01:47:42.040284+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.11/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in __call__
2023-03-08T01:47:42.040284+00:00 app[web.1]: await self.app(scope, receive, send)
2023-03-08T01:47:42.040285+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.11/site-packages/starlette/routing.py", line 706, in __call__
2023-03-08T01:47:42.040285+00:00 app[web.1]: await route.handle(scope, receive, send)
2023-03-08T01:47:42.040285+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.11/site-packages/starlette/routing.py", line 443, in handle
2023-03-08T01:47:42.040285+00:00 app[web.1]: await self.app(scope, receive, send)
2023-03-08T01:47:42.040285+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.11/site-packages/fastapi/applications.py", line 271, in __call__
2023-03-08T01:47:42.040285+00:00 app[web.1]: await super().__call__(scope, receive, send)
2023-03-08T01:47:42.040286+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.11/site-packages/starlette/applications.py", line 118, in __call__
2023-03-08T01:47:42.040286+00:00 app[web.1]: await self.middleware_stack(scope, receive, send)
2023-03-08T01:47:42.040286+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.11/site-packages/starlette/middleware/errors.py", line 149, in __call__
2023-03-08T01:47:42.040286+00:00 app[web.1]: await self.app(scope, receive, send)
2023-03-08T01:47:42.040286+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.11/site-packages/starlette/middleware/gzip.py", line 26, in __call__
2023-03-08T01:47:42.040286+00:00 app[web.1]: await self.app(scope, receive, send)
2023-03-08T01:47:42.040287+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 79, in __call__
2023-03-08T01:47:42.040287+00:00 app[web.1]: raise exc
2023-03-08T01:47:42.040287+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 68, in __call__
2023-03-08T01:47:42.040287+00:00 app[web.1]: await self.app(scope, receive, sender)
2023-03-08T01:47:42.040287+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.11/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in __call__
2023-03-08T01:47:42.040287+00:00 app[web.1]: raise e
2023-03-08T01:47:42.040287+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.11/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in __call__
2023-03-08T01:47:42.040288+00:00 app[web.1]: await self.app(scope, receive, send)
2023-03-08T01:47:42.040288+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.11/site-packages/starlette/routing.py", line 706, in __call__
2023-03-08T01:47:42.040288+00:00 app[web.1]: await route.handle(scope, receive, send)
2023-03-08T01:47:42.040288+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.11/site-packages/starlette/routing.py", line 443, in handle
2023-03-08T01:47:42.040288+00:00 app[web.1]: await self.app(scope, receive, send)
2023-03-08T01:47:42.040288+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.11/site-packages/engineio/async_drivers/asgi.py", line 58, in __call__
2023-03-08T01:47:42.040288+00:00 app[web.1]: await self.engineio_server.handle_request(scope, receive, send)
2023-03-08T01:47:42.040289+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.11/site-packages/socketio/asyncio_server.py", line 398, in handle_request
2023-03-08T01:47:42.040289+00:00 app[web.1]: return await self.eio.handle_request(*args, **kwargs)
2023-03-08T01:47:42.040289+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-03-08T01:47:42.040289+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.11/site-packages/engineio/asyncio_server.py", line 269, in handle_request
2023-03-08T01:47:42.040289+00:00 app[web.1]: socket = self._get_socket(sid)
2023-03-08T01:47:42.040289+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^^^
2023-03-08T01:47:42.040290+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.11/site-packages/engineio/server.py", line 638, in _get_socket
2023-03-08T01:47:42.040293+00:00 app[web.1]: raise KeyError('Session is disconnected')
2023-03-08T01:47:42.040293+00:00 app[web.1]: KeyError: 'Session is disconnected'
2023-03-08T01:47:42.040673+00:00 app[web.1]: INFO: connection open
2023-03-08T01:47:42.041097+00:00 app[web.1]: INFO: connection closed
web: uvicorn --host 0.0.0.0 --port $PORT redistests:app
The minimal FastAPI example provided in demos does not run when deploying to Heroku. On opening the UI page, it continuously drops connection and attempts to reconnect/reload the page. HTTP traffic in the developer inspector shows numerous 400 and 403 errors. In the Heroku logs, this section at the start provides some insight into what goes wrong.
I'm deploying with the following Procfile:
Configurations using
gunicorn/WSGI have the same issue.Edit: I just realized it'd probably be helpful to link to the deployed page as well.