fork with exit [some integer] seems to not change exitstatus in Process.wait2, ruby 2.2.4

Happy New Year ruby-talk.

I'm attempting to retrieve the exit status of child processes. So far, no
matter how the child process exists, wait2 status only has a 1 for
exitstatus.

I'm also seeing "unexpected throw" messages. Perhaps I'm doing something
that is causing the unexpected throw, which is causing the exit status to
be 1.

ruby:
    interpreter: "ruby"
    version: "2.4.4p296"
    date: "2018-03-28"
    platform: "x86_64-darwin17"
    patchlevel: "2018-03-28 revision 63013"
    full_version: "ruby 2.4.4p296 (2018-03-28 revision 63013)
[x86_64-darwin17]"

OSX: 10.13.6 (17G4015)

sample code:

pid = fork do
  sleep 2
  exit 555
end

returned_pid, status = Process.wait2

puts "pid: #{pid}, returned_pid: #{returned_pid}, status:
#{status.inspect}, exitstatus: #{status.exitstatus}"

pid = fork do
  sleep 2
  exit 0
end

returned_pid, status = Process.wait2

puts "pid: #{pid}, returned_pid: #{returned_pid}, status:
#{status.inspect}, exitstatus: #{status.exitstatus}"

Session:

2.4.4 :001 > pid = fork do

2.4.4 :002 > sleep 2
2.4.4 :003?> exit 555
2.4.4 :004?> end
=> 91187
2.4.4 :005 >
2.4.4 :006 > returned_pid, status = Process.wait2
(irb):1:in `fork': unexpected throw
=> [91187, #<Process::Status: pid 91187 exit 1>]
2.4.4 :007 >
2.4.4 :008 > puts "pid: #{pid}, returned_pid: #{returned_pid},
status: #{status.inspect}, exitstatus: #{status.exitstatus}"
pid: 91187, returned_pid: 91187, status: #<Process::Status: pid 91187
exit 1>, exitstatus: 1
=> nil
2.4.4 :009 >
2.4.4 :010 > pid = fork do
2.4.4 :011 > sleep 2
2.4.4 :012?> exit 0
2.4.4 :013?> end
=> 91188
2.4.4 :014 >
2.4.4 :015 > returned_pid, status = Process.wait2
(irb):10:in `fork': unexpected throw
=> [91188, #<Process::Status: pid 91188 exit 1>]
2.4.4 :016 >
2.4.4 :017 > puts "pid: #{pid}, returned_pid: #{returned_pid},
status: #{status.inspect}, exitstatus: #{status.exitstatus}"
pid: 91188, returned_pid: 91188, status: #<Process::Status: pid 91188
exit 1>, exitstatus: 1
=> nil

···

--
Join the conversation in your neighborhood.
Register for Oakland's homegrown neighbor network, JustMyNeighbors.com

https://www.justmyneighbors.com/register

Looks like IRB implements it's own exit method:

  irb(main):001:0> method :exit
  => #<Method: main.exit(irb_exit)>

You can use Process.exit inside IRB, instead.

···

Kelly Felkins <kelly@restlater.com> wrote:

I'm also seeing "unexpected throw" messages. Perhaps I'm doing something
that is causing the unexpected throw, which is causing the exit status to
be 1.

Try this outside IRB as a standalone script. Eric is most likely right
that it's an IRB-specific problem you're running into.

···

Am 06. Januar 2019 um 16:42 Uhr -0800 schrieb Kelly Felkins:

I'm attempting to retrieve the exit status of child processes. So far, no
matter how the child process exists, wait2 status only has a 1 for
exitstatus.

--
Blog: https://mg.guelker.eu
PGP/GPG ID: F1D8799FBCC8BC4F

Thanks Eric and Marvin. I moved it to a script and I get the expected
results.

Here's the script and the results.

Thanks again.

-Kelly

script:

···

-------------

#!/usr/bin/env ruby
puts "ruby version: #{RUBY_VERSION}"

pid = fork do
  sleep 1
  exit 55
end

returned_pid, status = Process.wait2

puts "pid: #{pid}, returned_pid: #{returned_pid}, status:
#{status.inspect}, exitstatus: #{status.exitstatus}"

pid = fork do
  sleep 1
  exit 0
end

returned_pid, status = Process.wait2

puts "pid: #{pid}, returned_pid: #{returned_pid}, status:
#{status.inspect}, exitstatus: #{status.exitstatus}"

--------

output

------------

$ ./test.rb
ruby version: 2.4.1
pid: 91480, returned_pid: 91480, status: #<Process::Status: pid 91480
exit 55>, exitstatus: 55
pid: 91481, returned_pid: 91481, status: #<Process::Status: pid 91481
exit 0>, exitstatus: 0

---------------

On Mon, Jan 7, 2019 at 3:41 AM Marvin Gülker <m-guelker@phoenixmail.de> wrote:

Am 06. Januar 2019 um 16:42 Uhr -0800 schrieb Kelly Felkins:
> I'm attempting to retrieve the exit status of child processes. So far, no
> matter how the child process exists, wait2 status only has a 1 for
> exitstatus.

Try this outside IRB as a standalone script. Eric is most likely right
that it's an IRB-specific problem you're running into.

--
Blog: https://mg.guelker.eu
PGP/GPG ID: F1D8799FBCC8BC4F

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

--
Join the conversation in your neighborhood.
Register for Oakland's homegrown neighbor network, JustMyNeighbors.com

https://www.justmyneighbors.com/register