# Correct way to close IO.popen pipe under Windows? (mingw)

**URL:** <https://www.ruby-forum.com/t/correct-way-to-close-io-popen-pipe-under-windows-mingw/56741>\
**Category:** Ruby\
**Created:** [April 3, 2006, 2:35pm UTC](https://www.ruby-forum.com/t/correct-way-to-close-io-popen-pipe-under-windows-mingw/56741 "2006-04-03T14:35:05Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Taisuke\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5f8ce5/32.png) [@Taisuke\_Y](https://www.ruby-forum.com/u/Taisuke_Y)\
**Post date:** [April 3, 2006, 2:35pm UTC](https://www.ruby-forum.com/t/correct-way-to-close-io-popen-pipe-under-windows-mingw/56741/1 "2006-04-03T14:35:05Z")

</div>

Hi.

I’m currently writing a wxRuby program to visualize wireless  
network (802.11), and is somewhat stuck with IO.popen behavior  
under Windows.

My software is composed of two parts: one for gathering Wi-Fi  
information through Windows NDIS/WMI API (CLI program written in C),  
and the other for controlling the former and visualizing  
collected result using Ruby/wxWindows. Gathered information is  
passed over IO.popen pipe.

The problem is that whenever I try to close this pipe,  
it never comes back. After some investigation, I found out  
IO.close on Windows waits forever until invoked process exits.  
Actually, this is same on UNIX (pclose(2) behavior), but it  
was not a issue on UNIX as process gets interrupted by EPIPE.

So the question is: How can I run forever-looping CLI program  
interactively on Windows? What I want to do is essentially as  
follows:

# double click event handler

# - this must not block as it kills GUI interactiveness

def on\_mouse\_double\_click(event)  
IO.popen(cmd) do |io|  
while ! io.eof? do  
# update progress and check for user-requested cancellation  
if (user\_req = update\_progress\_bar()) == CANCEL  
break  
end

```
    # stop processing once enough data is collected
    if got_enough_data(io.gets)
      break
    end
  end
end

```

end

As I wrote above, this event handler code blocks after it “broke”  
out of the loop because implicit IO.close never returns.

For now, I worked around the problem by modifying invoked command  
to exit if certain file exists, and updated above code to create  
such file once break condition is met. But since this workaround  
requires access to the source, I’d like to know cleaner way to do  
the same thing, without any kind of kludge.

I also tried doing “kill(SIGKILL, io.pid)” before exiting the loop,  
but it resulted to general unstability of the program as killed  
process left environment in uncertain state. Other signals (SIGINT, etc)  
had no effect on Windows.

Any ideas? Thanks in advance.
