python / python/cpython

sqlite3 connections always create a reference cycle

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

还没有人认领这个 Issue。

extension-modules topic-sqlite3 type-bug
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

Bug report

Bug description:

This is perhaps an annoyance rather than a bug, but it's nice to avoid creating reference cycles if possible. Practically, this means that the ResourceWarning from an sqlite connection object going out of scope fires from a random irrelevant line - where the cyclic GC happens to run - rather than predictably where the object goes out of scope.

The cycle is between the connection object and a functools _lru_cache_wrapper object, as can be seen like this:

import gc
import sqlite3

conn = sqlite3.connect(":memory:")
referrers = gc.get_referrers(conn)
for obj1 in gc.get_referents(conn):
    for obj2 in referrers:
        if obj1 is obj2:
            print(obj1)

If I'm following the relevant C code correctly, it's similar to doing this in Python:

# In Connection.__init__
self.statement_cache = lru_cache(maxsize)(self)

# In Cursor get_statement_from_cache
self.connection.statement_cache(sql)

I believe the reference cycle could be avoided by doing something like this:

# A function (/staticmethod etc.) not bound to the Connection instance
def compile_stmt(wr_conn, sql):
    conn = wr_conn()
    assert conn is not None
    return conn(sql)

# In Connection.__init__
self.statement_cache = lru_cache(maxsize)(compile_stmt)

# In Cursor get_statement_from_cache
wr_conn = weakref.ref(self.connection)
self.connection.statement_cache(wr_conn, sql)

Or functools.partial could be used to wrap the function so that the weakref is only created once. The significant bit is the connection's cache having only weakrefs back to the connection object. I'm not great at writing C code, but I think I can see that everything required for this is available as C functions.

Thanks!

CPython versions tested on:

3.14

Operating systems tested on:

Linux

Linked PRs
  • gh-144383

贡献指南

打开贡献指南

从这里开始

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

调研方向

从报告中描述的入口点 sqlite3 Connection.init 和 Cursor get_statement_from_cache 开始,然后审查相关的 PR gh-144383。当连接缓存不再创建引用循环,并且连接超出作用域时 ResourceWarning 的行为可预测时,即表示完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
python, sqlite
领域
databases
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
25/100

把新 issue 发到你的邮箱

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