From: John Carter Date: 2004-06-24T06:49:46+09:00 Subject: Re: PTY.spawn error-prone? On Thu, 24 Jun 2004, Hal Fulton wrote: > Ben Giddings wrote: >> Hal Fulton wrote: >> >>> I've been playing with PTY and it seems to me that this >>> implementation is exceptionally prone to errors. Let me elaborate a little, since I wrote this comments below.... > Allow me to quote from the rexpect docs on rubyforge: > >> 19 # >> 20 # Suppose you tell it to spawn off a command that doesn't >> exist.... >> 21 # What do you think will happen? >> 22 # 1) You get a sane error code? >> 23 # 2) It raises an appropriate Exception? >> 24 # 3) Returns quite happily and at some random time later, >> throws a >> 25 # PTY::ChildExited exception? >> 26 # >> 27 # Yup. Option 3) is what happens. >> 28 # Evil isn't it?! Firstly, most people _won't_ notice this as a problem. If you working on an unloaded system, the PTY::ChildExited exception happens immediately. It is when you are driving the system to the edge by using something like rexpect to drive hundreds of processes that the problem hits. Looking deep into the Pty code (cvs latest) we have... a) fork() b) if child c) Do Lots of stuff d) exec "sh" e) shell does PATH lookup. f) PATH lookup fails g) shell exits "bad command" h) ruby interpretor receives a "SIGCHLD". Now from the moment we fork(), it is up to the OS scheduler, depending on the current load, device(hard drive, keyboard,...) activity, phase of moon, etc, etc how long it takes to get from a) to h). Thus several events can get overloaded _indistinguishably_ onto that one SIGCHLD.... * Exec'd program couldn't be started. (Command not found, permission denied etc.) * Exec'd program failed to start. (Not a valid program) * Exec'd program started and died. * Exec'd program, started, did it's thing correctly, and exited before you noticed. ie. In a sense the problem is not with Pty, it is with the notion of invoking 'sh' to do our PATH lookups, IO redirection and command line globbing. Then we could get much more sensible exceptions coming back than just PTY::ChildExited I have said it before in this forum, we need to be able to do what "system()" and backtick and "open" and "exec" do _without_ invoking "sh", that twenty year old downgrade to ruby. John Carter Phone : (64)(3) 358 6639 Tait Electronics Fax : (64)(3) 359 4632 PO Box 1645 Christchurch Email : john.carter@tait.co.nz New Zealand The universe is absolutely plastered with the dashed lines exactly one space long.