Fixed bug when closing connection in a callback