From e4b2ea0982d3eb76ebb75754bbcb9059a018ca6b Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Thu, 18 Feb 2016 19:17:09 -0800 Subject: [PATCH] [ios, osx] Don't delete a mutex with an active lock 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. --- platform/darwin/src/http_request_nsurl.mm | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) 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; }