Can not catch PTY::ChildExited exception

  • Thread starter Denis Berezhnoy
  • Start date
D

Denis Berezhnoy

Hi guys!

I have problem that I can not catch PTY::ChildExited exception. Here is
the code:

require 'pty'

cmd = "/home3/cman/work/product/integrator/test/syb2/OCS-15_0/bin/isql
-Uuser2 -Puser2 -Scman2"

begin

pipe_read, pipe_write, pid = PTY.spawn(cmd)

rescue PTY::ChildExited => msg
puts "Caught child exited exception!"
rescue => msg
puts "Caught exception!"
end

sleep 10

printf "TEST END\n"

When I run it I have the following:

pty_test.rb:7: pty - exited: 1278 (PTY::ChildExited)


I am wondering why exception is not caught? What is more when I remove
sleep operator then there is no exception at all. Script works and
prints TEST END.

My environment:

ruby 1.8.7 (2008-06-20 patchlevel 22) [x86_64-linux]
Linux 2.6.24-23-server #1 SMP Mon Jan 26 01:36:05 UTC 2009 x86_64
GNU/Linux

What I am doing wrong?

Best regards.
Denis
 
D

Denis Berezhnoy

Yukihiro said:
Hi,

In message "Re: Can not catch PTY::ChildExited exception"
on Wed, 9 Sep 2009 15:14:09 +0900, Denis Berezhnoy

|I have problem that I can not catch PTY::ChildExited exception. Here is
|the code:

The exception was raised when the child process terminated
(asynchronously), so you need to wrap not only spawn but everything.

matz.

Hello Matsumoto-san,

Thank you very much for clarifications! I tried to wrap around whole
code with begin rescue block and I can catch ChildExited exception. So
now I have code like this:

require 'pty'

cmd = "/home3/cman/work/product/integrator/test/syb2/OCS-15_0/bin/isql
-Uuser2 -Puser2 -Scman2"

begin

pipe_read, pipe_write, pid = PTY.spawn(cmd)

sleep 10

printf "TEST END\n"

rescue PTY::ChildExited => msg
puts "Caught child exited exception!"
rescue => msg
puts "Caught exception!"
rescue => e
puts e.msg
puts e.type
end


But now the problem is that ChildExited exception can occur at any
moment after spawn and interrupt program in random place. In my test
above I never get TEST END printed.

How can I handle this in right way?

Best regards,
Denis
 
E

Ehsanul Hoque

Well=2C I'm sure I have nowhere as good an idea of PTY.spawn() as Matsumoto=
does=2C but let me try to explain what your code is doing=2C and how to ge=
t your desired result.

First=2C you might want to consider using other methods for what you're doi=
ng. You probably don't really need a pseudo terminal=2C which is what PTY g=
ives you=2C and you can use more well documented methods. These two links g=
ive a good overview of some of the other ways you can start subprocesses in=
ruby:
http://devver.net/blog/2009/06/a-dozen-or-so-ways-to-start-sub-processes-in=
-ruby-part-1/
http://devver.net/blog/2009/07/a-dozen-or-so-ways-to-start-sub-processes-in=
-ruby-part-2/

If you still think you really need to use a pseudo terminal=2C then read on=
 
D

Denis Berezhnoy

Yukihiro said:
Hi,

In message "Re: Can not catch PTY::ChildExited exception"
on Fri, 11 Sep 2009 09:56:15 +0900, Denis Berezhnoy

|But now the problem is that ChildExited exception can occur at any
|moment after spawn and interrupt program in random place. In my test
|above I never get TEST END printed.
|
|How can I handle this in right way?

Define the "right way" first.

I admit asynchronous exception is a bad design choice. pty bundled
with 1.9 has changed to use polling via pty.check method.

matz.

Hi,

Saying about "right way" I meant that may be there is a standard Ruby
mechanism to handle such async exceptions besides begin rescue
operators. I am newbie in Ruby so I am not sure about its capabilities.

But you answered my question. This is a flaw in design of Ruby PTY
component and we can not avoid interrupting code by child exited
exception.

Anyway thanks for clarification. I will look how I can patch PTY in Ruby
to eliminate generating PTY::ChildExited exception at all.

Best regards,
Denis
 
D

Denis Berezhnoy

Ehsanul said:
Well, I'm sure I have nowhere as good an idea of PTY.spawn() as
Matsumoto does, but let me try to explain what your code is doing, and
how to get your desired result.

First, you might want to consider using other methods for what you're
doing. You probably don't really need a pseudo terminal, which is what
PTY gives you, and you can use more well documented methods.

Hi Ehsan,

Thanks for detailed explanation. The reason why I use PTY is because I
need to interact with application that does not flush its output to
pipe. So when I use IO.popen or IO.pipe there is no output.

PTY.spawn() solves this problem so I use it intentionally.

Best regards,
Denis
 
D

Denis Berezhnoy

Denis said:
Hi Ehsan,

Thanks for detailed explanation. The reason why I use PTY is because I
need to interact with application that does not flush its output to
pipe. So when I use IO.popen or IO.pipe there is no output.

PTY.spawn() solves this problem so I use it intentionally.

Best regards,
Denis

Hi Ehsan,

One more question. Can I use Process.wait to wait for termination of
the process which was run by PTY.spawn?

Best regards,
Denis
 
E

Ehsanul Hoque

Denis=2C

That's a good reason=2C exactly the same reason I needed to find out so muc=
h about PTY in fact.=20

As for Process.wait=2C as long as you have the pid=2C it should work. I tri=
ed it out in irb now=2C and it seems to work. But you will get the child ex=
ited exception still. I hadn't thought of this in my explanation=2C it's de=
finitely a better way to wait for the process to finish compared to my "t.j=
oin". Here's what I tried in irb:

def test
a=2Cb=2Cpid =3D PTY.spawn("sleep 5")
Process.wait(pid)
end
test
Date: Fri=2C 11 Sep 2009 17:12:27 +0900
From: (e-mail address removed)
Subject: Re: Can not catch PTY::ChildExited exception
To: (e-mail address removed)
=20

=20
Hi Ehsan=2C
=20
One more question. Can I use Process.wait to wait for termination of=20
the process which was run by PTY.spawn?
=20
Best regards=2C
Denis
--=20
Posted via http://www.ruby-forum.com/.
=20

_________________________________________________________________
Get back to school stuff for them and cashback for you.
http://www.bing.com/cashback?form=3DMSHYCB&publ=3DWLHMTAG&crea=3DTEXT_MSHYC=
B_BackToSchool_Cashback_BTSCashback_1x1=
 

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

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top