sqlite3 connections always create a reference cycle
まだ誰も着手していません。
- 主要言語
- 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
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
レポートで説明されているエントリーポイント sqlite3 Connection.init と Cursor get_statement_from_cache から始め、リンクされている PR gh-144383 を確認します。接続キャッシュが参照サイクルを作成しなくなり、接続がスコープ外に出たときの ResourceWarning の動作が予測可能になれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python, sqlite
- 領域
- databases
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 25/100