Graceful shutdown

See https://bugzilla.mozilla.org/show_bug.cgi?id=1028899

I suspect we need to do this here too -