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

Embedded python crashed on 4th run, if "ctypes" is used #51118

Closed
warlock mannequin opened this issue Sep 9, 2009 · 7 comments
Closed

Embedded python crashed on 4th run, if "ctypes" is used #51118

warlock mannequin opened this issue Sep 9, 2009 · 7 comments
Assignees
Labels
topic-ctypes type-crash A hard crash of the interpreter, possibly with a core dump

Comments

@warlock
Copy link
Mannequin

warlock mannequin commented Sep 9, 2009

BPO 6869
Nosy @theller
Files
  • fix_ctypes_crash.patch
  • 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 = 'https://github.com/theller'
    closed_at = <Date 2010-08-08.18:29:31.227>
    created_at = <Date 2009-09-09.08:31:13.900>
    labels = ['ctypes', 'type-crash']
    title = 'Embedded python crashed on 4th run, if "ctypes" is used'
    updated_at = <Date 2010-09-12.16:20:38.048>
    user = 'https://bugs.python.org/warlock'

    bugs.python.org fields:

    activity = <Date 2010-09-12.16:20:38.048>
    actor = 'ocean-city'
    assignee = 'theller'
    closed = True
    closed_date = <Date 2010-08-08.18:29:31.227>
    closer = 'theller'
    components = ['ctypes']
    creation = <Date 2009-09-09.08:31:13.900>
    creator = 'warlock'
    dependencies = []
    files = ['14936']
    hgrepos = []
    issue_num = 6869
    keywords = ['patch']
    message_count = 7.0
    messages = ['92444', '92449', '92555', '92880', '102369', '113297', '116211']
    nosy_count = 7.0
    nosy_names = ['theller', 'ocean-city', 'warlock', 'mkp', 'robince', 'nik.lutz@gmail.com', 'tanaga']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'patch review'
    status = 'closed'
    superseder = None
    type = 'crash'
    url = 'https://bugs.python.org/issue6869'
    versions = ['Python 3.1', 'Python 2.7', 'Python 3.2']

    @warlock
    Copy link
    Mannequin Author

    warlock mannequin commented Sep 9, 2009

    When embedding python from C, and importing "ctypes" module in embedded
    script, it always crashes on Py_Finalize() on 4th cycle.
    Tested with both PyRun_SimpleString(...) and PyRun_String(...).
    Platform: Windows XP
    IDE's: LabWindows/CVI 8.5 and Code::Blocks/gcc

    Code:
    --------------------------

    #include <stdio.h>
    #include <python.h>
    
    int main()
    {
        int i;
        for (i=0; i<10; i++)
        {
            printf("--- %d ---\n", i);
            Py_Initialize();
            PyRun_SimpleString("import ctypes");
            Py_Finalize();
        }
        return 0;
    }

    Output:
    --------------------------------
    --- 0 ---
    --- 1 ---
    --- 2 ---
    --- 3 ---

    Process returned -1073741819 (0xC0000005) execution time : 3.109 s
    Press any key to continue.
    --------------------------------

    @warlock warlock mannequin assigned theller Sep 9, 2009
    @warlock warlock mannequin added topic-ctypes type-crash A hard crash of the interpreter, possibly with a core dump labels Sep 9, 2009
    @warlock
    Copy link
    Mannequin Author

    warlock mannequin commented Sep 9, 2009

    Tested
       obj=PyImport_ImportModule("ctypes");
       Py_DECREF(obj);
    instead of PyRun_SimpleString(...) - same result

    @mkp
    Copy link
    Mannequin

    mkp mannequin commented Sep 12, 2009

    on python2.5 / mac os x 10.5.7:

    $ cat main.c
    #include <stdio.h>
    #include <python.h>
    
    int main()
    {
        int i;
        for (i=0; i<10; i++)
        {
            printf("--- %d ---\n", i);
            Py_Initialize();
            PyRun_SimpleString("import ctypes");
            Py_Finalize();
        }
        return 0;
    }
    $ gcc -I/usr/include/python2.5/ -L/usr/lib/python2.5/ -lpython main.c
    $ ./a.out 
    --- 0 

    --- 1 ---
    --- 2 ---
    --- 3 ---
    Assertion failed: (type->tp_flags & Py_TPFLAGS_HEAPTYPE), function
    type_dealloc, file Objects/typeobject.c, line 2148.
    Abort trap
    $ uname -mprsv
    Darwin 9.7.0 Darwin Kernel Version 9.7.0: Tue Mar 31 22:52:17 PDT 2009;
    root:xnu-1228.12.14~1/RELEASE_I386 i386 i386

    @ocean-city
    Copy link
    Mannequin

    ocean-city mannequin commented Sep 20, 2009

    I hope attached patch will fix this issue.

    @niklutzgmailcom
    Copy link
    Mannequin

    niklutzgmailcom mannequin commented Apr 5, 2010

    I can confirm that the patch fix this issue. I adapted the patch (variable names changed):

    5523a5524

        Py_INCREF(&Struct_Type);
    

    5529a5531

        Py_INCREF(&Union_Type);
    

    5535a5538

        Py_INCREF(&Pointer_Type);
    

    5541a5545

        Py_INCREF(&Array_Type);
    

    5547a5552

        Py_INCREF(&Simple_Type);
    

    5553a5559

        Py_INCREF(&CFuncPtr_Type); 
    

    Platform: Linux (kubuntu 9.10 x64)
    Python: 2.6.4-0ubuntu3

    @theller
    Copy link

    theller commented Aug 8, 2010

    Fixed in rev 83841 (py3k), rev 83842 (release31-maint), and rev 83843 (release27-maint).

    Thanks for the patch.

    @theller theller closed this as completed Aug 8, 2010
    @ocean-city
    Copy link
    Mannequin

    ocean-city mannequin commented Sep 12, 2010

    I found another refcount bug in Python3.x.
    Fixed in r84741(py3k), r84742(release31-maint).

    @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
    topic-ctypes type-crash A hard crash of the interpreter, possibly with a core dump
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant