On 9/2/06, Nick Maclaren <[EMAIL PROTECTED]> wrote:
> According to [1], all python needs to do to avoid this problem is
> block all signals in all but the main thread; then we can guarantee
> signal handlers are always called from the main thread, and pygtk
> doesn't need a timeout.

1) That page is password protected, so I can't see what it says, and
am disinclined to register myself to yet another such site.

 Oh, sorry, here's the comment:

(coment by Arjan van de Ven):
| afaik the kernel only sends signals to threads that don't have them blocked.
| If python doesn't want anyone but the main thread to get signals, it
should just
| block signals on all but the main thread and then by nature, all
signals will go
| to the main thread....


2) No way, Jose, anyway.  The POSIX signal handling model was broken
beyond redemption, even before threading was added, and the combination
is evil almost beyond belief.  That procedure is good practice, yes,
but that is NOT all that you have to do - it may be all that you CAN
do, but that is not the same.

Nope.  Sorry, but you can't solve a broken design by adding interfaces.

 Well, Python has a broken design too; it postpones tasks and expects
to magically regain control in order to finish the job.  That often
doesn't happen!


>   But I would really prefer the first alternative, as it could be
> fixed within python 2.5; no need to wait for 2.6.

It clearly should be done, assuming that Python's model is that it
doesn't want to get involved with subthread signalling (and I really,
but REALLY, recommend not doing so).  The best that can be done is to
say that all signal handling is the business of the main thread and
that, when the system bypasses that, all bets are off.

 Python is halfway there; it assumes signals are to be handled in the
main thread.  However, it _catches_ them in any thread, sets a flag,
and just waits for the next opportunity when it runs again in the main
thread.  It is precisely this "split handling" of signals that is
failing now.

 Anyway, attached a patch that should fix the problem in posix
threads systems, in case anyone wants to review.

 Cheers.
Index: Modules/threadmodule.c
===================================================================
--- Modules/threadmodule.c	(revision 51687)
+++ Modules/threadmodule.c	(working copy)
@@ -12,6 +12,17 @@
 
 #include "pythread.h"
 
+#if defined(_POSIX_THREADS) && defined(HAVE_PTHREAD_SIGMASK) && defined(HAVE_SIGNAL_H)
+#  include <pthread.h>
+#  include <signal.h>
+#  if defined(HAVE_BROKEN_PTHREAD_SIGMASK)
+#    define SET_THREAD_SIGMASK sigprocmask
+#  else
+#    define SET_THREAD_SIGMASK pthread_sigmask
+#  endif
+#endif
+
+
 static PyObject *ThreadError;
 
 
@@ -417,6 +428,15 @@
 	PyThreadState *tstate;
 	PyObject *res;
 
+          /* We want to block signals from all non-main threads, so
+           that we can guarantee they arrive always at the main
+           thread, where we can handle them */
+#ifdef SET_THREAD_SIGMASK
+        sigset_t set;
+        sigfillset(&set);
+        SET_THREAD_SIGMASK(SIG_BLOCK, &set, NULL);
+#endif
+
 	tstate = PyThreadState_New(boot->interp);
 
 	PyEval_AcquireThread(tstate);
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to