diff --git a/platform/darwin/src/http_request_nsurl.mm b/platform/darwin/src/http_request_nsurl.mm index 5684afec3..e58441e4d 100644 --- a/platform/darwin/src/http_request_nsurl.mm +++ b/platform/darwin/src/http_request_nsurl.mm @@ -153,8 +153,15 @@ void HTTPNSURLRequest::cancel() { task = nullptr; } - std::lock_guard lock(cancelled->second); - cancelled->first = true; + { + std::lock_guard lock(cancelled->second); + cancelled->first = true; + } + + // The lock is in place to enforce that `async` is not accessed if the request has been + // cancelled. Therefore it's not necessary to hold the lock beyond setting cancelled to + // true, and in fact it's unsafe to so: if this is the last remaining shared reference, + // `delete this` will destroy the mutex. If the lock was held, it would then be orphaned. delete this; }