Skip to content

Fix null-check crash in semantics geometry-dirty enqueue path - #189942

Draft
mbcorona wants to merge 3 commits into
flutter:masterfrom
mbcorona:fix-semantics-geometry-null-owner-189908
Draft

mbcorona wants to merge 3 commits into
flutter:masterfrom
mbcorona:fix-semantics-geometry-null-owner-189908

Conversation

@mbcorona

Copy link
Copy Markdown
Member

Description

Fixes a fatal _TypeError ("Null check operator used on a null value") thrown during PipelineOwner.flushSemantics.

In _RenderObjectSemantics (packages/flutter/lib/src/rendering/object.dart), two spots enqueue a render object for a geometry update via owner!._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), owner is null and the owner! deref throws.

This mirrors the already-fixed sibling crash #184036 (fixed by #184226), which guarded the _buildSemanticsSubtree site but left these two enqueue sites unguarded.

Changes

  • updateChildren: guard the enqueue with owner?. instead of owner!.
  • 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. scheduleInitialSemantics is 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.dart that 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

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the Flutter Style Guide, including Features we expect every widget to implement.
  • I signed the CLA.
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is test-exempt.
  • All existing and new tests are passing.

@flutter-dashboard flutter-dashboard Bot added the CICD Run CI/CD label Jul 23, 2026
@github-actions github-actions Bot added framework flutter/packages/flutter repository. See also f: labels. a: accessibility Accessibility, e.g. VoiceOver or TalkBack. (aka a11y) labels Jul 23, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +145 to +163
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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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);

@justinmc
justinmc self-requested a review July 28, 2026 22:31
@hannah-hyj
hannah-hyj self-requested a review July 29, 2026 21:47
@mbcorona mbcorona self-assigned this Jul 31, 2026
@chunhtai
chunhtai self-requested a review August 19, 2026 22:01

@chunhtai chunhtai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, once the gemini comment is addressed

@mbcorona

Copy link
Copy Markdown
Member Author

Thanks for the review @chunhtai , Gemini's suggestion has been addressed.

@chunhtai chunhtai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@chunhtai chunhtai added the autosubmit Merge PR when tree becomes green via auto submit App label Aug 21, 2026

@justinmc justinmc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change looks good but see my comment on the test.

Comment on lines 126 to +127

test('does not crash enqueuing geometry update when child owner is null', () {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test passes on master, could you make it fail without your change?

@chunhtai chunhtai removed the autosubmit Merge PR when tree becomes green via auto submit App label Aug 21, 2026
@mbcorona

Copy link
Copy Markdown
Member Author

HI @justinmc

You're right, as written it passes on master so it isn't proving anythingthe, the tricky part is that updateChildren only runs for nodes the flush loop picks up, and that loop already skips anything where owner != this. So the node being flushed is always attached. And since attach and detach walk the whole subtree together, any real child of an attached node is attached too. That means a normal child never has a null owner while its parent is being flushed. The null owner only seems to happen as a brief cross frame race in production, with an accessibility service running.
To hit the crash I need a node with a null owner sitting under an attached node that's being flushed, but the geometry step right after needs that same node to have a real ancestry path back up, which only happens if it's attached. Can't have both at once in a settled tree.

At this point it's basically a defensive null check, same shape as the if (renderObject.owner != null) guard we already have for the build path lower in this file. I'm inclined to drop the test and mark this one test exempt, but I didn't want to just do that without checking. Would that work for you, or is there a repro angle you'd want me to try?

@chunhtai

Copy link
Copy Markdown
Contributor

This shouldn't be test exempted. If we can't figure out the exact cause, we shouldn't merge this.

@hannah-hyj hannah-hyj added the waiting for response The Flutter team cannot make further progress on this issue until the original reporter responds label Sep 9, 2026
@mbcorona
mbcorona marked this pull request as draft September 30, 2026 12:57
@mbcorona

Copy link
Copy Markdown
Member Author

Draft until we get a real repro. #192724 fixes a related geometry! crash in the same path with a failing-on-master test and may be the actual cause of #189908; asked the reporter to verify. If it is, I'll close this.

@github-actions github-actions Bot removed the waiting for response The Flutter team cannot make further progress on this issue until the original reporter responds label Sep 30, 2026
Gustl22 pushed a commit to Gustl22/flutter that referenced this pull request Nov 24, 2026
… 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

a: accessibility Accessibility, e.g. VoiceOver or TalkBack. (aka a11y) CICD Run CI/CD framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[a11y] Null-check crash (_TypeError) in _RenderObjectSemantics.updateChildren — unguarded owner! at rendering/object.dart:5875

4 participants