python / python/cpython

sqlite3 Blob objects leave behind weakrefs which are never cleaned up

Open
#144,424 4 comments 0 reactions 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:

The sqlite3 Blob class registers a weakref to itself on the Connection it came from. These are added to a list and never cleaned up, so if you have a long-lived connection and call .blobopen() repeatedly, you gradually accumulate ever more dead weakrefs.

It looks like the blob weakrefs were added to match a similar system for weakrefs (removed in #144378), but the code to periodically clear out dead weakrefs was missed.

import gc
import sqlite3

conn = sqlite3.connect(":memory:")
conn.execute("CREATE TABLE foo(a INTEGER PRIMARY KEY, b BLOB)")
conn.execute("INSERT INTO foo VALUES(NULL, ?)", (b'abcdefpokpokpko',))
conn.commit()

# Create & destroy many Blob objects
for _ in range(10_000):
    conn.blobopen("foo", "b", 1)

# Get a reference to the internal list of blob weakrefs - you may need to play with the index here
blob_list = gc.get_referents(conn)[-3]
print(len(blob_list))
CPython versions tested on:

3.14

Operating systems tested on:

Linux

Linked PRs
  • gh-144435

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

The report identifies weakrefs registered by sqlite3 Blob objects on long-lived connections and links PR gh-144435. Start by reviewing the Blob weakref registration and the related weakref-cleanup pattern mentioned in the report, then run the supplied reproduction to check whether dead references stop accumulating; coordinate with the linked PR rather than duplicating work.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.