direct_tls doesn't work with PostgreSQL 17+ because ALPN isn't set
还没有人认领这个 Issue。
评估
调研方向
首先定位 asyncpg 为 direct_tls 创建的 SSL 上下文,以及处理直接 SSL 协商的连接路径。使用 issue 中的 PostgreSQL 18 Docker 命令复现,然后验证 direct_tls 连接可与默认上下文正常工作,并为 PostgreSQL ALPN 协议添加或更新覆盖率。
由索引模型根据 Issue 内容生成。
描述
- 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.
- 主要语言
- Python
- 星标
- 8.1k
- 派生
- 469
- 平均合并
- 4 天 1 小时
- 30 天内合并 PR
- 14
贡献指南
这个仓库没有索引到贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
MagicStack/asyncpg 的其他 Issue
-
难度 3/5 1-2 天 新手友好度 72/100
MagicStack/asyncpg#1342 ·
-
难度 3/5 1-2 天 新手友好度 56/100
MagicStack/asyncpg#1340 · 1 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 28/100
MagicStack/asyncpg#1337 ·
-
难度 4/5 3-5 天 新手友好度 42/100
MagicStack/asyncpg#1330 · 1 条评论 ·
-
难度 5/5 一周以上 新手友好度 38/100
MagicStack/asyncpg#1322 ·
查看 MagicStack/asyncpg 的全部 Issue
相似的 Issue
-
agent-ready documentation needs-triage
难度 1/5 1-3 小时 新手友好度 88/100
-
documentation
难度 1/5 1 小时以内 新手友好度 91/100
-
workflow-status page template still says reusable workflows are "triggered only by workflow_call:" 未关闭
难度 1/5 1 小时以内 新手友好度 92/100
-
instance instance add
难度 1/5 1 小时以内 新手友好度 72/100
searxng/searx-instances#939 · 1 条评论 ·
-
area-deployment area-integrations triage:bot-seen
难度 2/5 半天 新手友好度 86/100