Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crash and refleaks when calling sqlite3.Cursor.__init__() more than once #75951

Closed
orenmn mannequin opened this issue Oct 12, 2017 · 5 comments
Closed

crash and refleaks when calling sqlite3.Cursor.__init__() more than once #75951

orenmn mannequin opened this issue Oct 12, 2017 · 5 comments
Labels
3.7 (EOL) end of life extension-modules C modules in the Modules dir type-crash A hard crash of the interpreter, possibly with a core dump

Comments

@orenmn
Copy link
Mannequin

orenmn mannequin commented Oct 12, 2017

BPO 31770
Nosy @vstinner, @orenmn
PRs
  • bpo-31770: Prevent a crash and refleaks when calling sqlite3.Cursor.__init__() more than once #3968
  • [3.6] bpo-31770: Prevent a crash and refleaks when calling sqlite3.Cursor.__init__() more than once (GH-3968) #4301
  • [2.7] bpo-31770: Prevent a crash and refleaks when calling sqlite3.Cursor.__init__() more than once (GH-3968) #4302
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2017-11-07.00:50:12.995>
    created_at = <Date 2017-10-12.08:18:36.858>
    labels = ['extension-modules', '3.7', 'type-crash']
    title = 'crash and refleaks when calling sqlite3.Cursor.__init__() more than once'
    updated_at = <Date 2017-11-07.00:50:12.994>
    user = 'https://github.com/orenmn'

    bugs.python.org fields:

    activity = <Date 2017-11-07.00:50:12.994>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2017-11-07.00:50:12.995>
    closer = 'vstinner'
    components = ['Extension Modules']
    creation = <Date 2017-10-12.08:18:36.858>
    creator = 'Oren Milman'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 31770
    keywords = ['patch']
    message_count = 5.0
    messages = ['304220', '305692', '305698', '305699', '305700']
    nosy_count = 2.0
    nosy_names = ['vstinner', 'Oren Milman']
    pr_nums = ['3968', '4301', '4302']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'crash'
    url = 'https://bugs.python.org/issue31770'
    versions = ['Python 2.7', 'Python 3.6', 'Python 3.7']

    @orenmn
    Copy link
    Mannequin Author

    orenmn mannequin commented Oct 12, 2017

    The following code crashes:
    import sqlite3
    import weakref
    def callback(*args):
    pass

    connection = sqlite3.connect(":memory:")
    cursor = sqlite3.Cursor(connection)
    ref = weakref.ref(cursor, callback)
    cursor.__init__(connection)
    del cursor
    del ref

    IIUC, this is because pysqlite_cursor_init() (in Modules/_sqlite/cursor.c) sets
    self->in_weakreflist to NULL, and thus corrupts the weakref list. Later,
    clear_weakref() (in Objects/weakrefobject.c) tries to remove a reference from
    the corrupted list, and crashes.

    In every other place (that i saw) where such a weakreflist field is used, it is
    set to NULL right after allocating the object (usually in new()), or just
    not set at all, e.g. in functools.partial.
    So since PyType_GenericNew() is the new() of sqlite3.Cursor, ISTM that the
    simplest solution is to not touch self->in_weakreflist at all in
    pysqlite_cursor_init().

    Also, the following code results in refleaks:
    import sys
    import sqlite3
    connection = sqlite3.connect(":memory:")
    cursor = sqlite3.Cursor(connection)
    refcount_before = sys.gettotalrefcount()
    cursor.__init__(connection)
    print(sys.gettotalrefcount() - refcount_before) # should be close to 0

    This is because pysqlite_cursor_init() doesn't decref before assigning to
    fields of self.

    I would open a PR to fix this soon.

    @orenmn orenmn mannequin added 3.7 (EOL) end of life extension-modules C modules in the Modules dir type-crash A hard crash of the interpreter, possibly with a core dump labels Oct 12, 2017
    @vstinner
    Copy link
    Member

    vstinner commented Nov 7, 2017

    New changeset e56ab74 by Victor Stinner (Oren Milman) in branch 'master':
    bpo-31770: Prevent a crash and refleaks when calling sqlite3.Cursor.__init__() more than once (bpo-3968)
    e56ab74

    @vstinner
    Copy link
    Member

    vstinner commented Nov 7, 2017

    New changeset 4b544aa by Victor Stinner (Miss Islington (bot)) in branch '2.7':
    bpo-31770: Prevent a crash and refleaks when calling sqlite3.Cursor.__init__() more than once (GH-3968) (bpo-4302)
    4b544aa

    @vstinner
    Copy link
    Member

    vstinner commented Nov 7, 2017

    New changeset 9684cf6 by Victor Stinner (Miss Islington (bot)) in branch '3.6':
    bpo-31770: Prevent a crash and refleaks when calling sqlite3.Cursor.__init__() more than once (GH-3968) (bpo-4301)
    9684cf6

    @vstinner
    Copy link
    Member

    vstinner commented Nov 7, 2017

    Thank you Oren Milman for your bug report and the bug fix! I merged your PR and backported it Python 3.6 and 2.7.

    @vstinner vstinner closed this as completed Nov 7, 2017
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life extension-modules C modules in the Modules dir type-crash A hard crash of the interpreter, possibly with a core dump
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant