Thread not working correctly under FreeBSD

Ralf Schmitt ralf at brainbot.com
Thu Oct 23 15:37:50 EDT 2003


Frederic Jolliton <comp.lang.python at fred.jolliton.com> writes:

> Hi,
>
> Here is a small Python program that show curious behavior under
> FreeBSD when using threads:
>
> -=-=-=-
> $ cat test.py
> import os, sys, time, thread
> if len( sys.argv ) < 2 :
>         thread.start_new_thread(lambda : os.system("python test.py x"), ())
>         time.sleep( 5 )
> else :
>         os.system('echo "Hello, World!"')
>         print '~system'
> -=-=-=-
>
> This program run more or less the following:
>
> python test.py
> `- shell -c 'python test.py x'
>    `- python test.py x
>       `- shell -c 'echo "Hello World!"'
>
> When run without argument from Linux (Gentoo, kernel 2.4.21, Python
> 2.2.3), the output is:
>
> -=-=-=-
> Hello, World!
> ~system
> <5 seconds pause>
> -=-=-=-
>
> But, from FreeBSD, the ouput is:
>
> -=-=-=-
> Hello, World!
> <5 seconds pause>
> -=-=-=-
>
> and, as shown, os.system('echo ...') never return ! (However, the
> shell running 'echo' is correctly terminated.)
>
> Tested with the following FreeBSD version:
>
>  - 5.0-RELEASE (gcc 3.2.1) Python 2.2.2 and Python 2.3.2
>  - 4.8-STABLE (gcc 2.95)
>
> Bug, feature or misuse of the thread module ?
>

I'd call it a bug. We had the same problem here at work. I attached a
short c-module, which solves that problem (at least it did for us).
When starting a new thread, just call 'fixsignals.fixsignals()' and
afterwards os.system works as expected (and better do this on freebsd
only).

- Ralf



setup.py:
=============================
#!/usr/bin/env python

from distutils.core import setup, Extension

setup(name = "fixsignals",
      description = "fix signal handling",
      ext_modules = [Extension("fixsignals", ["fixsignals.c"])])


fixsignals.c:
=============================
#include <stdlib.h>
#include <Python.h>
#include <signal.h>

static PyObject * fixsignals(PyObject *ignore, PyObject *args)
{
 	sigset_t oldmask, newmask;
	sigemptyset(&newmask);
	sigaddset (&newmask, SIGPIPE);
	sigaddset (&newmask, SIGINT);
	sigprocmask(SIG_SETMASK, &newmask, &oldmask);
	return Py_BuildValue("");
}

static PyMethodDef py_fixsignals_methods[] = {
	{"fixsignals",		(PyCFunction)fixsignals,			METH_VARARGS|METH_KEYWORDS,
	 "allow all signals except SIGPIPE and SIGINT"},
	{ NULL,    NULL }
};


void initfixsignals(void)
{
	/*PyObject *m =*/ Py_InitModule("fixsignals", py_fixsignals_methods);
}



-- 
brainbot technologies ag
boppstrasse 64 . 55118 mainz . germany
fon +49 6131 211639-1 . fax +49 6131 211639-2
http://brainbot.com/  mailto:ralf at brainbot.com




More information about the Python-list mailing list