Pexpect hangs on Solaris

I want to write down this bit of knowledge so that it can get sucked up into the all-encompassing brain that is Google. If you use pexpect on Solaris, and you notice that it hangs in spawn.__del__ as soon as the spawn object goes out of scope, then this may be of interest to you.

The problem is that pexpect calls close() in its __del__ method, in order to keep from leaving around zombie processes. This works fine as long as the program you're controlling is reading from stdin. But if, instead of controlling the program, you're merely reading from it, and it's not paying any attention to its stdin, then closing its TTY will not cause the program to exit. On Linux it does, but not on Solaris. So the symptom of the problem is that pexpect hangs when it goes out of scope, because its __del__ method calls close(), which calls os.waitpid() without a WNOHANG flag.

The way I have worked around this is to explicitly call close(wait=0) before the pexpect object goes out of scope. That way the TTY is already closed, so the automatic call to close() does nothing -- in particular, it doesn't hang.

Another issue with pexpect on Solaris is that expect(pexpect.EOF) sometimes hangs. So I replaced that with a loop that checks isalive() repeatedly, until the timeout expires. That seems to work fine.

Posted on July 16, 2004 01:30 PM
More testing articles

Comments

how to do ?

for i in xrange(300):
_ child = pexpect.spawn(xx)
_ child.expect(pexpect.EOF)
_ child.close(wait=0)
.
but not success..

.1: pt_chmod: fatal: /usr/lib/libc.so.1: Too many open files
Traceback (most recent call last):
File "/home1/users/casper/python/module/QM/ptest.py", line 7, in ?
File "pexpect.py", line 161, in __init__
File "pexpect.py", line 200, in __spawn
pexpect.ExceptionPexpect: 'Pexpect: pty.fork() failed: out of pty devices'

Posted by: casper at January 5, 2005 08:59 AM

Thanks for covering this. I was setting up a persistent ssh tunnel script to make a connection from a Solaris box today and ran into this problem. I've made sure to close all of my connections and so far, everything it working as expected.

Posted by: Sean Stoops at December 11, 2007 08:47 PM
Post a comment









Remember info?




Prove you're human. Type "human":