From owner-freebsd-python@FreeBSD.ORG Thu Dec 11 14:43:04 2008 Return-Path: Delivered-To: python@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 4D70E1065675 for ; Thu, 11 Dec 2008 14:43:04 +0000 (UTC) (envelope-from bamby@sippysoft.com) Received: from sippysoft.com (gk1.360sip.com [72.236.70.240]) by mx1.freebsd.org (Postfix) with ESMTP id ECD098FC12 for ; Thu, 11 Dec 2008 14:43:03 +0000 (UTC) (envelope-from bamby@sippysoft.com) Received: from [192.168.0.102] (212.9.228.2.iptelecom.net.ua [212.9.228.2]) (authenticated bits=0) by sippysoft.com (8.13.8/8.13.8) with ESMTP id mBBEQi2k048947 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Thu, 11 Dec 2008 06:26:48 -0800 (PST) (envelope-from bamby@sippysoft.com) Message-ID: <4941231E.4050408@sippysoft.com> Date: Thu, 11 Dec 2008 16:26:38 +0200 From: Andriy Pylypenko User-Agent: Thunderbird 2.0.0.18 (X11/20081125) MIME-Version: 1.0 To: python@FreeBSD.org Content-Type: multipart/mixed; boundary="------------040608060000070008040404" Cc: Subject: Problem with signals in threads in Python X-BeenThere: freebsd-python@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: FreeBSD-specific Python issues List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 11 Dec 2008 14:43:04 -0000 This is a multi-part message in MIME format. --------------040608060000070008040404 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit 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 --------------040608060000070008040404 Content-Type: text/x-patch; name="pthread_sig.diff" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="pthread_sig.diff" Index: Python/thread_pthread.h =================================================================== --- Python/thread_pthread.h (revision 60447) +++ Python/thread_pthread.h (working copy) @@ -149,6 +149,7 @@ { pthread_t th; int status; + sigset_t set, oset; #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED) pthread_attr_t attrs; #endif @@ -178,6 +179,8 @@ pthread_attr_setscope(&attrs, PTHREAD_SCOPE_SYSTEM); #endif + sigfillset(&set); + SET_THREAD_SIGMASK(SIG_BLOCK, &set, &oset); status = pthread_create(&th, #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED) &attrs, @@ -187,6 +190,7 @@ (void* (*)(void *))func, (void *)arg ); + SET_THREAD_SIGMASK(SIG_SETMASK, &oset, NULL); #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED) pthread_attr_destroy(&attrs); --------------040608060000070008040404--