Skip to content

fix(safearea): accept static lambdas in CreateWeakHandler debug assert - #1655

Merged
kazo0 merged 2 commits into
mainfrom
dev/vs/safearea-weakhandler-assert
Oct 8, 2026
Merged

kazo0 merged 2 commits into
mainfrom
dev/vs/safearea-weakhandler-assert

Conversation

@kazo0

@kazo0 kazo0 commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

GitHub Issue (If applicable): Regression from the #1602 fix (b2844ea)

PR Type

  • Bugfix

What is the current behavior?

In Debug builds, every app that loads a SafeArea crashes on startup:

Assertion failed.
CreateWeakHandler: onEvent must be a static/non-capturing delegate, otherwise it reintroduces a strong reference.
   at Uno.Toolkit.UI.SafeArea.SafeAreaDetails.CreateWeakHandler[TSender,TArgs](...) SafeArea.cs:line 351
   at Uno.Toolkit.UI.SafeArea.SafeAreaDetails.RegisterEvents() SafeArea.cs:line 380

The assert checks Delegate.Target is null. But C# compiles static lambdas into instance methods on the compiler's cached <>c singleton, so Target is never null for them. The check fails even though the callers are correct.

What is the new behavior?

  • SafeAreaDetails.IsNonCapturing(Delegate) treats a delegate as non-capturing when it has no target, or when its target has no instance fields.
  • A closure display class, or a delegate bound to this, still fails the assert as intended.
  • The check only runs inside Debug.Assert. The IL2075 trim warning on GetFields is suppressed with a justification, following the pattern already used in DependencyObjectExtensions.

PR Checklist

  • Tested code with current supported SDKs
  • Tested the changes where applicable:
    • UWP
    • WinUI (Release build clean)
    • iOS (Release build clean)
    • Android (Release build clean)
    • WASM
    • MacOS
  • Updated the documentation as needed (n/a: internal debug check)
  • Runtime Tests: added SafeAreaTests.WeakHandler_IsNonCapturing
  • Contains NO breaking changes
  • Associated with an issue (GitHub or internal)
  • Commits must be following the Conventional Commits specification.

Other information

Red/fix/green, run headless on the Material desktop head:

  • Red: with the old Target is null logic, WeakHandler_IsNonCapturing fails with static lambda / actual: false.
  • Green (Release): WeakHandler_IsNonCapturing passes.
  • Green (Debug): the app starts without the assert. SafeAreaTests passed 3 of 3; NestedSafeArea_ApplyCount stays skipped by its existing [Ignore].
  • Release library build: msbuild Uno.Toolkit.WinUI.csproj -c Release for all target frameworks gives 0 warnings and 0 errors.

🤖 Generated with Claude Code

https://claude.ai/code/session_01AG4ybou3qxfwwx9oeYgeRC

- Static lambdas compile to instance methods on the cached `<>c` singleton, so `Delegate.Target` is never null and the assert failed for every SafeArea in Debug builds, crashing the app on load
- Treat a delegate as non-capturing when its target has no instance fields (closure display classes and bound `this` still fail)
- Add `WeakHandler_IsNonCapturing` runtime test covering static, capturing and instance-bound delegates

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01AG4ybou3qxfwwx9oeYgeRC
Copilot AI balanced review requested due to automatic review settings September 29, 2026 19:02

Copilot AI 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.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

Review effort: Lite
Findings: 2 Medium severity · 1 Low severity

Open (3)
What changed in this PR

Fixes a Debug-only assertion regression in SafeAreaDetails.CreateWeakHandler so that static (non-capturing) lambdas no longer trip the “Target must be null” check, while still catching capturing lambdas and bound instance delegates.

Changes:

  • Replaced Delegate.Target is null debug assertions in CreateWeakHandler with a new IsNonCapturing(Delegate) helper that detects compiler-emitted static lambdas (<>c) via “no instance fields”.
  • Added trimming-warning suppression for reflective field inspection used by the debug assertion.
  • Added a runtime test verifying IsNonCapturing behavior for static vs capturing vs bound delegates.
File Description
src/​Uno.Toolkit.UI/​Controls/​SafeArea/​SafeArea.cs Adjusts the Debug.Assert predicate to correctly accept compiler-generated static lambdas while still rejecting closures/bound delegates.
src/​Uno.Toolkit.RuntimeTests/​Tests/​SafeAreaTests.cs Adds a regression test covering static lambda, capturing lambda, and bound instance method detection.

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

@github-actions

Copy link
Copy Markdown

- A delegate bound to a user type with no instance fields must still count as capturing; require the target type to carry CompilerGeneratedAttribute so only the `<>c` singleton passes
- Add that case to WeakHandler_IsNonCapturing
- Reword the IL2075 justification: the helper is also called by the runtime test, not only by Debug.Assert

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01AG4ybou3qxfwwx9oeYgeRC
@github-actions

Copy link
Copy Markdown

@MartinZikmund MartinZikmund mentioned this pull request Oct 7, 2026
7 of 14 tasks
@kazo0
kazo0 enabled auto-merge (squash) October 8, 2026 05:03
@kazo0
kazo0 disabled auto-merge October 8, 2026 05:03
@kazo0
kazo0 merged commit 9918d84 into main Oct 8, 2026
22 checks passed
@kazo0
kazo0 deleted the dev/vs/safearea-weakhandler-assert branch October 8, 2026 05:04
kazo0 added a commit that referenced this pull request Oct 28, 2026
…t (backport #1655)

- Cherry-picked from 9918d84
- Keeps the comment on why Debug is fully qualified: servicing/10.0 still builds native net*-android

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01PZQ75jvGWnf3XhqeenkJ9X
kazo0 added a commit that referenced this pull request Oct 28, 2026
Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: Xiaotian Gu <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants