sanic-org / sanic

Accelerate your web app development | Build fast. Run fast.
https://sanic.dev
MIT License
17.92k stars 1.54k forks source link

[python-socketio (websocket transport)] sanic.exceptions.ServerError: Invalid response type None (need HTTPResponse) on Disconnect #2572

Open 4n1qz5skwv opened 1 year ago

4n1qz5skwv commented 1 year ago

Requirements:

sanic==22.9.0
python-socketio==5.7.1

Code:

import sanic
import socketio

app = sanic.Sanic("app")

@app.on_request
async def print_urls(request):
    print(f'>>>> {request.url}')

@app.route("/")
async def handler(_):
    return sanic.response.html(
        '''<!doctype html>
        <html lang="en">
           <head></head>
           <body>
              <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.1/socket.io.js"></script>
              <script>
                 var socket = io({
                     path: "/socket.io/",
                     transports: ["websocket"]
                 });
              </script>
           </body>
        </html>''')

sio = socketio.AsyncServer(async_mode='sanic', logger=False, engineio_logger=False)
sio.attach(app)

@sio.event
def connect(sid, *_):
    print(f'>> Connected Successfully: {sid}')

@sio.event
def disconnect(sid):
    print(f'>> Disconnected Successfully: {sid}')

if __name__ == '__main__':
    app.run(access_log=False)

Error:

https://user-images.githubusercontent.com/76768193/195773474-4a0cf52c-2ca2-45eb-9f12-4b0901a2bc76.mp4

More: Problem occurs only after disconnection when websocket transport is used. Details in video. Everything else is working Perfectly.

4n1qz5skwv commented 1 year ago

Old related issues:

https://github.com/sanic-org/sanic/issues/2105

ahopkins commented 1 year ago

I can try and take a look next week. Did you try as a hack to return a dummy response object from the disconnect callback?

ahopkins commented 1 year ago

I'll be honest, I have no clue how that is "attaching" to Sanic and plugging into the router for example 🤷‍♂️

ChihweiLHBird commented 3 months ago

I couldn't reproduce the issue with latest Sanic (main branch), latest python-socketio (5.11.2), and Python 3.11 on Linux.

catdogmat commented 1 week ago

I also can't reproduce the issue with latest Sanic (v24.6.0), latest python-socketio (5.11.3) and Python 3.11.8 on Windows. I believe this is fixed!

catdogmat commented 3 days ago

This is fixed and can be closed! See: https://github.com/miguelgrinberg/python-socketio/commit/287d6ed551090eed822f924bb548ba93923dd4d1