Commit 95f172e
Fix AnimationBackend choreographer deadlock with synchronous VirtualView events
Summary:
When the shared animation backend is enabled, starting/stopping a native animation calls `AnimationBackendChoreographer.resume()/pause()` on the thread
that started the animation, which can be the **JS thread**. The `resume()` method posted a frame callback to ReactChoreographer, acquiring the choreographer's callback queue. The UI thread might simultaneously hold that lock while dispatching a synchronous event through `executeSynchronouslyOnSameThread_CAN_DEADLOCK`. That path is chose after invoking the VirtualView `Visible` mode-change event. The `executeSynchronouslyOnSameThread_CAN_DEADLOCK` blocks main thread to access `jsi::Runtime` from the JS thread while holding a lock on the callbacks queue. There is a chance that resuming animation and the synchronous event will collide, as the first one tries to post a callback to the queue from the JS thread, waiting for release while the other holds a lock on that queue and waits for the JS thread to pass the runtime, causing a deadlock.
The fix is to keep the choreographer frame callback permanently registered. It is posted once on the UI thread and re-posts itself every frame, running as a no-op while paused. The `resume` and `pause` methods now only toggle so accessing the lock on the callback queue is not necessary.
Changelog:
[Android][Fixed] - Fix deadlock between the UI and JS threads when native animations start while a synchronous VirtualView mode-change event is dispatched
Differential Revision: D1073786271 parent 64c9663 commit 95f172e
1 file changed
Lines changed: 27 additions & 32 deletions
File tree
- packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric
Lines changed: 27 additions & 32 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
15 | | - | |
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
35 | | - | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
36 | 41 | | |
37 | 42 | | |
38 | | - | |
39 | | - | |
40 | | - | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
41 | 48 | | |
42 | 49 | | |
43 | | - | |
44 | | - | |
45 | | - | |
| 50 | + | |
46 | 51 | | |
47 | 52 | | |
48 | 53 | | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
| 54 | + | |
59 | 55 | | |
60 | 56 | | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
70 | 62 | | |
71 | 63 | | |
72 | 64 | | |
73 | | - | |
74 | 65 | | |
75 | | - | |
76 | | - | |
77 | | - | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
78 | 71 | | |
79 | 72 | | |
80 | 73 | | |
81 | 74 | | |
82 | | - | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
83 | 78 | | |
84 | 79 | | |
85 | 80 | | |
| |||
0 commit comments