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

FastAPI deployment on Heroku socket.io errors #469

Closed
krummja opened this issue Mar 8, 2023 · 11 comments
Closed

FastAPI deployment on Heroku socket.io errors #469

krummja opened this issue Mar 8, 2023 · 11 comments
Labels
documentation Improvements or additions to documentation
Milestone

Comments

@krummja
Copy link

krummja commented Mar 8, 2023

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

I'm deploying with the following Procfile:

web: uvicorn --host 0.0.0.0 --port $PORT redistests:app

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.

@rodja
Copy link
Member

rodja commented Mar 9, 2023

Is the basic FastAPI app working? E.g. what happens if you are commenting out

frontend.init(app)

Also it would be interesting to see the output with the newest version of NiceGUI 1.1.10. We changed the websocket path to not collide with the python-socketio default...

@krummja
Copy link
Author

krummja commented Mar 9, 2023

The FastAPI app works, yes. I just updated and the problem is unfortunately unchanged.

@rodja
Copy link
Member

rodja commented Mar 13, 2023

Ok, I just tried it myself. Seems that pure socket.io is already causing the problem. Here a minimal setup

Filename: Procfile

web: uvicorn --host 0.0.0.0 --port $PORT main:app

Filename: main.py

import uvicorn
from fastapi import FastAPI
from fastapi_socketio import SocketManager
from fastapi.responses import HTMLResponse

app = FastAPI()
socket_manager = SocketManager(app=app)

@app.get("/", response_class=HTMLResponse)
async def index():
    return """
        <html>
            <head>
                <script src="https://cdn.socket.io/4.6.0/socket.io.min.js" integrity="sha384-c79GN5VsunZvi+Q/WObgk2in0CbZsHnjEqvFxC5DxHn9lTfNce2WW6h2pH6u/kF+" crossorigin="anonymous"></script>
                <script>
                    var socket = io({path:"/ws/socket.io/"});
                    socket.on('connect', function() {
                        console.log('Connected!');
                    });
                </script>
            </head>
            <body>
                <h1>Socket.IO Example</h1>
            </body>
        </html>
    """

Filename: requirements.txt

nicegui

Filename: runtime.txt

python-3.11.2

@krummja
Copy link
Author

krummja commented Mar 13, 2023

Thanks for the example - that seems to work for the SocketIO issue, but it looks like I'm still getting 400 errors. However, with some of the chatter reduced in the logs, I got a nice side-by-side for running locally (succeeds) and deployment on Heroku (fails).

INFO:     127.0.0.1:59391 - "GET / HTTP/1.1" 200 OK
INFO:     127.0.0.1:59391 - "GET /socket.io/?EIO=4&transport=polling&t=ORRMkZu HTTP/1.1" 200 OK
INFO:     127.0.0.1:59391 - "POST /socket.io/?EIO=4&transport=polling&t=ORRMkaX&sid=-OyyFbDN5RLA-TRMAAAA HTTP/1.1" 200 OK
INFO:     127.0.0.1:59391 - "GET /socket.io/?EIO=4&transport=polling&t=ORRMkaY&sid=-OyyFbDN5RLA-TRMAAAA HTTP/1.1" 200 OK
INFO:     ('127.0.0.1', 59395) - "WebSocket /socket.io/?EIO=4&transport=websocket&sid=-OyyFbDN5RLA-TRMAAAA" [accepted]
INFO:     connection open
INFO:     127.0.0.1:59391 - "GET /socket.io/?EIO=4&transport=polling&t=ORRMkaj&sid=-OyyFbDN5RLA-TRMAAAA HTTP/1.1" 200 OK
INFO:     connection closed
INFO:     127.0.0.1:59391 - "GET /show HTTP/1.1" 200 OK
INFO:     127.0.0.1:59391 - "GET /socket.io/?client_id=df89ac67-1929-48f5-ac8e-9afabcda8ea2&EIO=4&transport=polling&t=ORRMlgd HTTP/1.1" 200 OK
INFO:     127.0.0.1:59391 - "POST /socket.io/?client_id=df89ac67-1929-48f5-ac8e-9afabcda8ea2&EIO=4&transport=polling&t=ORRMlhd&sid=J7Ah5V4ZKiKV4vbhAAAA HTTP/1.1" 200 OK
INFO:     127.0.0.1:59391 - "GET /socket.io/?client_id=df89ac67-1929-48f5-ac8e-9afabcda8ea2&EIO=4&transport=polling&t=ORRMlhf&sid=J7Ah5V4ZKiKV4vbhAAAA HTTP/1.1" 200 OK 
INFO:     127.0.0.1:59391 - "POST /socket.io/?client_id=df89ac67-1929-48f5-ac8e-9afabcda8ea2&EIO=4&transport=polling&t=ORRMliB&sid=J7Ah5V4ZKiKV4vbhAAAA HTTP/1.1" 200 OK
INFO:     127.0.0.1:59392 - "GET /socket.io/?client_id=df89ac67-1929-48f5-ac8e-9afabcda8ea2&EIO=4&transport=polling&t=ORRMliB.0&sid=J7Ah5V4ZKiKV4vbhAAAA HTTP/1.1" 200 OK
INFO:     ('127.0.0.1', 59398) - "WebSocket /socket.io/?client_id=df89ac67-1929-48f5-ac8e-9afabcda8ea2&EIO=4&transport=websocket&sid=J7Ah5V4ZKiKV4vbhAAAA" [accepted]   
INFO:     connection open
INFO:     127.0.0.1:59392 - "GET /socket.io/?client_id=df89ac67-1929-48f5-ac8e-9afabcda8ea2&EIO=4&transport=polling&t=ORRMlig&sid=J7Ah5V4ZKiKV4vbhAAAA HTTP/1.1" 200 OK
INFO:     47.4.204.57:0 - "GET / HTTP/1.1" 200 OK
INFO:     47.4.204.57:0 - "GET /socket.io/?EIO=4&transport=polling&t=ORRKABy HTTP/1.1" 200 OK
INFO:     47.4.204.57:0 - "POST /socket.io/?EIO=4&transport=polling&t=ORRKACZ&sid=LlE5GMtd7WR3IKd5AAAC HTTP/1.1" 200 OK
INFO:     47.4.204.57:0 - "GET /socket.io/?EIO=4&transport=polling&t=ORRKACa&sid=LlE5GMtd7WR3IKd5AAAC HTTP/1.1" 200 OK
INFO:     ('47.4.204.57', 0) - "WebSocket /socket.io/?EIO=4&transport=websocket&sid=LlE5GMtd7WR3IKd5AAAC" 403
INFO:     connection failed (403 Forbidden)
INFO:     connection closed
2023-03-13T14:12:19.199139+00:00 heroku[router]: at=info method=GET path="/ws/socket.io/?EIO=4&transport=polling&t=ORRKABy" host=redistests.herokuapp.com request_id=69bbfb49-cd9a-4eff-8e07-33dae44d0231 fwd="47.4.204.57" dyno=web.1 connect=0ms service=1ms status=200 bytes=309 protocol=https
2023-03-13T14:12:19.238542+00:00 heroku[router]: at=info method=POST path="/ws/socket.io/?EIO=4&transport=polling&t=ORRKACZ&sid=LlE5GMtd7WR3IKd5AAAC" host=redistests.herokuapp.com request_id=9862659f-b141-4780-a8d6-6f4c5100c57c fwd="47.4.204.57" dyno=web.1 connect=0ms service=1ms status=200 bytes=261 protocol=https
2023-03-13T14:12:19.242354+00:00 heroku[router]: at=info method=GET path="/ws/socket.io/?EIO=4&transport=polling&t=ORRKACa&sid=LlE5GMtd7WR3IKd5AAAC" host=redistests.herokuapp.com request_id=06f59032-b5a8-486e-b7e8-5b43471a484d fwd="47.4.204.57" dyno=web.1 connect=0ms service=1ms status=200 bytes=244 protocol=https
2023-03-13T14:12:19.293530+00:00 heroku[router]: at=info method=GET path="/ws/socket.io/?EIO=4&transport=websocket&sid=LlE5GMtd7WR3IKd5AAAC" host=redistests.herokuapp.com request_id=1a07bdae-9d78-4ac7-9b4e-2aa40385b69c fwd="47.4.204.57" dyno=web.1 connect=0ms service=1ms status=403 bytes=127 protocol=https
2023-03-13T14:12:23.475738+00:00 heroku[router]: at=info method=GET path="/show" host=redistests.herokuapp.com request_id=57a4340e-8ff8-48f4-9137-32514f80f5aa fwd="47.4.204.57" dyno=web.1 connect=0ms service=3ms status=200 bytes=3889 protocol=https
2023-03-13T14:12:23.588583+00:00 heroku[router]: at=info method=GET path="/_nicegui/static/vue.global.prod.js" host=redistests.herokuapp.com request_id=44ffc8c3-43c2-4322-85cf-f144e56cde68 fwd="47.4.204.57" dyno=web.1 connect=0ms service=3ms status=304 bytes=142 protocol=https
2023-03-13T14:12:23.528187+00:00 heroku[router]: at=info method=GET path="/_nicegui/static/fonts.css" host=redistests.herokuapp.com request_id=4777dae5-406e-4272-b7f3-5888b5000a8b fwd="47.4.204.57" dyno=web.1 connect=0ms service=2ms status=304 bytes=142 protocol=https
2023-03-13T14:12:23.560015+00:00 heroku[router]: at=info method=GET path="/_nicegui/static/nicegui.css" host=redistests.herokuapp.com request_id=0055770e-cca3-42b4-89e4-5b9d5ca2ebe3 fwd="47.4.204.57" dyno=web.1 connect=0ms service=2ms status=304 bytes=142 protocol=https

As you can see, it looks like it's failing when first attempting to establish the socket connection. Additionally, I get "Invalid session" at the very start of the logging each time the app is restarted. I've been digging into the problem to figure out how to fix it but I figure the additional context here will be useful.

@rodja
Copy link
Member

rodja commented Mar 14, 2023

Thanks for the example - that seems to work for the SocketIO issue, but it looks like I'm still getting 400 errors

@krummja does my minimal example without NiceGUI work for you?

@krummja
Copy link
Author

krummja commented Mar 14, 2023

Yes, without NiceGUI it works fine.

@rodja
Copy link
Member

rodja commented Mar 18, 2023

That is strange. As I wrote in #469 (comment), my minimal example does not work on Heroku:

Screen Shot 2023-03-18 at 05 27 53

Locally it works as expected.

@rodja
Copy link
Member

rodja commented Mar 18, 2023

Oh no! The solution was so simple! When limiting the number of workers to 1, everything works as expected. Even with NiceGUI:

Procfile

web: uvicorn --host 0.0.0.0 --port $PORT --workers 1 main:app

@krummja
Copy link
Author

krummja commented Mar 18, 2023

Good grief... Well, I'll take simple over the alternative. Thanks a bunch for looking into the issue!

rodja added a commit that referenced this issue Mar 18, 2023
We should always use workers == 1
rodja added a commit that referenced this issue Mar 18, 2023
rodja added a commit that referenced this issue Mar 18, 2023
@rodja rodja added this to the v1.2.0 milestone Mar 19, 2023
@rodja rodja added the documentation Improvements or additions to documentation label Mar 19, 2023
@rodja rodja closed this as completed Mar 19, 2023
@ColeBlender
Copy link

Oh no! The solution was so simple! When limiting the number of workers to 1, everything works as expected. Even with NiceGUI:

Procfile

web: uvicorn --host 0.0.0.0 --port $PORT --workers 1 main:app

I had the same problem and fixed it with this solution. I've read that this can slow your app down though especially a large one. Do you know if this is the case and if so is there a way to compensate for this?

@rodja
Copy link
Member

rodja commented May 17, 2023

I've read that this can slow your app down though especially a large one. Do you know if this is the case and if so is there a way to compensate for this?

It depends on what is meant by "large". NiceGUI (and FastAPI, and Starlette) are async frameworks. That means, no io-bound or cpu-bound tasks should be directly executed on the main thread but rather be "awaited". See https://fastapi.tiangolo.com/async/ for more details.
Your app can have many sites, large amounts of content etc without needing multiple workers as long as it does not block the main thread. But non-optimized code may have lags sometimes which can be circumvented by providing multiple guvicorn workers. I've just created such a feature request: #929

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

3 participants