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

Continuous memory increase in Sanic web server after calling `loop.start_tls(...)` in `connect_utils._create_ssl_connection`

未关闭
#1,004 2 条评论 0 个 reaction 已指派 1 人 在 GitHub 查看

@fantix 已经在做这个了。

开始于 2023年2月17日。

评估

这个 Issue 还没有评估数据。

描述

  • asyncpg version: 0.2.7
  • PostgreSQL version: PostgreSQL 13.3 (Debian 13.3-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
  • Do you use a PostgreSQL SaaS? If so, which? Can you reproduce
    the issue with a local PostgreSQL install?
    : I use PostgreSQL installed in docker image kartoza/postgis:pr-333-13-3.1
  • Python version: 3.8.10
  • Platform: 20.04.1-Ubuntu x86_64
  • Do you use pgbouncer?: Not sure, I use it indirectly. (behind sqlalchemy)
  • Did you install asyncpg with pip?: Yes
  • If you built asyncpg locally, which version of Cython did you use?: /
  • Can the issue be reproduced under both asyncio and
    uvloop?
    : I see the code below is based on uvloop.

Background: I built a simple web server using sqlalchemy and sanic, then created a request handler function that can query my local PostgreSQL server running in docker. However, I saw continuous memory increase while benching my handler function.

After running tracemalloc for help, I got the result below:

Top 10 lines
#1: /home/tusimple/Workspace/projects/map-workdata-manager/venv/lib/python3.8/site-packages/asyncpg/connect_utils.py:700: 5670.1 KiB
    new_tr = await loop.start_tls(

The code for creating the async_engine is:

from sqlalchemy.ext.asyncio import create_async_engine
engine = create_async_engine("postgresql+asyncpg://{}:{}@{}/{}".format(...))

At first, I tried adding ?ssl=disable after the previous connect string in order to prevent connect_utils._create_ssl_connection calls, and it works.
Then, I tried to make some stupid changes to the source code of asyncpg, because I think this SSL transport is not closed after creating new transport. (I'm not sure is that correct)

# line 697 
    if hasattr(loop, 'start_tls'):
        if do_ssl_upgrade:
            try:
                new_tr = await loop.start_tls(
                    tr, pr, ssl_context, server_hostname=host)        # <--- here
            except (Exception, asyncio.CancelledError):
                tr.close()
                raise
        else:
            new_tr = tr
# line 706

Finally, I modified the code starts from line 702 to:

            except (Exception, asyncio.CancelledError):
                raise
            finally:
                tr.close()

After this modification, my web server keeps working fine, but I don't know whether this is a proper modification.

If you have any questions or ideas, please let me know, thank you so much!

主要语言
Python
星标
8.1k
派生
469
平均合并
2 天 20 小时
30 天内合并 PR
9

贡献指南

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

从这里开始

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

MagicStack/asyncpg 的其他 Issue

查看 MagicStack/asyncpg 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

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