miguelgrinberg / python-engineio

Python Engine.IO server and client
MIT License
233 stars 148 forks source link

returns internal server error when called using asgi servers (daphne, uvicorn) #344

Closed hetvaghasia39 closed 9 months ago

hetvaghasia39 commented 9 months ago

Description The Daphne server encounters an error when handling an application. The error is related to the translate_request() function and occurs during the handling of a WebSocket request.

To Reproduce Steps to reproduce the behavior:

  1. Run the Daphne server with the command: daphne infinity_talks.asgi:application
  2. Make a WebSocket request to the server.

Expected behavior The Daphne server should handle WebSocket requests without encountering errors.

Logs daphne logs

2024-02-01 17:06:44,756 INFO     Starting server at tcp:port=8000:interface=127.0.0.1
2024-02-01 17:06:44,756 INFO     HTTP/2 support not enabled (install the http2 and tls Twisted extras)
2024-02-01 17:06:44,757 INFO     Configuring endpoint tcp:port=8000:interface=127.0.0.1
2024-02-01 17:06:44,757 INFO     Listening on TCP address 127.0.0.1:8000
2024-02-01 17:06:49,762 ERROR    Exception inside application: translate_request() takes 1 positional argument but 3 were given
Traceback (most recent call last):
  File "/home/het/het-workspace/infiniti_talks/.venv/lib/python3.12/site-packages/engineio/async_drivers/asgi.py", line 58, in __call__
    await self.engineio_server.handle_request(scope, receive, send)
  File "/home/het/het-workspace/infiniti_talks/.venv/lib/python3.12/site-packages/socketio/async_server.py", line 448, in handle_request
    return await self.eio.handle_request(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/het/het-workspace/infiniti_talks/.venv/lib/python3.12/site-packages/engineio/async_server.py", line 211, in handle_request
    environ = translate_request(*args, **kwargs)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: translate_request() takes 1 positional argument but 3 were given
127.0.0.1:44806 - - [01/Feb/2024:17:06:49] "GET /socket.io/" 500 453

uvicorn logs

uvicorn infinity_talks.uvicorn_asgi:application
INFO:     Started server process [39933]
INFO:     Waiting for application startup.
INFO:     ASGI 'lifespan' protocol appears unsupported.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "/home/het/het-workspace/infiniti_talks/.venv/lib/python3.12/site-packages/uvicorn/protocols/http/h11_impl.py", line 404, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/het/het-workspace/infiniti_talks/.venv/lib/python3.12/site-packages/uvicorn/middleware/proxy_headers.py", line 84, in __call__
    return await self.app(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/het/het-workspace/infiniti_talks/.venv/lib/python3.12/site-packages/engineio/async_drivers/asgi.py", line 58, in __call__
    await self.engineio_server.handle_request(scope, receive, send)
  File "/home/het/het-workspace/infiniti_talks/.venv/lib/python3.12/site-packages/socketio/async_server.py", line 448, in handle_request
    return await self.eio.handle_request(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/het/het-workspace/infiniti_talks/.venv/lib/python3.12/site-packages/engineio/async_server.py", line 211, in handle_request
    environ = translate_request(*args, **kwargs)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: translate_request() takes 1 positional argument but 3 were given
INFO:     127.0.0.1:33576 - "GET /socket.io/ HTTP/1.1" 500 Internal Server Error
^[[A^CINFO:     Shutting down
INFO:     Finished server process [39933]

Additional context The error occurs in the translate_request() function, and it seems to be related to the number of positional arguments passed to the function. This issue needs further investigation and resolution. And this is occurring in Django

miguelgrinberg commented 9 months ago

Does this happen with the ASGI examples in this repo, or with your own application? My guess is that this is your own app, and that you have not configured for ASGI correctly. Probably a missing async_mode='asgi'.

hetvaghasia39 commented 9 months ago

Does this happen with the ASGI examples in this repo, or with your own application? My guess is that this is your own app, and that you have not configured for ASGI correctly. Probably a missing async_mode='asgi'.

with my own app, but where should I configure it? And I am using python-socketio

miguelgrinberg commented 9 months ago

@hetvaghasia39 There are several examples for every supported platform in the python-socketio repo. Have you seen them?

hetvaghasia39 commented 9 months ago

Thank you so much for your help! I just saw your suggestion and implemented async_mode="asgi" for my server object, and it worked like a charm. I really appreciate your quick and effective solution. Thanks again!