Problem with signals in threads in Python

Andriy Pylypenko bamby at sippysoft.com
Thu Dec 11 06:43:04 PST 2008


Hello,

Almost a year ago I've created an issue in Python's bugtracker but the 
issue seems to be stuck since then so I'm sending the patch here in hope 
it could be included in ports collection.

In short there is a problem in the Python that can be seen under FreeBSD 
but not under  MacOSX or Linux. If a Python program started a thread in 
background then it can be impossible to interrupt the program with 
Ctrl-C. Here is an example of such script:

    some_time = 6000000 # seconds

    class MyThread(Thread):
        def run(self):
            while (True):
                time.sleep(some_time)

    t = MyThread()
    t.setDaemon(True)
    t.start()
    while(True):
        select.select(None, None, None, some_time)


The reason why this problem is FreeBSD specific is that the FreeBSD has 
different order of processing of signals.

The  signal handler under FreeBSD fires up in the context of the last 
started thread while under Linux signals are processed in the main 
thread. It should be noted that both behaviors are standards conformant. 
So to ensure that signal handler runs in the correct thread context the 
developer should use pthread_sigmask(). Initially the Python contained 
this code but starting from Python 2.4 it was thrown away.

The attached patch restores the code that ensures correct execution 
context of the signal handler code.

The detailed description of the issue can be seen here:

    http://bugs.python.org/issue1975

-- 
  Kind regards,
  Andriy Pylypenko

-------------- next part --------------
A non-text attachment was scrubbed...
Name: pthread_sig.diff
Type: text/x-patch
Size: 883 bytes
Desc: not available
Url : http://lists.freebsd.org/pipermail/freebsd-python/attachments/20081211/bf9257f9/pthread_sig.bin


More information about the freebsd-python mailing list