glue: improved error handling

The core error tests were passing mostly because the removeAsyncListener
function was never getting called after the synchronous crash, thus
ensuring that every thing after it was also being run with that
asyncListener active. It turns out that getting the tests to pass after
dealing with that required porting over a bunch of ideas from Trevor's
patch, with the upshot that the error handling is now much more robust,
at the cost of there being some spooky action at a distance around how
the data gets put where the error handler can see it.

Brought over Trevor's latest tests from #6011 as well.