MagicStack / MagicStack/asyncpg
ConnectionDoesNotExistError when releasing
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 8.1k
- Forks
- 468
- PR merge metrics
- No merged PRs in 30d
Description
* **asyncpg version**: 0.21.0
* **PostgreSQL version**: 10.7
* **Do you use a PostgreSQL SaaS? If so, which? Can you reproduce
the issue with a local PostgreSQL install?**: no
* **Python version**: 3.7
* **Platform**: debian (docker image `python:3.7.9-slim`)
* **Do you use pgbouncer?**: no
* **Did you install asyncpg with pip?**: yes
* **If you built asyncpg locally, which version of Cython did you use?**: no
* **Can the issue be reproduced under both asyncio and
[uvloop](https://github.com/magicstack/uvloop)?**: only tried asyncio
I've found the following exception in our logs:
```
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/asyncpg/pool.py", line 214, in release
self._con.terminate()
AttributeError: 'NoneType' object has no attribute 'terminate'
During handling of the above exception, another exception occurred:
File "/usr/local/lib/python3.7/site-packages/my_code_calling_release.py", line 40, in acquire
await self.pool.release(con)
File "/usr/local/lib/python3.7/site-packages/asyncpg/pool.py", line 654, in release
return await asyncio.shield(ch.release(timeout))
File "/usr/local/lib/python3.7/site-packages/asyncpg/pool.py", line 216, in release
raise ex
File "/usr/local/lib/python3.7/site-packages/asyncpg/pool.py", line 206, in release
await self._con.reset(timeout=budget)
File "/usr/local/lib/python3.7/site-packages/asyncpg/connection.py", line 1137, in reset
await self.execute(reset_query, timeout=timeout)
File "/usr/local/lib/python3.7/site-packages/asyncpg/connection.py", line 295, in execute
return await self._protocol.query(query, timeout)
File "asyncpg/protocol/protocol.pyx", line 316, in query
```
From what I can see, the call at `self._con.reset` is raising a `ConnectionDoesNotExistError`.
Should the exception be reraised at line 218 if the exception is an `ConnectionDoesNotExistError` (we know it's already closed)? It shouldn't even try to `self._con.terminate()` because `self._con` is already None (see the exception).
Not sure if the comments in that code still apply for that type of exception
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Read the release path around lines 206-218 of asyncpg/pool.py and the reset path in asyncpg/connection.py, focusing on how ConnectionDoesNotExistError leaves self._con as None. Reproduce or add a regression case for releasing an already-closed connection, then verify that release does not attempt to terminate a missing connection and preserves the intended exception behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- postgresql, python
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100