Skip to content

Commit 9192537

Browse files
CanArslanDevvictorsanni
authored andcommitted
Rebuild a semantics fragment when its sibling conflict changes (flutter#190431)
`shouldFormSemanticsNode` ends in `return parentData!.explicitChildNodes || _hasSiblingConflict;`. When the parent data half changes, `_didUpdateParentData` calls `markNeedsBuild`, because whether a fragment forms a node may have changed. When the sibling conflict half changes, nothing does. `markSiblingConfigurationConflict` just assigns the flag and returns. That gap is enough to corrupt the semantics tree. A fragment that stops forming its own node keeps `built == true` and keeps its `cachedSemanticsNode`. While it is out of the tree an ancestor can take its children back, and its child list is never refreshed because `_buildSemantics` short circuits on `built`. When the fragment forms a node again it is handed straight back into the tree still holding children that now belong to somebody else, and attaching it attaches them too. The result is a `SemanticsNode` that is attached but has no parent, which is what trips `assert(!child.attached)` in `_replaceChildren`. There are three earlier branches in `shouldFormSemanticsNode` (`isSemanticBoundary`, `isRoot`, `contributesToSemanticsTree`). I went looking for the same gap in those and could not find one, because everything that feeds them goes through `markNeedsUpdate`, which nulls parent data on its way up and so ends at `markNeedsBuild` anyway. I would not swear to that the way I would to the paragraph above, so if you know of a path I am not seeing, I would rather hear it now. Outside debug builds this looks worse than an assertion. The orphan still passes the `node.attached` check used when building `childrenInTraversalOrder`, so its id goes to the engine as a child that is not reachable, which is the shape of the `Failed to update ui::AXTree, error: N will not be in the tree and is not the new root` that @davidhicks980 reported in flutter#187198. I should be clear that this part is me reading the code and not something I measured. I could not find a way to drive VoiceOver from a test, so I have not confirmed that the release mode failure is this and only this. OverlayPortal runs into this reliably because showing an overlay child creates and removes a sibling conflict for the anchors around it, so node formation flips back and forth while the traversal graft moves nodes between subtrees. The change marks the fragment as needing a build when its sibling conflict flag actually changes across a pass. It has to be a before and after comparison rather than a check inside `markSiblingConfigurationConflict`, because `_marksConflictsInMergeGroup` deliberately resets every fragment to false first and that reset is load bearing: `configToMergeUp` reads `shouldFormSemanticsNode`, so leaving the previous pass's flag in place while recomputing changes the result. The snapshot is taken in the existing loop just before the reset, and the comparison rides along with the loop that marks the conflicts, so there is no extra pass over `mergeGroup`. Both directions are marked, because losing a conflict changes node formation just as much as gaining one. This runs during the parent data phase of `flushSemantics`, before the build loop, so the fragment is rebuilt in the same flush. ### Why not repair the child list instead The first version of this PR fixed the stale child list at the point of the theft, and guarded `attach` the way `detach` already is. Both work, and the test passes with either. @chunhtai pointed out that the fragment should have known its children were taken and marked itself, which turned out to be right, so this version drops both in favour of fixing the invalidation. Happy to add the `attach` guard back as hardening if you want it, since `detach` is still the only one of the ten methods in `semantics.dart` that walk `_children` that checks whether the child is still its own, but it is a separate concern from this bug. While I was checking that number I found I had got it wrong earlier. I said eight places here and on flutter#187198. It is ten, and the two I missed, `_redepthChildren` and `_updateChildrenMergeFlags`, both assert on `child.owner`, so a stale entry can trip there as well. I did earlier report that invalidating was not possible because `markNeedsBuild`, `RenderObject.markNeedsSemanticsUpdate` and the `flushSemantics` exit assert all reject being dirtied during the flush. That was true of invalidating at the moment of the theft, which is in the middle of the build phase. Invalidating at the moment the decision changes is a different place and does not have that problem. ### Test The regression test goes in the existing `Semantics` group in `overlay_portal_test.dart`. It needs two portal anchors before it will fail, because the fragment whose conflict changes belongs to the sibling anchor rather than the one being toggled. Taking the sibling out makes it pass on unpatched master. It fails on master with `'!child.attached': is not true` and passes with this change. One thing worth flagging: the test has no semantics expectations of its own, so its only failure mode is that framework assertion. Happy to add some if you would rather it pinned the resulting tree down. `test/semantics`, `test/rendering`, `test/widgets` and `test/material` come out at 16171 passing with nothing failing. Fixes flutter#189902 One request on that: @madenvel reported a `computeChildGeometry` assertion variant in a comment on that issue, and closing it through this PR would bury that report. Could that be split into its own issue? I have not been able to reproduce it, it is described as Linux and AT-SPI, and I do not know whether this change covers it. I said earlier that I expected it to survive any fix in this area, but that was about the two patches this version replaced, and the reasoning does not carry over. Rebuilding the fragment refreshes its geometry too, so it is genuinely an open question now rather than a no. Partially addresses flutter#187198. The assertions reported there go away. I said I would expect the AXTree failure to go away as well, but when I tried to check that I could not back it up. I recorded every semantics update sent to the engine in a widget test and checked it against the rules the desktop bridge applies. Neither of @davidhicks980's samples produces a bad update on current master, with or without this change. The shape from flutter#182444 (Tooltips in a ListView, and the MenuAnchor + Tooltip one reported there) does, with the exact same `will not be in the tree and is not the new root` error, and it does so with or without this change too. So that error has at least one cause this PR does not touch, and I can't tell which one was being hit in flutter#187198. The menu item positioning problem also reported there is separate and this does not touch it. For the record, flutter#182604 turned out not to be the same defect, even though it sits downstream of the same refactor. It was fixed by flutter#192139 on the engine side, where `setChildrenInHitTestOrder` was overwriting the traversal parent that `OverlayPortal` had set. I had said on the two issues above that all three came down to one thing, which was wrong. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --------- Co-authored-by: Victor Sanni <[email protected]>
1 parent 323b482 commit 9192537

2 files changed

Lines changed: 68 additions & 0 deletions

File tree

‎packages/flutter/lib/src/rendering/object.dart‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6606,9 +6606,13 @@ class _RenderObjectSemantics extends _SemanticsFragment with DiagnosticableTreeM
66066606
}
66076607

66086608
void _marksConflictsInMergeGroup(List<_SemanticsFragment> mergeGroup, {bool isMergeUp = false}) {
6609+
final wasConflicting = <_RenderObjectSemantics>{};
66096610
final hasSiblingConflict = <_SemanticsFragment>{};
66106611
for (var i = 0; i < mergeGroup.length; i += 1) {
66116612
final _SemanticsFragment fragment = mergeGroup[i];
6613+
if (fragment is _RenderObjectSemantics && fragment._hasSiblingConflict) {
6614+
wasConflicting.add(fragment);
6615+
}
66126616
// Remove old value
66136617
fragment.markSiblingConfigurationConflict(false);
66146618
if (fragment.configToMergeUp == null) {
@@ -6626,8 +6630,18 @@ class _RenderObjectSemantics extends _SemanticsFragment with DiagnosticableTreeM
66266630
}
66276631
}
66286632
}
6633+
// A sibling conflict feeds shouldFormSemanticsNode, so gaining or losing one
6634+
// changes whether the fragment produces a semantics node of its own. What it
6635+
// has cached is stale until it is rebuilt.
66296636
for (final fragment in hasSiblingConflict) {
66306637
fragment.markSiblingConfigurationConflict(true);
6638+
if (fragment is _RenderObjectSemantics && !wasConflicting.remove(fragment)) {
6639+
fragment.markNeedsBuild();
6640+
}
6641+
}
6642+
// Whatever is left in wasConflicting stopped conflicting in this pass.
6643+
for (final fragment in wasConflicting) {
6644+
fragment.markNeedsBuild();
66316645
}
66326646
}
66336647

‎packages/flutter/test/widgets/overlay_portal_test.dart‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3242,6 +3242,60 @@ void main() {
32423242
),
32433243
);
32443244
});
3245+
3246+
// Regression test for https://github.com/flutter/flutter/issues/189902 and
3247+
// https://github.com/flutter/flutter/issues/187198.
3248+
testWidgets('toggling an overlay child does not leave a parentless attached semantics node', (
3249+
WidgetTester tester,
3250+
) async {
3251+
final controller = OverlayPortalController();
3252+
final siblingController = OverlayPortalController();
3253+
3254+
// A second anchor is required: the fragment whose conflict changes is the
3255+
// sibling's, not the one belonging to the portal being toggled.
3256+
final Widget sibling = OverlayPortal.overlayChildLayoutBuilder(
3257+
controller: siblingController,
3258+
overlayChildBuilder: (BuildContext context, OverlayChildLayoutInfo info) =>
3259+
const SizedBox.shrink(),
3260+
child: Semantics(explicitChildNodes: true, child: const Text('sibling')),
3261+
);
3262+
3263+
Widget portal = OverlayPortal.overlayChildLayoutBuilder(
3264+
controller: controller,
3265+
overlayChildBuilder: (BuildContext context, OverlayChildLayoutInfo info) =>
3266+
const Align(alignment: Alignment.topLeft, child: Text('overlay child')),
3267+
child: Semantics(explicitChildNodes: true, child: const Text('anchor')),
3268+
);
3269+
portal = Overlay.wrap(child: ExcludeSemantics(child: portal));
3270+
portal = SizedBox(width: 200, height: 100, child: portal);
3271+
3272+
await tester.pumpWidget(
3273+
Directionality(
3274+
textDirection: TextDirection.ltr,
3275+
child: Overlay.wrap(
3276+
child: Semantics(container: true, child: Column(children: <Widget>[sibling, portal])),
3277+
),
3278+
),
3279+
);
3280+
3281+
// Showing the overlay child gives the sibling anchor a sibling conflict,
3282+
// and hiding it takes that conflict away again. The conflict feeds
3283+
// shouldFormSemanticsNode, so the sibling's fragment stops producing a
3284+
// node of its own while keeping the one it cached. Its children are taken
3285+
// by an ancestor while it is out of the tree, and it used to be handed
3286+
// back in still holding them, leaving a SemanticsNode that was attached
3287+
// but had no parent. That only becomes observable on the second round
3288+
// trip.
3289+
for (var i = 0; i < 2; i += 1) {
3290+
controller.show();
3291+
await tester.pumpAndSettle();
3292+
expect(find.text('overlay child'), findsOneWidget);
3293+
3294+
controller.hide();
3295+
await tester.pumpAndSettle();
3296+
expect(find.text('overlay child'), findsNothing);
3297+
}
3298+
});
32453299
});
32463300

32473301
testWidgets(

0 commit comments

Comments
 (0)