Fix some daemon errors related to multiple/out-of-order events


Fix some daemon errors related to multiple/out-of-order events

Ref T10811. Currently, we can emit the "willExit" event multiple times, or emit it and then emit a "log" event.

This causes some problems with the Phabricator listener, which cleans up resources the first time it sees this event. When it gets the second copy (or logs afterward) it fails to process events.

Instead, emit it only once we're actually cleaning up the daemon handle. This guarantees it emits last and emits exactly once.

I believe this only cleans up logs, but it's possible it also improves stability.

Test Plan:
You can reproduce this issue like this:

  • Run bin/phd debug hangforever.
  • Kill it with ^C^C.
  • Most of the time, it will emit errors.

With --trace, you can see additional information. After this patch, it no longer emits errors but still successfully marks the daemon as exited, and the willExit event is always emitted exactly once and emitted last.

Reviewers: chad

Reviewed By: chad

Maniphest Tasks: T10811

Differential Revision: https://secure.phabricator.com/D15975