From 81074754588bb755f7e0f12341dba0f3cbfa7814 Mon Sep 17 00:00:00 2001 From: Nathan Gass Date: Thu, 16 Oct 2025 20:00:05 +0200 Subject: [PATCH] Fix test for races with multiple parallel requests (#3891) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- include/mbgl/style/sources/geojson_source.hpp | 1 + src/mbgl/style/sources/geojson_source.cpp | 8 +++++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/include/mbgl/style/sources/geojson_source.hpp b/include/mbgl/style/sources/geojson_source.hpp index 4b53b6f16..30b3344e2 100644 --- a/include/mbgl/style/sources/geojson_source.hpp +++ b/include/mbgl/style/sources/geojson_source.hpp @@ -82,6 +82,7 @@ protected: private: std::optional url; std::unique_ptr req; + std::atomic requestGeneration{0}; std::shared_ptr sequencedScheduler; mapbox::base::WeakPtrFactory weakFactory{this}; // Do not add members here, see `WeakPtrFactory` diff --git a/src/mbgl/style/sources/geojson_source.cpp b/src/mbgl/style/sources/geojson_source.cpp index afc65d71e..29319255a 100644 --- a/src/mbgl/style/sources/geojson_source.cpp +++ b/src/mbgl/style/sources/geojson_source.cpp @@ -99,10 +99,12 @@ void GeoJSONSource::loadDescription(FileSource& fileSource) { return makeMutable(current, std::move(geoJSONData)); }, /* onImplReady */ - [this, self = makeWeakPtr(), capturedReq = req.get()](Immutable newImpl) { - assert(capturedReq); + [this, self = makeWeakPtr(), capturedReqGeneration = ++requestGeneration]( + Immutable newImpl) { + assert(capturedReqGeneration); if (auto guard = self.lock(); self) { - if (capturedReq == req.get()) { // If a new request is being processed, ignore this impl. + if (capturedReqGeneration == + requestGeneration) { // If a new request is being processed, ignore this impl. baseImpl = std::move(newImpl); loaded = true; observer->onSourceLoaded(*this);