Kudo / react-native-v8

Opt-in V8 runtime for React Native Android
MIT License
917 stars 69 forks source link

V8 Runtime platform crash issue #75

Closed Victoriayangx closed 3 years ago

Victoriayangx commented 3 years ago

For Android,I embed V8 into ReactNative Sdk, And Found an issue.

1. Environment: ViewPager with Two Fragment:Fragment A & Fragment B, Every Fragment init a ReactRootView with different Bundle, with different ReactInstanceManager. Because ViewPager has PageCache, These two Fragments nearly init at the sameTime.

And it crashes At this Line : https://github.com/Kudo/react-native-v8/blob/e9d550ffc25e20d84ab92d452c238c7cc8b8b2be/src/v8runtime/V8Runtime.cpp#L15

2. RootCause: i found the reason is Two Different JS thread visit the variable "s_platform" nearly at the same time , but this variable did not add lock , so it crashed.

3. Suggest Solution: `std::mutex s_platform_mutex; // protects s_platform

V8Runtime::V8Runtime(const std::string &timezoneId) { { const std::lock_guard lock(s_platform_mutex); if (!s_platform) { s_platform = v8::platform::NewDefaultPlatform(); v8::V8::InitializeICU(); v8::V8::InitializePlatform(s_platform.get()); v8::V8::Initialize(); } }

arrayBufferAllocator_.reset( v8::ArrayBuffer::Allocator::NewDefaultAllocator()); .....

}

`

Kudo commented 3 years ago

nice finding! would you mind to open a pr for the solution?