Thread not working correctly under FreeBSD

F

Frederic Jolliton

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 ?

Regards,
 
F

Frederic Jolliton

Frederic Jolliton said:
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 ?

Just tested.. With FreeBSD 5.1-RELEASE and Python 2.3.2, all is
working fine !
 
R

Ralf Schmitt

Frederic Jolliton said:
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);
}
 
A

Andrew MacIntyre

Just tested.. With FreeBSD 5.1-RELEASE and Python 2.3.2, all is
working fine !

IME FreeBSD 4.x's pthreads support, while stable, has some interesting
corner cases which are progressively being dealt with in 5.x.

5.0 was very much a test release, and nobody should still be using it.
5.1 is quite usable as a workstation & light duty server, but I hear it
still suffers under heavy load. There has been no -stable release from
the 5.x (aka current) branch yet; the latest I hear is that -stable won't
arrive until 5.3.
 
F

Frederic Jolliton

Frederic Jolliton said:
Here is a small Python program that show curious behavior under
FreeBSD when using threads: [...]
Bug, feature or misuse of the thread module ?
[...]

Ralf Schmitt said:
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).
fixsignals.c:
[...]

Thanks you very much for your reply.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top