Repository navigation
Conversation
There was a problem hiding this comment.
Code Review
This pull request replaces force-unwraps of the render object's owner with null-safe navigation when enqueuing semantics geometry updates, preventing potential null pointer crashes. A test is also added to verify this behavior. The feedback suggests removing the expect(..., returnsNormally) wrapper from the new test to avoid a common testing anti-pattern and ensure cleaner stack traces upon failure.
| expect(() { | ||
| layout(parent, phase: EnginePhase.flushSemantics); | ||
|
|
||
| // Exclude `child` from the semantics tree while its geometry is dirty, | ||
| // leaving its geometry marked dirty (blocked). | ||
| middle1.markNeedsLayout(); | ||
| middle2.markNeedsLayout(); | ||
| child.markNeedsLayout(); | ||
| final child2 = RenderBlockSemanticsBoundary(); | ||
| parent.add(child2); | ||
| pumpFrame(phase: EnginePhase.flushSemantics); | ||
|
|
||
| // Re-include `child` while its geometry is still dirty. This drives the | ||
| // geometry-update enqueue in `updateChildren`. | ||
| middle1.markNeedsLayout(); | ||
| child.markNeedsLayout(); | ||
| parent.remove(child2); | ||
| pumpFrame(phase: EnginePhase.flushSemantics); | ||
| }, returnsNormally); |
There was a problem hiding this comment.
In Flutter/Dart tests, wrapping the entire test body in expect(() { ... }, returnsNormally) is generally considered an anti-pattern. If any exception is thrown, the test will automatically fail, and doing so without the expect wrapper provides a cleaner, more direct stack trace. Consider removing the expect and returnsNormally wrapper.
layout(parent, phase: EnginePhase.flushSemantics);
// Exclude `child` from the semantics tree while its geometry is dirty,
// leaving its geometry marked dirty (blocked).
middle1.markNeedsLayout();
middle2.markNeedsLayout();
child.markNeedsLayout();
final child2 = RenderBlockSemanticsBoundary();
parent.add(child2);
pumpFrame(phase: EnginePhase.flushSemantics);
// Re-include `child` while its geometry is still dirty. This drives the
// geometry-update enqueue in `updateChildren`.
middle1.markNeedsLayout();
child.markNeedsLayout();
parent.remove(child2);
pumpFrame(phase: EnginePhase.flushSemantics);
chunhtai
left a comment
There was a problem hiding this comment.
LGTM, once the gemini comment is addressed
|
Thanks for the review @chunhtai , Gemini's suggestion has been addressed. |
justinmc
left a comment
There was a problem hiding this comment.
Change looks good but see my comment on the test.
|
|
||
| test('does not crash enqueuing geometry update when child owner is null', () { |
There was a problem hiding this comment.
This test passes on master, could you make it fail without your change?
|
HI @justinmc You're right, as written it passes on master so it isn't proving anythingthe, the tricky part is that At this point it's basically a defensive null check, same shape as the |
|
This shouldn't be test exempted. If we can't figure out the exact cause, we shouldn't merge this. |
… cache (flutter#192724) Fixes flutter#195316 `PipelineOwner.flushSemantics` can crash with `Null check operator used on a null value` when a semantics boundary becomes invisible while its branch is blocked from the semantics tree (e.g. by the `BlockSemantics` of a dialog or modal bottom sheet). ### Cause The branch added in flutter#184226 handles a node whose geometry became invisible by adding its parent in the semantics tree to the update targets. When that parent has dirty geometry, it reads `parentInSemanticsTree.firstAncestorNodeWithCleanGeometry` directly. That cache is only valid for the current flush after `computeAncestorInfo(treeShapeToken)` has been called on the parent, and nothing guarantees that: the `node._semantics.computeAncestorInfo(treeShapeToken)` call just before it stops at the node itself when that node has clean geometry, so it never reaches the parent. So the parent can still hold the value from an earlier flush. If, in between, the ancestor it points to lost its geometry (a semantics boundary that is also a relayout boundary changes, and a non-contributing render object such as a `Transform` clears the geometry below it), the stale cache points at a node with `geometry == null`. Calling `ensureSemanticsNode()` on it then fails in `_buildSemanticsSubtree` (`geometry!.hidden`) or in `_produceSemanticsNode` -> `_updateSemanticsNodeGeometry` (`geometry!`). ### Fix Call `parentInSemanticsTree.computeAncestorInfo(treeShapeToken)` before reading the cache, like the other readers of `firstAncestorNodeWithCleanGeometry` in `flushSemantics` do. With the token this is a no-op when the value was already computed in this flush. ### Context - The crash started with flutter#180375 (geometry dirty nodes). flutter#184036 reported the `_buildSemanticsSubtree` null check and was closed by flutter#184226, which does not cover this path. - In our production app (mostly Android) this crash shows up on every release from 3.44 to 3.47.2: about 12,000 events from 2,200 users in 30 days. The stack traces match both null checks above. - flutter#189942 / flutter#189908 are about a different null check (`owner!` in `updateChildren` / `markNeedsUpdate`). Related: flutter#184036 (closed). ### Tests Added a regression test to `test/widgets/semantics_blocked_geometry_test.dart`. It uses only `package:flutter/widgets.dart` and reproduces the stale cache: the page is blocked, a frame caches an ancestor with clean geometry, and the next frame clears that ancestor's geometry while an invisible boundary triggers the lookup. - On current master without the fix, the test fails with `_TypeError: Null check operator used on a null value` in `_RenderObjectSemantics._buildSemanticsSubtree`. With the fix it passes. - On stable 3.47.3, the same test fails without the fix. A larger Material repro (`showModalBottomSheet` + `ListView` + `TextField`) fails in `_updateSemanticsNodeGeometry`. Both pass with the fix. That Material repro no longer crashes on master without the fix, so I did not add it. - Ran locally with the fix: `test/rendering/`, `test/semantics/`, `test/widgets/*semantics*_test.dart`, `test/material/bottom_sheet_test.dart`, `test/material/dialog_test.dart`, `test/widgets/routes_test.dart`, `test/widgets/modal_barrier_test.dart`. All pass. `flutter analyze` and `dart format` are clean on the changed files. ## 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 in-code documentation (doc comments with `///`). - [x] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). - [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. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- 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/ [flutter/tests]: https://github.com/flutter/tests [flutter/website]: https://github.com/flutter/website [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
Description
Fixes a fatal
_TypeError("Null check operator used on a null value") thrown duringPipelineOwner.flushSemantics.In
_RenderObjectSemantics(packages/flutter/lib/src/rendering/object.dart), two spots enqueue a render object for a geometry update viaowner!._nodesNeedingSemanticsGeometryUpdate.add(...). When a contributing render object is momentarily detached (a parent flips include -> exclude -> include across frames while the object's semantics geometry is still dirty),owneris null and theowner!deref throws.This mirrors the already-fixed sibling crash #184036 (fixed by #184226), which guarded the
_buildSemanticsSubtreesite but left these two enqueue sites unguarded.Changes
updateChildren: guard the enqueue withowner?.instead ofowner!.markNeedsUpdate: same guard.A detached node has no owner and is not in the pipeline, so skipping the enqueue is a correct no-op; it is re-scheduled when the node re-attaches.
scheduleInitialSemanticsis intentionally left unchanged, since it asserts the object is attached and its owner is non-null by contract.Added a regression test in
packages/flutter/test/rendering/semantics_layout_sized_test.dartthat drives the include -> exclude -> include geometry-dirty flip and asserts the flush completes without throwing. The exact detached-owner race cannot be staged deterministically in a single-threaded test (the framework's attach/detach invariants prevent setting it up directly), so the test exercises the same reachable enqueue path; a note in the test explains this.Fixes #189908
Pre-launch Checklist
///).