[Issue #1241] Fixing potential race condition in cancelConnection