Fix race condition

If the watcher is stale, it was possible that the application process
could exit before all of its wait thread had reported their exit status.

To fix this, we don't actually exit the process until all the wait
threads have finished. However when we enter the "exiting" state, we
immediately close the socket to the manager, so the manager knows not to
send any new commands to this process.