This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Andre Merzky
Recipients Andre Merzky
Date 2016-08-29.22:47:38
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1472510858.82.0.884835264582.issue27889@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2016-08-29 22:47:38Andre Merzkysetrecipients: + Andre Merzky
2016-08-29 22:47:38Andre Merzkysetmessageid: <1472510858.82.0.884835264582.issue27889@psf.upfronthosting.co.za>
2016-08-29 22:47:38Andre Merzkylinkissue27889 messages
2016-08-29 22:47:38Andre Merzkycreate