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: 
As answered [here](https://stackoverflow.com/a/2976109/4869068) and [here](https://unix.stackexchange.com/a/478969), `EIO` can be avoided by keeping a file descriptor to the pty slave device open in the parent process.

Since `PTY.spawn` closes the slave file descriptor passed to the child process, a simple workaround is to open a new one. For example:

```rb
PTY.spawn("ls") do |r, w, pid|
  r2 = File.open(r.path)

  while IO.select([r], [], [], 1)
    puts r.gets
  end

  r2.close
end

```
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"