#29 Race condition makes telnet session echo everything

open
nobody
None
5
2010-06-30
2010-06-30
Gary Shi
No

I use pexpect to spawn a telnet session and perform some auto-login. It's strange that if child process runs before the parent, I see all telnet inputs echo back (include the password), but after I add some order-control logic, that behavior just disappears:

add a method to pexpect.spawn:

def __continue_handler(self, sig, data):
self.continue_flag = True

within _spawn, add these before calling pty.fork:

self.continue_flag = False
signal.signal(signal.SIGUSR1, self.__continue_handler)

add this just when the child process starts:

while not self.continue_flag: time.sleep(1)
signal.signal(signal.SIGUSR1, signal.SIG_DFL)

and these before parent's _spawn exits:

time.sleep(0.1)
os.kill(self.pid, signal.SIGUSR1)
signal.signal(signal.SIGUSR1, signal.SIG_DFL)

I don't know why but this really works.

Discussion


Log in to post a comment.