Gino-sanic + Websockets: Shutdown hangs, causes OSError: Address already in use

Hey all! I’ve been having an issue with the websockets portion of my project.
I am using Python 3.6.9, Sanic 20.6.3 and Gino 1.0.1 (gino-sanic 0.1.0). I am also using @ahopkins WS PubSub Example

Issues:
After a Client / Websocket connection is established, everything runs fine until it’s time to restart the server. I shut down the server using supervisorctl and it hangs for about 10 seconds. When I start it up again, I get this error: OSError: [Errno 98] Address already in use. I use ps to verify that my server is still running (it is). Additionally, if I refresh my websocket page a bunch of times it locks up.

Edit: Another related issue is that if a browser tab is refreshed a bunch of times, the entire site will lock up.

Cause:
In this case when the handler wrapped by @app.websocket() is completed (after the client connection is closed), the typical middleware isn’t run, and therefore the database connection is never returned to the pool.

These issues are exacerbated by having a low app.config.DB_POOL_MAX_SIZE

1 Like

The problem is that with each request, gino-sanic (by default) borrows a connection and attaches it to request.ctx.connection and then releases this connection when a response is returned, and because websockets run on, this connection remains open for as long as the websocket is open, and so the number of websocket clients is limited to the number of available pool connections.

This can be fixed by turning off DB_USE_CONNECTION_FOR_REQUEST when configuring the app with the database configs:

app.config.DB_USE_CONNECTION_FOR_REQUEST = False

1 Like