Skip to content

sqlite3: use-after-free when the connection is closed during execute #162288

Open

Description

@johng

Bug report

This was found after doing a close and execute in different threads as part of free threading crash analysis, however it also exposed this as a gap on GIL builds where we can close during statement preperation not just another thread.

1. GIL use-after-free

Cursor.execute() looks up the prepared statement by calling the connection's statement cache (a functools.lru_cache wrapper) through a borrowed reference:

// Modules/_sqlite/cursor.c, get_statement_from_cache()
PyObject *cache = self->connection->statement_cache;
return PyObject_Vectorcall(cache, args + 1, nargsf, NULL);

The connection holds the only reference to the cache. If Connection.close() runs while that call is in progress, Py_CLEAR(self->statement_cache) frees the lru_cache object while bounded_lru_cache_wrapper is still running, and the wrapper then reads its freed cache dict.

This can be triggered from a single thread, because the cache lookup hashes the SQL string, and the hash of a str subclass is Python code:

import sqlite3

cx = sqlite3.connect(":memory:")

class SQL(str):
    def __hash__(self):
        cx.close()
        return super().__hash__()

cx.execute(SQL("select 1"))   # segfault

Proposed fix:

Take a strong reference to the cache under the connection's critical section.

2. free-threading race condition between close() and execute() on self->statement_cache.

Closing currently clears the statement_cache which is being read during execution resulting in a UAD.

Proposed fix:

Apply a critical section to the operations

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    on Nov 19, 2026
  2. changed the title [-]sqlite3: use-after-free when the connection is closed during the statement cache lookup[/-] [+]sqlite3: use-after-free when the connection is closed during execute[/+] on Nov 19, 2026
  3. johng commented on Nov 19, 2026

    @johng
    ContributorAuthor

    I've raised a draft PR demonstrating these issues and fix proposals, happy to re-organise these issues or discuss a wider high level fix here first!

  4. picnixz commented on Nov 19, 2026

    @picnixz
    Member

    This is already known I think. we have an open issue about it in general and one of my PR as well

  5. johng commented on Nov 19, 2026

    @johng
    ContributorAuthor

    I will admit I missed that open issue when I searched earlier, sorry! I certainly see they are related so maybe it can be combined however I believe they are slightly different failure cases?

    Believe some of the gaps identified here would still be open even though they certainly are similar style of bugs. Would you prefer this is all combined?

    Just to confirm this issue? #143198 thanks!

  6. added
    type-crashA hard crash of the interpreter, possibly with a core dump
    and removed
    type-bugAn unexpected behavior, bug, or error
    on Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions