Skip to content

sqlite3 Blob objects leave behind weakrefs which are never cleaned up #144424

Description

@takluyver

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

Activity

  1. vstinner commented on Feb 5, 2026

    @vstinner
    Member

    What is the purpose of this list of blob weak references? It seems to be unused. Why not just removing it instead?

  2. vstinner commented on Feb 5, 2026

    @vstinner
    Member

    What is the purpose of this list of blob weak references? It seems to be unused. Why not just removing it instead?

    Ah! It's used by pysqlite_close_all_blobs() function which is called by _sqlite3.Connection.close(). The function was added by the commit ee47543.

  3. erlend-aasland commented on Mar 4, 2026

    @erlend-aasland
    Contributor

    The list of blob weakrefs is used to close blobs on connection exit. Seems to me we can close this issue?

  4. takluyver commented on Mar 4, 2026

    @takluyver
    ContributorAuthor

    This issue is that the weakrefs themselves are not cleaned up, so if you repeatedly access blobs on a long-lived connection, the list of weakrefs grows indefinitely. It's not questioning whether those weakrefs are used. #144435 is my attempt to fix this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions