Problem with spawning an external process

N

Nico Kruger

I want to execute a command (in this case, and it seems to be
significant, a Java program) in a thread in Python. When I execute the
java binary in the main python thread, everything runs correctly. But
when I try and execute java in a thread, java segfaults. I am using
Python 2.3.3 and trying to run the java binary from the 1.4.2 SDK from
Sun.

I have tried executing some arbitrary C program that I wrote, and it
works fine in the main thread and the started thread. The problem seems
to be specific to the Java binary.

Here is the example code:

--- START CODE ---
import os
import popen2
import time
import select
import threading

# Change these to suit your system
PATH = '/home/nico/j2sdk1.4.2/bin/java'
#PATH = '/home/nico/j2sdk1.4.2/bin/java -invalid_arg'

def get_ret(status):
signal = status & 0xff
if signal == 0:
retcode = status >> 8
else:
retcode = 0
return signal,retcode

print "In main thread"
# Using spawn
pid = os.spawnl(os.P_NOWAIT,PATH,PATH)
ret = os.waitpid(pid,0)
print "PID: %i signal: %i return code: %i" %
(ret[0],get_ret(ret[1])[0],get_ret(ret[1])[1])

class TestThread(threading.Thread):
def run(self):
# Using spawn
pid = os.spawnl(os.P_NOWAIT,PATH,PATH)
ret = os.waitpid(pid,0)
print "PID: %i signal: %i return code: %i" %
(ret[0],get_ret(ret[1])[0],get_ret(ret[1])[1])

print "In Thread"
TestThread().start()

print "Waiting..."
time.sleep(2)
print "...Finished"
---- END CODE ----

Here is the output that I get on my machine:
[nico@ script]$ python testcrash2.py
In main thread
<JAVA USAGE INFO STRIPPED>
PID: 32107 signal: 0 return code: 1
In Thread
PID: 32116 signal: 11 return code: 0


You will notice that in the main thread, the program executes correctly
(return code 1, signal 0). When the command is executed in the thread,
it segfaults (signal 11). The second PATH= line calls Java with an
invalid argument. In this case, it does not crash in the thread.

I have tried a fork() and then os.execv in the thread as well, and get
the same behaviour.

I would appreciate it if someone with both Java and Python could try and
run this sample and report back. Any suggestions would be welcome, as
this is quite the showstopper for me at the moment. I am sure I am
missing something totally obvious...

Thanks in advance,
Nico.


--
NetSys International (Pty) Ltd.
Tel : +27 12 349 2056
Fax : +27 12 349 2757
Web : http://www.netsys.co.za
P.O. Box 35798, Menlo Park, 0102, South Africa
----

The information contained in this communication is confidential and may be
legally privileged. It is solely for use of the individual or entity to whom
is addressed and others authorised to receive it. If you are not the intended
recipient you are hereby notified that any disclosure, copying, distribution
or taking of any action in reliance on the contents of this information is
strictly prohibited and may be unlawful.

This Message has been scanned for viruses and dangerous content by the NetSys
International Mail Scanner and is believed to be clean.
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top