Skip to content

Commit 00e0b36

Browse files
Asset ImageFetcher thread correctness in DEBUG builds (facebook#54487)
Summary: Changelog: [Internal] Differential Revision: D86678354
1 parent c5bfab3 commit 00e0b36

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

packages/react-native/ReactCommon/react/renderer/imagemanager/platform/android/react/renderer/imagemanager/ImageFetcher.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,19 @@ extern const char ImageFetcherKey[] = "ImageFetcher";
1717

1818
ImageFetcher::ImageFetcher(
1919
std::shared_ptr<const ContextContainer> contextContainer)
20-
: contextContainer_(std::move(contextContainer)) {}
20+
: contextContainer_(std::move(contextContainer))
21+
#ifdef REACT_NATIVE_DEBUG
22+
,
23+
threadId_(std::this_thread::get_id())
24+
#endif
25+
{
26+
}
2127

2228
ImageFetcher::~ImageFetcher() noexcept {
2329
if (ReactNativeFeatureFlags::enableImagePrefetchingJNIBatchingAndroid()) {
30+
#ifdef REACT_NATIVE_DEBUG
31+
assertThread();
32+
#endif
2433
contextContainer_->erase(ImageFetcherKey);
2534
}
2635
}
@@ -68,4 +77,12 @@ void ImageFetcher::flushImageRequests() {
6877
items_.clear();
6978
}
7079

80+
#ifdef REACT_NATIVE_DEBUG
81+
void ImageFetcher::assertThread() const {
82+
assert(
83+
threadId_ == std::this_thread::get_id() &&
84+
"ImageFetcher method called from the wrong thread!");
85+
}
86+
#endif
87+
7188
} // namespace facebook::react

packages/react-native/ReactCommon/react/renderer/imagemanager/platform/android/react/renderer/imagemanager/ImageFetcher.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
#include <memory>
1414
#include <unordered_map>
1515
#include <vector>
16+
#ifdef REACT_NATIVE_DEBUG
17+
#include <thread>
18+
#endif
1619

1720
namespace facebook::react {
1821

@@ -39,5 +42,10 @@ class ImageFetcher {
3942

4043
std::unordered_map<SurfaceId, std::vector<ImageRequestItem>> items_;
4144
std::shared_ptr<const ContextContainer> contextContainer_;
45+
46+
#ifdef REACT_NATIVE_DEBUG
47+
std::thread::id threadId_;
48+
void assertThread() const;
49+
#endif
4250
};
4351
} // namespace facebook::react

0 commit comments

Comments
 (0)