Cannot obtain child process exit code on Windows



  • 1. thread deadlock issue
    can anyone interpret this deadlock 0xb7d8ff3c: sleep:T(1000000000000000019884624838656.000000) - /usr/local/ruby-1.8.4/lib/ruby/site_ruby/slave.rb:306 deadlock 0xb7d90888: sleep:F(4) - /usr/local/ruby-1.8.4/lib/ruby/site_ruby/drb/unix.rb:87 deadlock 0xb7d8fe9c: sleep:S - /usr/local/ruby-1.8.4/lib/ruby/site_ruby/drb/drb.rb:127 ^^ ^^ ^^ ^^ the bit in question ?? -a -- my religion is very simple. my religion is kindness. -- the dalai lama

Cannot obtain child process exit code on Windows

Postby Wi siong Ko » Thu, 17 Dec 2009 16:18:46 GMT


I would like to obtain the exit code from a child program. But all the
methods I tried were in vain. Here are two files that i used to test.

C:\> type a.bat
@echo off
exit /b 29

C:\> type test.rb
puts $?
puts $? >> 8

C:\>ruby test.rb
pid 5952 exit 0

Seems like in Windows, Ruby can never see the number 29 anyway.

Can anyone help me?
Thanks in advance.
Posted via  http://www.**--****.com/ 

Re: Cannot obtain child process exit code on Windows

Postby Luis Lavena » Thu, 17 Dec 2009 16:38:44 GMT

When reporting any issue, please include full version of Ruby (ruby -
v) that you're using.

Here an example:

ruby 1.8.6 (2009-08-04 patchlevel 383) [i386-mingw32]

irb(main):001:0> system("echo foo && exit /b 29")
=> false
irb(main):002:0> $?
=> #<Process::Status: pid=2296,exited(29)>

irb(main):001:0> $?
=> #<Process::Status: pid=2180,exited(0)>
irb(main):002:0> system('foo.bat')
=> false
irb(main):003:0> $?
=> #<Process::Status: pid=2860,exited(29)>

The exitstatus is correct, what are you trying to achieve shifting the
Process:Status exitstatus value?

Luis Lavena

Re: Cannot obtain child process exit code on Windows

Postby Wi siong Ko » Thu, 17 Dec 2009 17:32:54 GMT


I am using ruby 1.9.1p0 (2009-01-30 revision 21907) [i386-mswin32].
The right shifting was just trying with example given in
< http://www.**--****.com/ ;

Following your example, the exit code is 0 if is in a batch file. Here 
is my session:

C:\>type foo.bat
exit /b 29

irb(main):001:0> system("echo foo && exit /b 29")
=> false
irb(main):002:0> $?
=> #<Process::Status: pid 5712 exit 29>
irb(main):003:0> system('foo.bat')

C:\>exit /b 29
=> true
irb(main):004:0> $?
=> #<Process::Status: pid 4488 exit 0>

Posted via  http://www.**--****.com/ 

Re: Cannot obtain child process exit code on Windows

Postby Luis Lavena » Thu, 17 Dec 2009 23:51:29 GMT

When testing if there is a bug, please try latest patchlevels.

Since you're using mswin32 installation and a really old patchlevel
(zero). Will recommend migrate to RubyInstaller:


Which is the successor of One-Click Installer and provides, at this
time under RC1, binaries for both 1.8.6 and 1.9.1-p243

Luis Lavena

Re: Cannot obtain child process exit code on Windows

Postby Roger Pack » Fri, 18 Dec 2009 09:01:46 GMT

I bet ruby is giving you the exit code of running that .bat file under a 
different instance of command.  Maybe ping core it looks suspicious.
Posted via  http://www.**--****.com/ 

Re: Cannot obtain child process exit code on Windows

Postby Lars Christensen » Fri, 18 Dec 2009 19:41:03 GMT

This is normal, and doesn't have much to do with Ruby. You get the
same result in a plain command prompt. When running a batch script
from Ruby (or any other program), a new cmd.exe instance must be
spawned to launch the batch file:

C:\>type a.bat
@echo off
exit /B 29

C:\>cmd /c a.bat

C:\>echo %errorlevel%

When you run the batch file directly, you are not setting the exit
code, but simply the "errorlevel" of the current cmd instance. There
is no process exiting when running a.bat from the command line, so
there is no exit code to set by doing "exit /B 29".
Ruby gives you the exit code of the "process" - which is "cmd.exe",
and you are not asking "cmd.exe" to exit with any code other than 0.

You can change your .bat:

C:\>type b.bat
@echo off
exit 29

C:\>cmd /c b.bat

C:\>echo %errorlevel%

But this .bat will exit your console window if run from the command
line. This may help though:

Re: Cannot obtain child process exit code on Windows

Postby Wi siong Ko » Wed, 23 Dec 2009 13:17:54 GMT

Thank you for the explanation and solution. The cmd /c works great for 
my purpose.
Posted via  http://www.**--****.com/ 

Similar Threads:

1.Starting and stopping a child process in Windows

Alexey Verkhovsky wrote:
> What is the best / most reliable / most obvious way to start and kill
> a child process under Windows? This child process happens to be a Webrick 
> application, and I don't
> care about being platform-dependent in this case.

You might use CreateProcess from Win32api.
The PID is returned in the last parameter, the process_information 
Then use that PID in calling OpenProcess to get the Handle to use
when calling TerminateProcess.

2.Creating child processes in Windows?

3.inheriting socket in child process on Windows

Hello, list

In my Ruby application, I accept TCP connections from several clients.
any client sends a particular command, I want to spawn a child process
place of the parent process that will continue to service the already-
TCP connections without the clients needing to reconnect.

I originally wrote this code on Linux, and it works in the most
obvious way:

When the command is received, make note of all of the client's file
descriptors (IO#fileno). Spawn the child process and communicate the
list of
file descriptors to it. In the child process, call TCPSocket.for_fd
for each
file descriptor, and it is ready to read and write data.

I suppose it's not too surprising that this doesn't "just work" on
mainly because Ruby does a lot of special handling around the fact
that on
Windows, you can have both file descriptors and handles, and can only
one or the other into various functions. I've done enough debugging to
specifically why the above method doesn't work on Windows, but I don't
it's necessary to explain it unless someone is curious.

After a good amount of debugging and examining the Ruby 1.9 source, I
this working to the point where my child process can write to the
sockets, but cannot read from them. Here's what I did, and what

In the parent process, I now wrap _get_osfhandle and _open_osfhandle.
Instead of just calling IO#fileno, I call _get_osfhandle(IO#fileno).
fetches the real SOCKET (i.e. HANDLE) value. I pass these to the

In the child process, I now call TCPSocket.for_fd(_open_osfhandle
The other flags to _open_osfhandle don't actually matter. This step is
important to set up a mapping between the fd and the HANDLE value in
child process, so that later when I call TCPSocket#send or recv,
internally calls _get_osfhandle on its fd, it will get the right
value to pass into winsock functions.

By doing this, as I mentioned above, TCPSocket#send works, but recv
As far as I can tell, this is entirely due to some extra checks that
does before actually calling ws2_32!select.

In rb_w32_select:

nonsock += extract_fd(&else_rd, rd, is_not_socket);

This removes entries from the "rd" fd set that is passed in that match
criterion "is not a socket." I assumed that since I had passed in a
my fd would remain on this list. However...

From is_socket:

if (st_lookup(socklist, (st_data_t)sock, NULL)) // then true - it is

Only sockets produced by winsock function wrappers in win32.c here
admitted to the special socklist. Outsiders aren't allowed in, even if
actually are sockets. I also verified using a bit of debugger hacking
if I invert the return value of is_not_socket for the fd I care about,
ws2_32!select does get called, which in turn does lead to recv
called successfully.

Is there something special I can call to get my socket added to this
list? I
didn't see anything with a cursory glance of win32.c, which appears to
the only place socklist is used. Is there an entirely different
approach I
should be taking?


4.Process ruby exited with code 834

What does this message mean?

I just started seeing this from some JEdit macros I have that run ruby on 
the current buffer.
  cd $d  # change to dir. of current buffer
  ruby $f  # run file of current buffer


5.Child processes live after parent process is killed

6. Parent class cannot call child class method

7. Windows package task, windows cannot fork

8. waiting for multiple child processes to finish

Return to ruby


Who is online

Users browsing this forum: No registered users and 10 guest