[New-bugs-announce] [issue27889] ctypes interfers with signal handling

Andre Merzky report at bugs.python.org
Mon Aug 29 18:47:38 EDT 2016


New submission from Andre Merzky:

Summary:  A thread uses signals to the MainThread to signal an abnormal condition.  The main thread is expected to get that signal, invoke the signal handler, and raise an exception.  In combination with 'ctypes' calls, that does not happen.


Consider the following code:

------------------------------------------------
#!/usr/bin/env python

import multiprocessing as mp
import threading       as mt
import signal
import time
import os

# from uuid.py line 400
import ctypes, ctypes.util
lib = ctypes.CDLL(ctypes.util.find_library('uuid'))

def sigusr2_handler(signum, frame):
    raise RuntimeError('caught sigusr2')
signal.signal(signal.SIGUSR2, sigusr2_handler)

def sub(pid):
    time.sleep(1)
    os.kill(pid, signal.SIGUSR2)

try:
  # p = mp.Process(target=sub, args=[os.getpid()])
  # p.start()
    t = mt.Thread(target=sub, args=[os.getpid()])
    t.start()
    time.sleep(3)
except Exception as e:
    print 'except: %s' % e
else:
    print 'unexcepted'
finally:
  # p.join()
    t.join()
------------------------------------------------

With Python 2.7 I would expect the output:
------------------------------------------------
except: caught sigusr2
------------------------------------------------

but I get:
------------------------------------------------
Traceback (most recent call last):
  File "./bug.py", line 29, in <module>
    print 'unexcepted'
  File "./bug.py", line 13, in sigusr2_handler
    raise RuntimeError('caught sigusr2')
  File "./bug.py", line 29, in <module>
    print 'unexcepted'
  File "./bug.py", line 13, in sigusr2_handler
    raise RuntimeError('caught sigusr2')
RuntimeError: caught sigusr2
------------------------------------------------


most of the time.  The problem only happens when the 'ctypes.CDLL' line is enabled -- commenting it out results in the expected behavior.  That line is copied from uuid.py -- importing uuid.py triggers the same unexpected behavior, which is ultimately why I am stuck.

Note that the problem only occurs when a *thread* sends the signal -- it does *not* happen if the signal is sent by the main thread or by a different process (switch to the multiprocessing code path for confirmation).

Interestingly, the problem also disappears when a 'print' statement is added after the 'time.sleep(3)', which may (or may not) indicate a timing issue.

I would welcome any suggestion on how to further triage this.

----------
components: ctypes
messages: 273889
nosy: Andre Merzky
priority: normal
severity: normal
status: open
title: ctypes interfers with signal handling
type: behavior
versions: Python 2.7

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue27889>
_______________________________________


More information about the New-bugs-announce mailing list