From 649898f1df8ba1ac275cf2e617818bf208fb62ce Mon Sep 17 00:00:00 2001 From: Felix Weilbach Date: Mon, 30 Nov 2026 07:01:49 -0800 Subject: [PATCH] Ignore image progress that arrives after the request finished Summary: On iOS and macOS, an image request reports progress and completion from different threads, so a progress callback can arrive after the request has already completed or failed. `ImageResponseObserverCoordinator::nativeImageResponseProgress` asserted that the request was still loading, so in debug builds such a late callback aborted the app. It is likeliest with images reloaded many times a second from a local HTTP server. A late progress callback is now dropped: there is nothing left to report once the request has finished. Completion after cancellation was allowed the same way before; this covers progress after completion or failure. Changelog: [General][Fixed] - Fix a debug-build crash when image progress arrives after the image finished loading Differential Revision: D135046959 --- .../ImageResponseObserverCoordinator.cpp | 10 ++- .../ImageResponseObserverCoordinatorTest.cpp | 73 +++++++++++++++++++ 2 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 packages/react-native/ReactCommon/react/renderer/imagemanager/tests/ImageResponseObserverCoordinatorTest.cpp diff --git a/packages/react-native/ReactCommon/react/renderer/imagemanager/ImageResponseObserverCoordinator.cpp b/packages/react-native/ReactCommon/react/renderer/imagemanager/ImageResponseObserverCoordinator.cpp index c63b4c984d8a..777c1a97aa52 100644 --- a/packages/react-native/ReactCommon/react/renderer/imagemanager/ImageResponseObserverCoordinator.cpp +++ b/packages/react-native/ReactCommon/react/renderer/imagemanager/ImageResponseObserverCoordinator.cpp @@ -74,10 +74,14 @@ void ImageResponseObserverCoordinator::nativeImageResponseProgress( int64_t loaded, int64_t total) const { mutex_.lock(); + // Progress is reported on a different thread than completion, so it can + // arrive after the request has finished. It has nothing left to report then. + if (status_ != ImageResponse::Status::Loading && + status_ != ImageResponse::Status::Cancelled) { + mutex_.unlock(); + return; + } auto observers = observers_; - react_native_assert( - status_ == ImageResponse::Status::Loading || - status_ == ImageResponse::Status::Cancelled); mutex_.unlock(); for (const auto& observer : observers) { diff --git a/packages/react-native/ReactCommon/react/renderer/imagemanager/tests/ImageResponseObserverCoordinatorTest.cpp b/packages/react-native/ReactCommon/react/renderer/imagemanager/tests/ImageResponseObserverCoordinatorTest.cpp new file mode 100644 index 000000000000..3a1e41d7cbfa --- /dev/null +++ b/packages/react-native/ReactCommon/react/renderer/imagemanager/tests/ImageResponseObserverCoordinatorTest.cpp @@ -0,0 +1,73 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#include + +#include + +#include +#include + +using namespace facebook::react; + +namespace { + +class CountingObserver : public ImageResponseObserver { + public: + void didReceiveProgress( + float /*progress*/, + int64_t /*loaded*/, + int64_t /*total*/) const override { + ++progressCount; + } + void didReceiveImage(const ImageResponse& /*imageResponse*/) const override { + ++imageCount; + } + void didReceiveFailure(const ImageLoadError& /*error*/) const override { + ++failureCount; + } + + mutable int progressCount{0}; + mutable int imageCount{0}; + mutable int failureCount{0}; +}; + +} // namespace + +TEST(ImageResponseObserverCoordinatorTest, reportsProgressWhileLoading) { + const ImageResponseObserverCoordinator coordinator{{}, {}}; + const auto observer = std::make_shared(); + coordinator.addObserver(observer); + + coordinator.nativeImageResponseProgress(0.5f, 50, 100); + + EXPECT_EQ(observer->progressCount, 1); +} + +TEST(ImageResponseObserverCoordinatorTest, ignoresProgressAfterCompletion) { + const ImageResponseObserverCoordinator coordinator{{}, {}}; + const auto observer = std::make_shared(); + coordinator.addObserver(observer); + coordinator.nativeImageResponseComplete(ImageResponse{nullptr, nullptr}); + + coordinator.nativeImageResponseProgress(1.0f, 100, 100); + + EXPECT_EQ(observer->imageCount, 1); + EXPECT_EQ(observer->progressCount, 0); +} + +TEST(ImageResponseObserverCoordinatorTest, ignoresProgressAfterFailure) { + const ImageResponseObserverCoordinator coordinator{{}, {}}; + const auto observer = std::make_shared(); + coordinator.addObserver(observer); + coordinator.nativeImageResponseFailed(ImageLoadError{nullptr}); + + coordinator.nativeImageResponseProgress(1.0f, 100, 100); + + EXPECT_EQ(observer->failureCount, 1); + EXPECT_EQ(observer->progressCount, 0); +}