MagicStack / MagicStack/asyncpg

Query Wait Timout Error from Pgbouncer Not Propogating to Client

オープン
#1,299 コメント 0 件 リアクション 2 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

主要言語
Python
スター
8.1k
フォーク
468
PR マージ指標
30日以内にマージされた PR はありません

説明

When pgbouncer kills a connection due to its query_wait_timeout, the error message is not propagated to the client. All that is provided to the client is a not very helpful message "connection was closed in the middle of operation". This error message seems to be correctly handled by psql and psycopg.

I replicated this by using a pgbouncer instance with a pool_size of 1 and made sure another connection was consuming this connection to ensure that the connection would be put in a waiting mode.

asyncpg version

 > python -c 'import asyncpg; print(asyncpg.__version__)'
0.31.0

Example asyncpg code

import asyncio
import asyncpg

async def main():
    conn = await asyncpg.connect('postgresql://postgres@localhost:6432/postgres')
    row = await conn.execute('SELECT 1')

asyncio.run(main())

Stdout from the above code

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.12/asyncio/runners.py", line 194, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/asyncio/base_events.py", line 687, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "<stdin>", line 3, in main
  File "/path/to/venv/lib/python3.12/site-packages/asyncpg/connection.py", line 349, in execute
    result = await self._protocol.query(query, timeout)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "asyncpg/protocol/protocol.pyx", line 375, in query
asyncpg.exceptions.ConnectionDoesNotExistError: connection was closed in the middle of operation

Example code for psycopg

import psycopg
with psycopg.connect("postgresql://postgres:@localhost:6432/postgres") as conn:
    with conn.cursor() as cur:
         cur.execute("SELECT 1")

example traceback from psycopg

Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
  File "/path/to/venv/lib/python3.12/site-packages/psycopg/cursor.py", line 97, in execute
    raise ex.with_traceback(None)
psycopg.errors.ProtocolViolation: query_wait_timeout

Example call and stdout from psql on query_wait_timeout

> psql \
    'postgresql://postgres:@localhost:6432/postgres' \
    --command='SELECT 1'

FATAL:  query_wait_timeout
server closed the connection unexpectedly
        This probably means the server terminated abnormally
        before or while processing the request.
connection to server was lost

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

Issue に記載されている pgbouncer のセットアップを通じて timeout を再現し、その後 asyncpg/connection.py の Connection.execute パスと asyncpg/protocol/protocol.pyx の query から調査を開始してください。エラー処理を psycopg および psql の例と比較し、クライアントが接続が閉じられたと報告するだけでなく、query_wait_timeout エラーを公開することを確認してください。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
postgresql, python
領域
databases
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
38/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。