Skip to content

Commit 31de0e6

Browse files
jamesacklinclaude
andauthored
Fix mutex lock crash when dispatching events during exit (#59830)
* Leak EventEmitter::DispatchMutex to survive static destruction A function-local static std::mutex is destroyed by exit(). iOS calls exit() when a foreground app is terminated, and a keyboard or other host callback that dispatches an event during teardown then locks the destroyed mutex, which throws std::system_error ("mutex lock failed: Invalid argument") and terminates the process. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Use no_destroy for EventEmitter::DispatchMutex instead of leaking Co-Authored-By: Claude <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 206bc5b commit 31de0e6

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

‎packages/react-native/ReactCommon/react/renderer/core/EventEmitter.cpp‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,17 @@ static bool hasPrefix(const std::string& str, const std::string& prefix) {
3838
return "top" + prefixedType;
3939
}
4040

41+
#if defined(__clang__)
42+
#define NO_DESTROY [[clang::no_destroy]]
43+
#else
44+
#define NO_DESTROY
45+
#endif
46+
4147
std::mutex& EventEmitter::DispatchMutex() {
42-
static std::mutex mutex;
48+
// Never destroyed: host platform callbacks can still dispatch events while
49+
// `exit()` runs static destructors, and locking a destroyed mutex throws
50+
// `std::system_error`.
51+
NO_DESTROY static std::mutex mutex;
4352
return mutex;
4453
}
4554

0 commit comments

Comments
 (0)