After digging in a bit more, this gets even more complicated; if the inbound `$!` is a normal exception (not a `SystemExit`), this will fail badly when we hit the exit line:

> `-e:1:in 'exit': no implicit conversion from nil to integer (TypeError)`

And since we probably shouldn't be so bold as to set our own exit code here, maybe this needs a slightly different approach; since `$!` is a magical thread-local global, this would work just fine:

~~~ ruby
at_exit do
  # do the work in a separate thread, to avoid stomping on $!,
  # since other libraries depend on it directly.
  Thread.new do
    stop if Process.pid == pid
  end.join
end
~~~