python / python/cpython

sqlite3 connections always create a reference cycle

Open
#144,345 10 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

extension-modules topic-sqlite3 type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the sqlite3 Connection.init and Cursor get_statement_from_cache entry points described in the report, then review linked PR gh-144383. Done means the connection cache no longer creates a reference cycle and ResourceWarning behavior is predictable when the connection goes out of scope.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, sqlite
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.