Fix for race condition when cancelling a connection.