Fix hangs when connection drops or exception is thrown from query callback