AMQP cleanup and race condition fixes.

Closes #953.