Correctly close the agent when all requests are delivered.

This bug required a LOT of effort for being tracked down, and some more
tests here and there were added.