Question: In 2003 the workbook would just close. But now the same code is re-opening the workbook. There are some lines afterwards and then the sub ends. When the sub ends, the `workbook_open` event gets fired for the closed workbook, even though there is no code that opens any workbooks. The debugger is almost useless, it is not reproducing the bug if I execute everything step by step, in fact everything works fine when I use the debugger.

I use

`ActiveWorkbook.Close False`

For closing the workbook (from an add-in).

Help much appreciated.

Answer: 
It seems valid for Errno::EIO to be raised here (it simply means the child process has finished and closed the stream), so you should expect that and catch it.

For example, see the selected answer in [Continuously read from STDOUT of external process in Ruby](https://stackoverflow.com/questions/1154846/continuously-read-from-stdout-of-external-process-in-ruby) and <http://www.shanison.com/2010/09/11/ptychildexited-exception-and-ptys-exit-status/>

BTW, I did some testing. On Ruby 1.8.7 on Ubuntu 10.04, I don't get a error. With Ruby 1.9.3, I do. With JRuby 1.6.4 on Ubuntu in both 1.8 and 1.9 modes, I don't get an error. On OS X, with 1.8.7, 1.9.2 and 1.9.3, I don't get an error. The behavior is obviously dependent on your Ruby version and platform.
ruby-doc.org says this since ruby 1.9:

```
# The result of read operation when pty slave is closed is platform
# dependent.
ret = begin
        m.gets          # FreeBSD returns nil.
      rescue Errno::EIO # GNU/Linux raises EIO.
        nil
      end

```

Ok, so now I get this behavior is "normal" on Linux, but that means it's a little tricky to get the output of a PTY. If you do `m.read` it reads everything and then throws it away and raises Errno::EIO. You really need to read the content chunk by chunk with `m.readline`. And even then you risk losing the last line if it doesn't end with "\n" for whatever reason. To be extra safe you need to read the content byte by byte with `m.read(1)`

Additional note about the effect of tty and pty on buffering: it's not the same as `STDOUT.sync = true` (unbuffered output) in the child process, but rather it triggers **line buffering**, where output is flushed on "\n"