From 85b22ff5946878f45b8164afda85fa7850fb1e43 Mon Sep 17 00:00:00 2001 From: chunhtai <47866232+chunhtai@users.noreply.github.com> Date: Fri, 12 Mar 2021 16:18:05 -0800 Subject: [PATCH] Fix semantics node id overflow issue (#77287) --- .../flutter/lib/src/semantics/semantics.dart | 22 ++++++++++++++----- .../test/semantics/semantics_test.dart | 20 +++++++++++++++++ 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/packages/flutter/lib/src/semantics/semantics.dart b/packages/flutter/lib/src/semantics/semantics.dart index de2a06818f..0ecbdc2293 100644 --- a/packages/flutter/lib/src/semantics/semantics.dart +++ b/packages/flutter/lib/src/semantics/semantics.dart @@ -1207,7 +1207,7 @@ class SemanticsNode extends AbstractNode with DiagnosticableTreeMixin { SemanticsNode({ this.key, VoidCallback? showOnScreen, - }) : id = _generateNewId(), + }) : _id = _generateNewId(), _showOnScreen = showOnScreen; /// Creates a semantic node to represent the root of the semantics tree. @@ -1217,7 +1217,7 @@ class SemanticsNode extends AbstractNode with DiagnosticableTreeMixin { this.key, VoidCallback? showOnScreen, required SemanticsOwner owner, - }) : id = 0, + }) : _id = 0, _showOnScreen = showOnScreen { attach(owner); } @@ -1244,9 +1244,15 @@ class SemanticsNode extends AbstractNode with DiagnosticableTreeMixin { /// The unique identifier for this node. /// - /// The root node has an id of zero. Other nodes are given a unique id when - /// they are created. - final int id; + /// The root node has an id of zero. Other nodes are given a unique id + /// when they are attached to a [SemanticsOwner]. If they are detached, their + /// ids are invalid and should not be used. + /// + /// In rare circumstances, id may change if this node is detached and + /// re-attached to the [SemanticsOwner]. This should only happen when the + /// application has generated too many semantics nodes. + int get id => _id; + int _id; final VoidCallback? _showOnScreen; @@ -1541,7 +1547,11 @@ class SemanticsNode extends AbstractNode with DiagnosticableTreeMixin { @override void attach(SemanticsOwner owner) { super.attach(owner); - assert(!owner._nodes.containsKey(id)); + while (owner._nodes.containsKey(id)) { + // Ids may repeat if the Flutter has generated > 2^16 ids. We need to keep + // regenerating the id until we found an id that is not used. + _id = _generateNewId(); + } owner._nodes[id] = this; owner._detachedNodes.remove(this); if (_dirty) { diff --git a/packages/flutter/test/semantics/semantics_test.dart b/packages/flutter/test/semantics/semantics_test.dart index 6faedf3551..cee139fcb3 100644 --- a/packages/flutter/test/semantics/semantics_test.dart +++ b/packages/flutter/test/semantics/semantics_test.dart @@ -8,6 +8,8 @@ import 'package:flutter_test/flutter_test.dart'; import '../rendering/rendering_tester.dart'; +const int kMaxFrameworkAccessibilityIdentifier = (1<<16) - 1; + void main() { setUp(() { debugResetSemanticsIdCounter(); @@ -567,6 +569,24 @@ void main() { ); }); + test('Semantics id does not repeat', () { + final SemanticsOwner owner = SemanticsOwner(); + const int expectId = 1400; + SemanticsNode? nodeToRemove; + for (int i = 0; i < kMaxFrameworkAccessibilityIdentifier; i++) { + final SemanticsNode node = SemanticsNode(); + node.attach(owner); + if (node.id == expectId) { + nodeToRemove = node; + } + } + nodeToRemove!.detach(); + final SemanticsNode newNode = SemanticsNode(); + newNode.attach(owner); + // Id is reused. + expect(newNode.id, expectId); + }); + test('Tags show up in debug properties', () { final SemanticsNode actionNode = SemanticsNode() ..tags = {RenderViewport.useTwoPaneSemantics};