src: emit 'beforeExit' event on process object

Unlike the 'exit' event, this event allows the user to schedule more
work and thereby postpone the exit.  That also means that the
'beforeExit' event may be emitted many times, see the attached test
case for an example.

Refs #6305.
doc: document `beforeExit` and add more tests