Files
nitro/packages/react-native-nitro-modules/cpp
cd3d48677e fix: Synchronize C++ Promise<T> state-inspection accessors (#1398)
* fix(core): synchronize C++ `Promise<T>` state-inspection accessors

`resolve()`/`reject()` write `_state` under `_mutex`, but `isPending`/
`isResolved`/`isRejected`/`getResult`/`getError` read it lock-free.
`JSIConverter<shared_ptr<Promise<T>>>::toJSI` runs those reads on the JS thread
the instant a hybrid method returns, racing an off-JS-thread `resolve()`
(`Promise::async` / `Promise.parallel` / a main-thread resolve). Data race on the
`std::variant _state`, flagged by ThreadSanitizer.

Make `_mutex` mutable and acquire it in the inspection accessors. The lock-free
bodies move to private overloads that take a `unique_lock` (the caller proves it
holds the lock), so `getResult()`/`isPending()`-style call sites stay intact
without recursive self-locking. `resolve`/`reject` set the state and drain the
listeners under a single lock via `takeResolvedListeners(lock)`/
`takeRejectedListeners(lock)`, then fire them outside the lock; the `NITRO_DEBUG`
state assert runs just before the lock. Same treatment for `Promise<void>`.

* refactor(core): address review feedback on Promise synchronization

- Remove takeResolvedListenersAndClearAllListeners / takeRejectedListenersAndClearAllListeners helpers; inline as std::move + clearAllListeners
- Restore readable if/else structure in addOnXxxListener, using lock.unlock() before invoking the callback instead of nested RAII blocks with early returns
- Capture result pointer inside the lock in resolve() to avoid a second lock acquisition

* refactor(core): drop listeners outside lock by moving instead of clearing

* Update packages/react-native-nitro-modules/cpp/core/Promise.hpp

Co-authored-by: Marc Rousavy <me@mrousavy.com>

* refactor(core): remove redundant inline comments from listener methods

* fix(core): fix rebase artifact in resolve(const TResult&) — remove duplicate declaration and stale clearAllListeners

* refactor(core): remove remaining inline comment from addOnRejectedListener

* refactor(core): restore inline comments in addOnXxxListener per reviewer

---------

Co-authored-by: Miklós Fazekas <boga@MBPM5P.local>
Co-authored-by: Marc Rousavy <me@mrousavy.com>
2026-06-22 15:13:29 +02:00
..
…
2026-05-27 17:27:59 +02:00