Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

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

未关闭 适合新手
#1,371 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
76/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
postgresql, python
领域
databases

调研方向

首先定位 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

贡献指南

这个仓库没有索引到贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

MagicStack/asyncpg 的其他 Issue

查看 MagicStack/asyncpg 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。