Skip to content

Commit 4160322

Browse files
authored
Prevent stale mounting revisions from recreating removed views
## Summary: A delayed revision can reach `MountingCoordinator::push` after a newer revision has already been pulled. Since pulling clears `lastRevision_`, the older revision is accepted and the next transaction can recreate a removed view. Reject revisions older than `baseRevision_` under the existing mutex. Equal revisions retain their existing behavior. ## Changelog: [ANDROID] [FIXED] - Prevent stale mounting revisions from recreating removed views during concurrent commits. ## Test Plan: Run the two added cases in `MountingCoordinatorTest.cpp` with a native C++ test harness: - `lateRevisionDoesNotRecreateRemovedView`: after revision 3 removes a view, a delayed revision 2 must produce no transaction or change to the base; revision 4 must still be accepted. - `lateRevisionPreservesNewerPendingRevision`: an older push must not replace a newer pending revision. Locally ran `./mounting_coordinator_tests --gtest_filter='MountingCoordinatorTest.*'`: both pass with the fix. Without the guard, the first regression fails. The executable was built with a temporary local CMake/gtest harness, which is not included in this PR; this is not a repository-provided test command. Execution of these tests in upstream CI has not been verified. Differential Revision: D129591526 Pull Request resolved: #59468
1 parent 418fc56 commit 4160322

3 files changed

Lines changed: 102 additions & 0 deletions

File tree

‎packages/react-native/ReactCommon/react/renderer/mounting/MountingCoordinator.cpp‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ void MountingCoordinator::push(ShadowTreeRevision revision) const {
4040
{
4141
std::scoped_lock lock(mutex_);
4242

43+
// ShadowTree releases its commit lock before pushing. Another thread may
44+
// already have diffed a newer revision, even when lastRevision_ is empty.
45+
// Never recreate removed views by diffing backwards from that newer base.
46+
if (revision.number < baseRevision_.number) {
47+
return;
48+
}
49+
4350
react_native_assert(
4451
!lastRevision_.has_value() || revision.number != lastRevision_->number);
4552

‎packages/react-native/ReactCommon/react/renderer/mounting/MountingCoordinator.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ class MountingCoordinator final {
110110
*/
111111
private:
112112
friend class ShadowTree;
113+
friend class MountingCoordinatorTest;
113114

114115
void push(ShadowTreeRevision revision) const;
115116

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#include <algorithm>
9+
#include <memory>
10+
#include <utility>
11+
12+
#include <gtest/gtest.h>
13+
14+
#include <react/renderer/element/Element.h>
15+
#include <react/renderer/element/testUtils.h>
16+
#include <react/renderer/mounting/MountingCoordinator.h>
17+
18+
namespace facebook::react {
19+
20+
class MountingCoordinatorTest : public ::testing::Test {
21+
protected:
22+
ComponentBuilder builder_{simpleComponentBuilder()};
23+
RootShadowNode::Unshared withChild_;
24+
RootShadowNode::Unshared withoutChild_;
25+
26+
void SetUp() override {
27+
auto props = std::make_shared<ViewProps>();
28+
props->collapsable = false;
29+
withChild_ = builder_.build(
30+
Element<RootShadowNode>().tag(1).surfaceId(1).children(
31+
{Element<ViewShadowNode>().tag(2).surfaceId(1).props(props)}));
32+
withChild_->layoutIfNeeded();
33+
withoutChild_ =
34+
std::static_pointer_cast<RootShadowNode>(withChild_->ShadowNode::clone(
35+
{.children = ShadowNode::emptySharedShadowNodeSharedList()}));
36+
withoutChild_->layoutIfNeeded();
37+
}
38+
39+
static void push(
40+
const MountingCoordinator& coordinator,
41+
ShadowTreeRevision revision) {
42+
coordinator.push(std::move(revision));
43+
}
44+
45+
static bool hasMutation(
46+
const MountingTransaction& transaction,
47+
ShadowViewMutation::Type type) {
48+
const auto& mutations = transaction.getMutations();
49+
return std::any_of(
50+
mutations.begin(), mutations.end(), [&](const auto& mutation) {
51+
return mutation.type == type &&
52+
(mutation.oldChildShadowView.tag == 2 ||
53+
mutation.newChildShadowView.tag == 2);
54+
});
55+
}
56+
};
57+
58+
TEST_F(MountingCoordinatorTest, lateRevisionDoesNotRecreateRemovedView) {
59+
MountingCoordinator coordinator{{withChild_, 1, {}}};
60+
61+
// Revision 3 removes the view while revision 2 is delayed between commit
62+
// and push. Pulling clears lastRevision_, but revision 3 remains the base.
63+
push(coordinator, {withoutChild_, 3, {}});
64+
auto removal = coordinator.pullTransaction();
65+
ASSERT_TRUE(removal.has_value());
66+
ASSERT_TRUE(hasMutation(*removal, ShadowViewMutation::Type::Delete));
67+
68+
push(coordinator, {withChild_, 2, {}});
69+
EXPECT_FALSE(coordinator.pullTransaction().has_value());
70+
EXPECT_EQ(coordinator.getBaseRevision().number, 3);
71+
EXPECT_EQ(coordinator.getBaseRevision().rootShadowNode, withoutChild_);
72+
73+
// A subsequent navigation may legitimately create the view again.
74+
push(coordinator, {withChild_, 4, {}});
75+
auto insertion = coordinator.pullTransaction();
76+
ASSERT_TRUE(insertion.has_value());
77+
EXPECT_TRUE(hasMutation(*insertion, ShadowViewMutation::Type::Create));
78+
EXPECT_EQ(coordinator.getBaseRevision().number, 4);
79+
}
80+
81+
TEST_F(MountingCoordinatorTest, lateRevisionPreservesNewerPendingRevision) {
82+
MountingCoordinator coordinator{{withChild_, 1, {}}};
83+
push(coordinator, {withoutChild_, 3, {}});
84+
push(coordinator, {withChild_, 2, {}});
85+
86+
auto removal = coordinator.pullTransaction();
87+
ASSERT_TRUE(removal.has_value());
88+
EXPECT_TRUE(hasMutation(*removal, ShadowViewMutation::Type::Delete));
89+
EXPECT_EQ(coordinator.getBaseRevision().number, 3);
90+
EXPECT_EQ(coordinator.getBaseRevision().rootShadowNode, withoutChild_);
91+
EXPECT_FALSE(coordinator.pullTransaction().has_value());
92+
}
93+
94+
} // namespace facebook::react

0 commit comments

Comments
 (0)