create on_exit replacement

Adds OnExitManager that can be used to replace on_exit which isn't portable.