Fix possible race in submitting queries after disconnecting