Skip to content

deleting sqlite3 row_factory causes segfault #149738

Description

@lunixbochs

Crash report

What happened?

import sqlite3
db = sqlite3.connect(":memory:")
del db.row_factory
db.execute("test")

-> zsh: segmentation fault python3

In connection.c, the row_factory is compared against Py_None and incref'd, but deleting it sets it to null, resulting in Py_INCREF(NULL)

CPython versions tested on:

3.14

Operating systems tested on:

macOS

Output from running 'python -VV' on the command line:

No response

Linked PRs

Activity

  1. added
    type-crashA hard crash of the interpreter, possibly with a core dump
    on May 12, 2026
  2. sepehr-rs commented on May 12, 2026

    @sepehr-rs
    Contributor

    Hi @lunixbochs, thanks for the clear report!
    I can work on a fix for this if nobody is already doing so.
    It may also make sense to add the topic-sqlite3 and stdlib labels.

  3. sharktide commented on May 12, 2026

    @sharktide
    Contributor

    Hi @lunixbochs, thanks for the clear report!
    I can work on a fix for this if nobody is already doing so.
    It may also make sense to add the topic-sqlite3 and stdlib labels.

    Sure! I don't think anyone is actively working on it, feel free to to correct me if I'm wrong :)

  4. added a commit that references this issue on Jun 2, 2026
  5. vstinner commented on Jun 2, 2026

    @vstinner
    Member

    With the fix, the example now fails with:

    Traceback (most recent call last):
      File "/home/vstinner/python/main/x.py", line 3, in <module>
        del db.row_factory
            ^^^^^^^^^^^^^^
    AttributeError: cannot delete row_factory attribute
    
  6. encukou commented on Jun 2, 2026

    @encukou
    Member

    Is there any reasonable user code that gets this error, and without the fix it didn't segfault [edit: or raise] later?

  7. added a commit that references this issue on Jun 2, 2026
  8. vstinner commented on Jun 2, 2026

    @vstinner
    Member

    Is there any reasonable user code that gets this error, and without the fix it didn't segfault later?

    I don't think that it's possible to do anything useful without row_factory or text_factory attribute.

    After del db.row_factory, any db.execute() call does crash. So it makes the connection unusable.

    For text_factory, any request returning a text column does fail with an internal error (SystemError):

    import sqlite3
    db = sqlite3.connect(":memory:")
    del db.text_factory
    row = db.execute("SELECT 'a'")
    print(row.fetchone())

    Output without this fix:

    Traceback (most recent call last):
      File "/home/vstinner/python/main/x.py", line 5, in <module>
        print(row.fetchone())
              ~~~~~~~~~~~~^^
    SystemError: null argument to internal routine
    
  9. added 2 commits that reference this issue on Jun 2, 2026
  10. added a commit that references this issue on Aug 21, 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

    Labels

    extension-modulesC modules in the Modules dirtopic-sqlite3type-crashA hard crash of the interpreter, possibly with a core dump

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions