Gallopsled/pwntools — pull request #114 by zachriggle (2014-09-15 06:53:03+00:00)
Title: Clean up tubes on process exit

Very frequently, I'll take a look at `ps`/`top`/`htop` and see a billion shells and various `stdin`-driven binaries still running in the background.

This happens because they're spawned, and I don't bother to call `close()` on them when I'm done. While this can be taken care of in a `with` block, it's a bit cumbersome to always remember to do (and in my opinion, messes up an otherwise pretty exploit).

This patch automatically closes all tubes when the process exits for any reason. While this will have little to no effect on some tubes (e.g. `sock`, `ssh`, `listen`) it is useful for `process` and may as well be the default behavior for all tubes.

```
$ python -c 'from pwn import *; process("/bin/sh");'
[+] Started program '/bin/sh'
[*] Stopped program '/bin/sh'
```

```
$ python -c 'from pwn import *; process("/bin/sh"); exit();'
[+] Started program '/bin/sh'
[*] Stopped program '/bin/sh'
```

```
python -c 'from pwn import *; process("/bin/sh"); time.sleep(10);'
[+] Started program '/bin/sh'
Traceback (most recent call last):
File "", line 1, in
KeyboardInterrupt
[*] Program '/bin/sh' stopped with exit code -2
```

```
$ python -c 'from pwn import *; process("/bin/sh"); raise Exception;'
[+] Started program '/bin/sh'
Traceback (most recent call last):
File "", line 1, in
Exception
[*] Stopped program '/bin/sh'
```