Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

direct_tls doesn't work with PostgreSQL 17+ because ALPN isn't set

Đang mở Phù hợp với người mới
#1,371 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
76/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
postgresql, python
Lĩnh vực
databases

Hướng nghiên cứu

Trước tiên, hãy xác định các ngữ cảnh SSL mà asyncpg tạo cho direct_tls và đường dẫn kết nối xử lý quá trình thương lượng SSL trực tiếp. Tái hiện bằng lệnh Docker PostgreSQL 18 trong issue, sau đó xác minh rằng các kết nối với direct_tls hoạt động với ngữ cảnh mặc định và thêm hoặc cập nhật độ bao phủ cho giao thức ALPN của PostgreSQL.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

  • asyncpg version: 0.31.0 (also checked master)
  • PostgreSQL version: 17.11 and 18.0
  • Do you use a PostgreSQL SaaS? If so, which? Can you reproduce
    the issue with a local PostgreSQL install?
    : No, reproduced with a local PostgreSQL in Docker
  • Python version: 3.12
  • Platform: Linux
  • Do you use pgbouncer?: No
  • Did you install asyncpg with pip?: Yes (via uv)
  • If you built asyncpg locally, which version of Cython did you use?: N/A
  • Can the issue be reproduced under both asyncio and
    uvloop?
    : Only tried asyncio

When connecting with direct_tls=True, the connection gets dropped right away:

await asyncpg.connect(dsn, ssl="require", direct_tls=True)
# ConnectionDoesNotExistError: connection was closed in the middle of operation

The server log says why:

LOG:  received direct SSL connection request without ALPN protocol negotiation extension

PostgreSQL 17 requires the postgresql ALPN protocol for direct SSL connections, and libpq always sends it. asyncpg doesn't set ALPN on the context it builds, so as far as I can tell direct_tls (and sslnegotiation=direct in the DSN) can't work against a stock PG 17+ server unless you pass your own context:

ctx = ssl.create_default_context()
ctx.set_alpn_protocols(["postgresql"])
await asyncpg.connect(dsn, ssl=ctx, direct_tls=True)  # works

Repro with Docker:

docker run --rm -e POSTGRES_PASSWORD=postgres -p 5432:5432 postgres:18 \
  -c ssl=on \
  -c ssl_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem \
  -c ssl_key_file=/etc/ssl/private/ssl-cert-snakeoil.key

I'll send a PR that sets postgresql ALPN on the contexts asyncpg creates.

Ngôn ngữ chính
Python
Star
8.1k
Fork
469
Merge trung bình
4 ngày 1 giờ
Pull request đã merge (30 ngày)
14

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của MagicStack/asyncpg

Tất cả issue của MagicStack/asyncpg

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.