-
Notifications
You must be signed in to change notification settings - Fork 404
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
connections not returning to connection pool on CancelledError #97
Comments
This is not specific to asyncpg. aiohttp cancels the entire task, including the @1st1 : ideas on the best way of handling this? One way is to |
I understand this isn't specific to asyncpg. It's possible to make task cancellation unlikely but not impossible. (eg. this app could call Therefore would it be possible to have a |
Well, the app shouldn't be using a pool if it plans to hold onto a connection for a long time. In your case there's no reason why a db connection should be open for the entirety of a websocket connection. You're essentially limiting the number of concurrent users with the size of the pool. |
Ye, I realise that but the long-lived connection was required for the listener. I've now changed it to use redis pub-sub so the pg connections are only acquired when required. |
You only really need one listener in a background task and a list of currently open WS connections. |
I realised that exact thing just after hitting comment. 😄 |
Oh, and |
We can wrap |
Use asyncio.shield() to guarantee that task cancellation does not prevent the connection from being returned to the pool properly. Fixes: #97.
This works! I implemented the fix in #98. |
Use asyncio.shield() to guarantee that task cancellation does not prevent the connection from being returned to the pool properly. Fixes: #97.
Use asyncio.shield() to guarantee that task cancellation does not prevent the connection from being returned to the pool properly. Fixes: #97.
Use asyncio.shield() to guarantee that task cancellation does not prevent the connection from being returned to the pool properly. Fixes: #97.
very cool, thanks. |
uvloop?: yes
In a websocket handler with aiohttp (2.0.4) I have (simplified for brevity):
This is causing problems; if a browser is closed without calling
websocket.close()
in js, the handler is terminated with aCancelledError
,__aexit__
also gets cancelled and the connection is never returned to the pool.If I use
The connection is correctly released and the pool doesn't run out of connections.
(actual code is here if that helps.)
I'm not sure whether asyncpg can resolve this or whether it's a problem with aiohttp?
Would it be possible for the pool manager to somehow recover those connections which weren't released?
Or could there be an alternative
acquire
used withwith await pool.acquire() as conn:
so__exit__
was not a coroutine?The text was updated successfully, but these errors were encountered: