FAQ
Hello,

My code sends a pointer to a Python function to a C library, using
ctypes module. When my program restarts, after a crash or normal
exit... it cannot start again without sigfaulting

What is the proper way around this problem? other than adding code to
delete the cache files?

Thanks a lot,
Mohamed.

Search Discussions

  • Steven D'Aprano at May 6, 2009 at 4:18 am

    On Tue, 05 May 2009 23:43:25 -0400, Mohamed Lrhazi wrote:

    Hello,

    My code sends a pointer to a Python function to a C library, using
    ctypes module. When my program restarts, after a crash or normal exit...
    it cannot start again without sigfaulting

    What is the proper way around this problem? other than adding code to
    delete the cache files?

    Surely the proper way to solve this is to find the cause of the segfault,
    and fix that bug.



    --
    Steven
  • Gabriel Genellina at May 6, 2009 at 4:58 am
    En Wed, 06 May 2009 00:43:25 -0300, Mohamed Lrhazi <lrhazi at gmail.com>
    escribi?:
    My code sends a pointer to a Python function to a C library, using
    ctypes module. When my program restarts, after a crash or normal
    exit... it cannot start again without sigfaulting
    Do you mean that, if you delete the .pyc files your program runs properly,
    but if you keep the .pyc files your program crashes?
    That's very strange...
    What is the proper way around this problem? other than adding code to
    delete the cache files?
    There is no "proper way around" the problem; it should not exist in the
    first place!

    Have you read the note at the end of the "Callback" section in the ctypes
    documentation?
    """Important note for callback functions:

    Make sure you keep references to CFUNCTYPE objects as long as they are
    used from C code. ctypes doesn't, and if you don't, they may be garbage
    collected, crashing your program when a callback is made."""

    There is a ctypes-specific list:
    https://lists.sourceforge.net/lists/listinfo/ctypes-users

    --
    Gabriel Genellina
  • Dave Angel at May 6, 2009 at 8:57 am

    Gabriel Genellina wrote:
    <div class="moz-text-flowed" style="font-family: -moz-fixed">En Wed,
    06 May 2009 00:43:25 -0300, Mohamed Lrhazi <lrhazi at gmail.com> escribi?:
    My code sends a pointer to a Python function to a C library, using
    ctypes module. When my program restarts, after a crash or normal
    exit... it cannot start again without sigfaulting
    Do you mean that, if you delete the .pyc files your program runs
    properly, but if you keep the .pyc files your program crashes?
    That's very strange...
    What is the proper way around this problem? other than adding code to
    delete the cache files?
    There is no "proper way around" the problem; it should not exist in
    the first place!

    Have you read the note at the end of the "Callback" section in the
    ctypes documentation?
    """Important note for callback functions:

    Make sure you keep references to CFUNCTYPE objects as long as they are
    used from C code. ctypes doesn't, and if you don't, they may be
    garbage collected, crashing your program when a callback is made."""

    There is a ctypes-specific list:
    https://lists.sourceforge.net/lists/listinfo/ctypes-users
    1) Is it true (as Gabriel asks) that deleting the .pyc files solves the
    problem?
    2) If so, have you investigated to see which one of them gets
    corrupted? This isn't necessarily the problem, it could also be timing,
    or related to the order of the memory allocation pool.
    3) When you get the segment fault, what does you C debugger show?
    What's happening at the time of the crash?
    4) Is either the C code or the Python code multithreaded?
    5) Are you running it under a Python debugger?
  • Scott David Daniels at May 6, 2009 at 2:57 pm

    Dave Angel wrote:
    Gabriel Genellina wrote: ...
    06 May 2009 00:43:25 -0300, Mohamed Lrhazi <lrhazi at gmail.com> escribi?:
    My code sends a pointer to a Python function to a C library, using
    ctypes module. When my program restarts, after a crash or normal
    exit... it cannot start again without sigfaulting
    Do you mean that, if you delete the .pyc files your program runs
    properly, but if you keep the .pyc files your program crashes?
    That's very strange...
    1) Is it true (as Gabriel asks) that deleting the .pyc files solves the
    problem?
    2) If so, have you investigated to see which one of them gets
    corrupted? This isn't necessarily the problem, it could also be timing,
    or related to the order of the memory allocation pool.
    3) When you get the segment fault, what does you C debugger show?
    What's happening at the time of the crash?
    4) Is either the C code or the Python code multithreaded?
    5) Are you running it under a Python debugger?
    Could this have to do with a manifest floating point constant
    being a "Not A Number" or "Infinity"? That is the last .pyc-
    related problem I know about.

    To the original poster:
    Please give a _lot_ more detail about the full Python version,
    OS type and version, and so on. Otherwise, it becomes
    tempting to say, "that is too bad, sorry you have troubles."
    One way to isolate the problem (if it is, indeed .pyc-loading
    related) is to delete individual .pyc files until you find the
    suspect, then cut that one in pieces and re-isolate until you
    have a small bit of code with the issue. This is why we point
    people to smart-questions(*) so often.

    (*) http://www.catb.org/~esr/faqs/smart-questions.html

    --Scott David Daniels
    Scott.Daniels at Acm.Org

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedMay 6, '09 at 3:43a
activeMay 6, '09 at 2:57p
posts5
users5
websitepython.org

People

Translate

site design / logo © 2022 Grokbase