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: 
So I had to go as far as reading the C source for the PTY library to get really satisfied with what is going on here.

The Ruby PTY doc doesn't really say what the comments [in the source code](https://github.com/ruby/ruby/blob/57fb2199059cb55b632d093c2e64c8a3c60acfbb/ext/pty/pty.c#L519) say.

My solution was to put together a wrapper method and to call that from my script where needed. I've also boxed into the method waiting on the process to for sure exit and the accessing of the exit status from `$?`:

```
# file: lib/safe_pty.rb

require 'pty'
module SafePty
  def self.spawn command, &block

    PTY.spawn(command) do |r,w,p|
      begin
        yield r,w,p
      rescue Errno::EIO
      ensure
        Process.wait p
      end
    end

    $?.exitstatus
  end
end

```

This is used basically the same as PTY.spawn:

```
require 'safe_pty'
exit_status = SafePty.spawn(command) do |r,w,pid|
  until r.eof? do
    logger.debug r.readline
  end
end

#test exit_status for zeroness

```

I was more than a little frustrated to find out that this is a valid response, as it was completely undocumented on ruby-doc.
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

```