Problem with spawning an external process

2006-04-12 Thread 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.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problem with spawning an external process

2006-04-12 Thread Nico Kruger
Apologies, this is on a Fedora Core 2 system.

On Wed, 2006-04-12 at 11:27, Nico Kruger wrote:
 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.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problem with spawning an external process

2006-04-12 Thread Nico Kruger
Daniel, you are correct, it is not that good news for me :)

But anyway, thanks to your responses, I installed Python 2.4.3 and it is
working on my machine now as well. We were overdue for an upgrade to our
Python environment anyways, so I think this is the final incentive to
upgrade.

Although, I will download 2.3.5 now and get back to you with the status.
Would be interesting to know when exactly this problem was fixed, maybe
it would help to know what is causing the problem? It really is a
strange one.

Again, thanks for your effort!


On Wed, 2006-04-12 at 12:41, Daniel Nogradi wrote:
  Daniel, thanks for your input. What version of the JDK/JRE and Python
  are you using?
 
 So the previous test was on python 2.4, java 1.4.2, suse 9.3 but now I
 ran it on python 2.3.5, java 1.4.2, gentoo 1.4.16 and your code still
 does what it supposed to do. I'm not sure if that is good new for you
 though :)



--
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.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problem with spawning an external process

2006-04-12 Thread Nico Kruger
Not working with Python 2.3.5 here on Fedora Core 2 :(. Oh well, at
least it's working on 2.4. 

Must be specific to something in the combination of Fedora Core 2 and
Python 2.3.x, that is all I can think of.

On Wed, 2006-04-12 at 12:55, Nico Kruger wrote:
 Daniel, you are correct, it is not that good news for me :)
 
 But anyway, thanks to your responses, I installed Python 2.4.3 and it is
 working on my machine now as well. We were overdue for an upgrade to our
 Python environment anyways, so I think this is the final incentive to
 upgrade.
 
 Although, I will download 2.3.5 now and get back to you with the status.
 Would be interesting to know when exactly this problem was fixed, maybe
 it would help to know what is causing the problem? It really is a
 strange one.
 
 Again, thanks for your effort!
 
 



--
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.

-- 
http://mail.python.org/mailman/listinfo/python-list