AndrewRadev/vimrunner — pull request #10 by AndrewRadev (2012-09-23 09:24:51+00:00)
Title: Explicit killing

During my work on a plugin, I noticed two problems with vimrunner:

1. With a single Vim instance used to run all the tests, at some point it seems to just hang and
do nothing. This only occurred with a terminal Vim, GUI Vim always seems to run fine.
2. With a Vim instance per test, after the tests are over, *a lot* of leftover Vim instances are
still alive.

The first problem is still a mystery to me, but it seems like the second problem is caused by the Vim processes simply not being killed properly.

Now, according to the PTY documentation, `PTY#spawn` detaches the process when it's done with it and dedicates a separate thread whose purpose is to kill it. If I monitor what's happening with htop, it does seem like each vimrunner gets a `ruby` thread next to it once it's done. I'm only guessing, but I think that, afterwards, after the master process is over, the threads are just not waited for, so the processes never get the chance to get reaped. This would explain why a single process doesn't seem to have this problem -- it's just fast enough to kill it at the end, so everything seems fine. But, again, this is only my guess. I played around a bit with `Thread#wait` and `Process.wait`, but I couldn't figure out something that works.

Explicitly sending a `Process.kill` seems to clean things up nicely, but the signal needs to be 9. The usual 15 doesn't do a thing for some reason, at least for me. Since the `kill` call is meant to just get rid of the process, this seems okay. Just in case, I put a `noswapfile nobackup` in the `vimrc` to avoid accidental leftovers, though I'm not entirely sure if something like this is necessary. I also wrote some specs, but even without the `kill`, they only fail occasionally. They do seem to fail more often than not, but it's still kind of iffy.

@mudge, if you have some time one of these days, could you check this out and see how it works on a Mac? I'd also really appreciate your thoughts on the matter and whether this is a problem on OSX as well. You can test the leftover Vim instances on your own, but you could also check out my [switch.vim](https://github.com/AndrewRadev/switch.vim) project -- I spawn a Vim instance per test there, so you should be able to see the issue manifesting.