From c523391b2024bfa6ab5d60c8a86fe3bf735c588f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Mon, 5 Aug 2019 14:32:37 +0200 Subject: [PATCH] [macos,ios] Fix rare crash when downloading tiles that returned a 404 We were capturing the reference to an object that could go away when the request was canceled. However, the lambda could still be invoked by NSURLSession, creating a race condition with a resulting crash when the response to a tile request was 404 Not Found. --- platform/darwin/src/http_file_source.mm | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/platform/darwin/src/http_file_source.mm b/platform/darwin/src/http_file_source.mm index 79810546b..5737d8b99 100644 --- a/platform/darwin/src/http_file_source.mm +++ b/platform/darwin/src/http_file_source.mm @@ -245,8 +245,10 @@ std::unique_ptr HTTPFileSource::request(const Resource& resource, } [req addValue:impl->userAgent forHTTPHeaderField:@"User-Agent"]; - - if (resource.kind == mbgl::Resource::Kind::Tile) { + + const bool isTile = resource.kind == mbgl::Resource::Kind::Tile; + + if (isTile) { [[MGLNetworkConfiguration sharedManager] startDownloadEvent:url.relativePath type:@"tile"]; } @@ -322,7 +324,7 @@ std::unique_ptr HTTPFileSource::request(const Resource& resource, if (responseCode == 200) { response.data = std::make_shared((const char *)[data bytes], [data length]); - } else if (responseCode == 204 || (responseCode == 404 && resource.kind == Resource::Kind::Tile)) { + } else if (responseCode == 204 || (responseCode == 404 && isTile)) { response.noContent = true; } else if (responseCode == 304) { response.notModified = true;