Skip to content

Avoid notifying collected command targets - #40199

Merged
kubaflo merged 1 commit into
dotnet:candidate/11.0.1xx-sr2from
Oxymoron290:fix/weak-command-collected-target-net11
Nov 13, 2026
Merged

kubaflo merged 1 commit into
dotnet:candidate/11.0.1xx-sr2from
Oxymoron290:fix/weak-command-collected-target-net11

Conversation

@Oxymoron290

Copy link
Copy Markdown
Contributor

Note

Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

Target Branch

  • I selected the applicable base branch using the contribution guide.
  • net11: .NET 11 servicing fix for review.

Description of Change

When a command outlives a button, the subscription's DependentHandle can expose the button while it is finalization-reachable even after the button's short weak reference has cleared. Notifying that button can reapply its compiled command parameter binding after the binding's weak-reference handle has been released, causing Handle is not initialized.

Keep a short weak reference to the bindable object in the subscription proxy and resolve it before accessing the dependent handler. If the short reference has cleared, dispose the subscription instead of notifying the abandoned object. Retain the existing DependentHandle to preserve the handler's lifetime while the object is live. This adds one weak reference per subscription.

Add a regression test that delays finalizer processing, collects an abandoned button with a null compiled command-parameter binding, raises CanExecuteChanged, and verifies that a live button still disables and re-enables. The gate is bounded and released in finally.

Validation

  • On the net11 base, the new regression fails without this change with InvalidOperationException: Handle is not initialized from WeakReference<T>.SetTarget, and passes with the change.
  • Targeted CommandTests, ButtonUnitTest, and TypedBindingUnitTests on Windows ARM64 with the .NET 11 SDK: 251 passed, 2 skipped. The selection excluded TypedBindingUnitTests.FailToConvert, which also failed on the unmodified .NET 10 source in earlier local testing.
  • The standalone .NET 10 reproduction and Windows host also reproduce the reported exception without the fix. Testing used simulated button clicks, not pointer automation.
  • Formatted the modified files with dotnet format whitespace; git diff --check passes.

The original weak-subscription implementation was introduced by #29837. A search found no open PR for this exact exception; this change addresses the stale-callback lifetime window rather than modifying that PR's command-retention behavior.

Issues Fixed

Fixes #40198

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 41443241-c376-48d0-b685-7cfa647534be
@github-actions

Copy link
Copy Markdown
Contributor

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 40199

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 40199"

@dotnet-policy-service dotnet-policy-service Bot added the community ✨ Community Contribution label Nov 12, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Hey there @@Oxymoron290! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@azure-pipelines

This comment has been minimized.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Hey there @Oxymoron290! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@kubaflo

This comment has been minimized.

@github-actions github-actions Bot added the s/agent-review-in-progress AI review is currently running for this PR label Nov 12, 2026
@kubaflo

kubaflo commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

/rebase

@kubaflo

This comment has been minimized.

@azure-pipelines

This comment has been minimized.

@MauiBot

MauiBot commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator

PR Test Gate

@Oxymoron290 — existing-test verification; Setup did not capture a tested commit.

Result INCONCLUSIVE Platform Android


🧪 Gate analysis — click to expand

⚠ Inconclusive. Setup, build, or execution did not produce a conclusive result. This does not prove the fix is incorrect.

No interpretable test-group summary was produced. No additional test execution is inferred.

Setup did not capture a verified PR head; no commit is claimed as tested.

No immutable without-fix baseline was prepared.

@MauiBot MauiBot removed the s/agent-review-in-progress AI review is currently running for this PR label Nov 12, 2026

@github-actions github-actions 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.

Expert Review — 0 findings

Reviewed commit 0088a35.

@kubaflo
kubaflo changed the base branch from net11 to candidate/11.0.1xx-sr2 November 13, 2026 12:13
@kubaflo
kubaflo merged commit 4145855 into dotnet:candidate/11.0.1xx-sr2 Nov 13, 2026
143 of 182 checks passed
@kubaflo kubaflo mentioned this pull request Nov 13, 2026
1 task
kubaflo added a commit that referenced this pull request Nov 13, 2026
<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

### Target Branch

- [x] I selected the applicable base branch using the [contribution
guide](https://github.com/dotnet/maui/blob/main/.github/CONTRIBUTING.md#choose-your-pr-base-before-starting).
- `net11`: .NET 11 servicing fix for review.

### Description of Change

When a command outlives a button, the subscription's `DependentHandle`
can expose the button while it is finalization-reachable even after the
button's short weak reference has cleared. Notifying that button can
reapply its compiled command parameter binding after the binding's
weak-reference handle has been released, causing `Handle is not
initialized`.

Keep a short weak reference to the bindable object in the subscription
proxy and resolve it before accessing the dependent handler. If the
short reference has cleared, dispose the subscription instead of
notifying the abandoned object. Retain the existing `DependentHandle` to
preserve the handler's lifetime while the object is live. This adds one
weak reference per subscription.

Add a regression test that delays finalizer processing, collects an
abandoned button with a null compiled command-parameter binding, raises
`CanExecuteChanged`, and verifies that a live button still disables and
re-enables. The gate is bounded and released in `finally`.

### Validation

- On the `net11` base, the new regression fails without this change with
`InvalidOperationException: Handle is not initialized` from
`WeakReference<T>.SetTarget`, and passes with the change.
- Targeted `CommandTests`, `ButtonUnitTest`, and `TypedBindingUnitTests`
on Windows ARM64 with the .NET 11 SDK: 251 passed, 2 skipped. The
selection excluded `TypedBindingUnitTests.FailToConvert`, which also
failed on the unmodified .NET 10 source in earlier local testing.
- The standalone .NET 10 reproduction and Windows host also reproduce
the reported exception without the fix. Testing used simulated button
clicks, not pointer automation.
- Formatted the modified files with `dotnet format whitespace`; `git
diff --check` passes.

The original weak-subscription implementation was introduced by #29837.
A search found no open PR for this exact exception; this change
addresses the stale-callback lifetime window rather than modifying that
PR's command-retention behavior.

### Issues Fixed

Fixes #40198

Copilot-Session: 41443241-c376-48d0-b685-7cfa647534be
Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com>
kubaflo added a commit that referenced this pull request Nov 16, 2026
<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

### Target Branch

- [x] I selected the applicable base branch using the [contribution
guide](https://github.com/dotnet/maui/blob/main/.github/CONTRIBUTING.md#choose-your-pr-base-before-starting).
- `net11`: .NET 11 servicing fix for review.

### Description of Change

When a command outlives a button, the subscription's `DependentHandle`
can expose the button while it is finalization-reachable even after the
button's short weak reference has cleared. Notifying that button can
reapply its compiled command parameter binding after the binding's
weak-reference handle has been released, causing `Handle is not
initialized`.

Keep a short weak reference to the bindable object in the subscription
proxy and resolve it before accessing the dependent handler. If the
short reference has cleared, dispose the subscription instead of
notifying the abandoned object. Retain the existing `DependentHandle` to
preserve the handler's lifetime while the object is live. This adds one
weak reference per subscription.

Add a regression test that delays finalizer processing, collects an
abandoned button with a null compiled command-parameter binding, raises
`CanExecuteChanged`, and verifies that a live button still disables and
re-enables. The gate is bounded and released in `finally`.

### Validation

- On the `net11` base, the new regression fails without this change with
`InvalidOperationException: Handle is not initialized` from
`WeakReference<T>.SetTarget`, and passes with the change.
- Targeted `CommandTests`, `ButtonUnitTest`, and `TypedBindingUnitTests`
on Windows ARM64 with the .NET 11 SDK: 251 passed, 2 skipped. The
selection excluded `TypedBindingUnitTests.FailToConvert`, which also
failed on the unmodified .NET 10 source in earlier local testing.
- The standalone .NET 10 reproduction and Windows host also reproduce
the reported exception without the fix. Testing used simulated button
clicks, not pointer automation.
- Formatted the modified files with `dotnet format whitespace`; `git
diff --check` passes.

The original weak-subscription implementation was introduced by #29837.
A search found no open PR for this exact exception; this change
addresses the stale-callback lifetime window rather than modifying that
PR's command-retention behavior.

### Issues Fixed

Fixes #40198

Copilot-Session: 41443241-c376-48d0-b685-7cfa647534be
Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com>
kubaflo added a commit that referenced this pull request Nov 16, 2026
<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

### Target Branch

- [x] I selected the applicable base branch using the [contribution
guide](https://github.com/dotnet/maui/blob/main/.github/CONTRIBUTING.md#choose-your-pr-base-before-starting).
- `net11`: .NET 11 servicing fix for review.

### Description of Change

When a command outlives a button, the subscription's `DependentHandle`
can expose the button while it is finalization-reachable even after the
button's short weak reference has cleared. Notifying that button can
reapply its compiled command parameter binding after the binding's
weak-reference handle has been released, causing `Handle is not
initialized`.

Keep a short weak reference to the bindable object in the subscription
proxy and resolve it before accessing the dependent handler. If the
short reference has cleared, dispose the subscription instead of
notifying the abandoned object. Retain the existing `DependentHandle` to
preserve the handler's lifetime while the object is live. This adds one
weak reference per subscription.

Add a regression test that delays finalizer processing, collects an
abandoned button with a null compiled command-parameter binding, raises
`CanExecuteChanged`, and verifies that a live button still disables and
re-enables. The gate is bounded and released in `finally`.

### Validation

- On the `net11` base, the new regression fails without this change with
`InvalidOperationException: Handle is not initialized` from
`WeakReference<T>.SetTarget`, and passes with the change.
- Targeted `CommandTests`, `ButtonUnitTest`, and `TypedBindingUnitTests`
on Windows ARM64 with the .NET 11 SDK: 251 passed, 2 skipped. The
selection excluded `TypedBindingUnitTests.FailToConvert`, which also
failed on the unmodified .NET 10 source in earlier local testing.
- The standalone .NET 10 reproduction and Windows host also reproduce
the reported exception without the fix. Testing used simulated button
clicks, not pointer automation.
- Formatted the modified files with `dotnet format whitespace`; `git
diff --check` passes.

The original weak-subscription implementation was introduced by #29837.
A search found no open PR for this exact exception; this change
addresses the stale-callback lifetime window rather than modifying that
PR's command-retention behavior.

### Issues Fixed

Fixes #40198

Copilot-Session: 41443241-c376-48d0-b685-7cfa647534be
Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com>
kubaflo added a commit that referenced this pull request Nov 17, 2026
<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

### Target Branch

- [x] I selected the applicable base branch using the [contribution
guide](https://github.com/dotnet/maui/blob/main/.github/CONTRIBUTING.md#choose-your-pr-base-before-starting).
- `net11`: .NET 11 servicing fix for review.

### Description of Change

When a command outlives a button, the subscription's `DependentHandle`
can expose the button while it is finalization-reachable even after the
button's short weak reference has cleared. Notifying that button can
reapply its compiled command parameter binding after the binding's
weak-reference handle has been released, causing `Handle is not
initialized`.

Keep a short weak reference to the bindable object in the subscription
proxy and resolve it before accessing the dependent handler. If the
short reference has cleared, dispose the subscription instead of
notifying the abandoned object. Retain the existing `DependentHandle` to
preserve the handler's lifetime while the object is live. This adds one
weak reference per subscription.

Add a regression test that delays finalizer processing, collects an
abandoned button with a null compiled command-parameter binding, raises
`CanExecuteChanged`, and verifies that a live button still disables and
re-enables. The gate is bounded and released in `finally`.

### Validation

- On the `net11` base, the new regression fails without this change with
`InvalidOperationException: Handle is not initialized` from
`WeakReference<T>.SetTarget`, and passes with the change.
- Targeted `CommandTests`, `ButtonUnitTest`, and `TypedBindingUnitTests`
on Windows ARM64 with the .NET 11 SDK: 251 passed, 2 skipped. The
selection excluded `TypedBindingUnitTests.FailToConvert`, which also
failed on the unmodified .NET 10 source in earlier local testing.
- The standalone .NET 10 reproduction and Windows host also reproduce
the reported exception without the fix. Testing used simulated button
clicks, not pointer automation.
- Formatted the modified files with `dotnet format whitespace`; `git
diff --check` passes.

The original weak-subscription implementation was introduced by #29837.
A search found no open PR for this exact exception; this change
addresses the stale-callback lifetime window rather than modifying that
PR's command-retention behavior.

### Issues Fixed

Fixes #40198

Copilot-Session: 41443241-c376-48d0-b685-7cfa647534be
Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com>
@Oxymoron290

Copy link
Copy Markdown
Contributor Author

Could this fix be considered for the next available .NET 10 servicing release? The crash reproduces with MAUI 10.0.100 and 10.0.110. The candidate fix and regression were previously validated against 10.0.110, and the net11 regression fails without the fix and passes with it.

The change prevents command notifications to collected controls and adds one weak reference per subscription. Customer-app validation and Android runtime validation remain outstanding.

Could a maintainer apply backport/suggested and confirm the appropriate .NET 10 branch and release?

kubaflo added a commit that referenced this pull request Nov 17, 2026
…0339)

<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

<!--
!!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING
MAIN. !!!!!!!
-->

### Description of Change

Backport of #40199 to `release/10.0.1xx-sr14` (SR14 / 10.0.140, tracking
PR #40222), as requested in
#40199 (comment).

`WeakCommandSubscription.CommandCanExecuteSubscription` now keeps a
`WeakReference<BindableObject>` to the target and resolves it before
invoking the handler. A `DependentHandle` can still expose a
finalization-reachable target after its binding weak references have
been finalized; notifying that target caused the crash. When the target
is gone, the subscription disposes itself. Cost: one weak reference per
subscription.

Clean cherry-pick of 0088a35 (no
conflicts).

Validation on the SR14 base (72615b7) with SDK
10.0.113-servicing.26454.107 (from `global.json`), `net10.0`, mobile
TFMs disabled:
- `dotnet test
src/Controls/tests/Core.UnitTests/Controls.Core.UnitTests.csproj
--filter
"FullyQualifiedName~CommandTests|FullyQualifiedName~WeakCommand"` →
passed 48/48
- `CanExecuteDoesNotRefreshCollectedButtonBindings` with production
change reverted → failed 3/3; restored → passed 3/3
- Full `Controls.Core.UnitTests` → passed 5966, skipped 30, failed 0

Not run: customer-app validation and Android/iOS runtime validation.

### Issues Fixed

Fixes #40198

Co-authored-by: Timothy Sturm <timothysturm@microsoft.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
kubaflo added a commit that referenced this pull request Nov 18, 2026
<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

### Target Branch

- [x] I selected the applicable base branch using the [contribution
guide](https://github.com/dotnet/maui/blob/main/.github/CONTRIBUTING.md#choose-your-pr-base-before-starting).
- `net11`: .NET 11 servicing fix for review.

### Description of Change

When a command outlives a button, the subscription's `DependentHandle`
can expose the button while it is finalization-reachable even after the
button's short weak reference has cleared. Notifying that button can
reapply its compiled command parameter binding after the binding's
weak-reference handle has been released, causing `Handle is not
initialized`.

Keep a short weak reference to the bindable object in the subscription
proxy and resolve it before accessing the dependent handler. If the
short reference has cleared, dispose the subscription instead of
notifying the abandoned object. Retain the existing `DependentHandle` to
preserve the handler's lifetime while the object is live. This adds one
weak reference per subscription.

Add a regression test that delays finalizer processing, collects an
abandoned button with a null compiled command-parameter binding, raises
`CanExecuteChanged`, and verifies that a live button still disables and
re-enables. The gate is bounded and released in `finally`.

### Validation

- On the `net11` base, the new regression fails without this change with
`InvalidOperationException: Handle is not initialized` from
`WeakReference<T>.SetTarget`, and passes with the change.
- Targeted `CommandTests`, `ButtonUnitTest`, and `TypedBindingUnitTests`
on Windows ARM64 with the .NET 11 SDK: 251 passed, 2 skipped. The
selection excluded `TypedBindingUnitTests.FailToConvert`, which also
failed on the unmodified .NET 10 source in earlier local testing.
- The standalone .NET 10 reproduction and Windows host also reproduce
the reported exception without the fix. Testing used simulated button
clicks, not pointer automation.
- Formatted the modified files with `dotnet format whitespace`; `git
diff --check` passes.

The original weak-subscription implementation was introduced by #29837.
A search found no open PR for this exact exception; this change
addresses the stale-callback lifetime window rather than modifying that
PR's command-retention behavior.

### Issues Fixed

Fixes #40198

Copilot-Session: 41443241-c376-48d0-b685-7cfa647534be
Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com>
kubaflo added a commit that referenced this pull request Nov 18, 2026
<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

### Target Branch

- [x] I selected the applicable base branch using the [contribution
guide](https://github.com/dotnet/maui/blob/main/.github/CONTRIBUTING.md#choose-your-pr-base-before-starting).
- `net11`: .NET 11 servicing fix for review.

### Description of Change

When a command outlives a button, the subscription's `DependentHandle`
can expose the button while it is finalization-reachable even after the
button's short weak reference has cleared. Notifying that button can
reapply its compiled command parameter binding after the binding's
weak-reference handle has been released, causing `Handle is not
initialized`.

Keep a short weak reference to the bindable object in the subscription
proxy and resolve it before accessing the dependent handler. If the
short reference has cleared, dispose the subscription instead of
notifying the abandoned object. Retain the existing `DependentHandle` to
preserve the handler's lifetime while the object is live. This adds one
weak reference per subscription.

Add a regression test that delays finalizer processing, collects an
abandoned button with a null compiled command-parameter binding, raises
`CanExecuteChanged`, and verifies that a live button still disables and
re-enables. The gate is bounded and released in `finally`.

### Validation

- On the `net11` base, the new regression fails without this change with
`InvalidOperationException: Handle is not initialized` from
`WeakReference<T>.SetTarget`, and passes with the change.
- Targeted `CommandTests`, `ButtonUnitTest`, and `TypedBindingUnitTests`
on Windows ARM64 with the .NET 11 SDK: 251 passed, 2 skipped. The
selection excluded `TypedBindingUnitTests.FailToConvert`, which also
failed on the unmodified .NET 10 source in earlier local testing.
- The standalone .NET 10 reproduction and Windows host also reproduce
the reported exception without the fix. Testing used simulated button
clicks, not pointer automation.
- Formatted the modified files with `dotnet format whitespace`; `git
diff --check` passes.

The original weak-subscription implementation was introduced by #29837.
A search found no open PR for this exact exception; this change
addresses the stale-callback lifetime window rather than modifying that
PR's command-retention behavior.

### Issues Fixed

Fixes #40198

Copilot-Session: 41443241-c376-48d0-b685-7cfa647534be
Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com>
kubaflo added a commit that referenced this pull request Nov 18, 2026
<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

### Target Branch

- [x] I selected the applicable base branch using the [contribution
guide](https://github.com/dotnet/maui/blob/main/.github/CONTRIBUTING.md#choose-your-pr-base-before-starting).
- `net11`: .NET 11 servicing fix for review.

### Description of Change

When a command outlives a button, the subscription's `DependentHandle`
can expose the button while it is finalization-reachable even after the
button's short weak reference has cleared. Notifying that button can
reapply its compiled command parameter binding after the binding's
weak-reference handle has been released, causing `Handle is not
initialized`.

Keep a short weak reference to the bindable object in the subscription
proxy and resolve it before accessing the dependent handler. If the
short reference has cleared, dispose the subscription instead of
notifying the abandoned object. Retain the existing `DependentHandle` to
preserve the handler's lifetime while the object is live. This adds one
weak reference per subscription.

Add a regression test that delays finalizer processing, collects an
abandoned button with a null compiled command-parameter binding, raises
`CanExecuteChanged`, and verifies that a live button still disables and
re-enables. The gate is bounded and released in `finally`.

### Validation

- On the `net11` base, the new regression fails without this change with
`InvalidOperationException: Handle is not initialized` from
`WeakReference<T>.SetTarget`, and passes with the change.
- Targeted `CommandTests`, `ButtonUnitTest`, and `TypedBindingUnitTests`
on Windows ARM64 with the .NET 11 SDK: 251 passed, 2 skipped. The
selection excluded `TypedBindingUnitTests.FailToConvert`, which also
failed on the unmodified .NET 10 source in earlier local testing.
- The standalone .NET 10 reproduction and Windows host also reproduce
the reported exception without the fix. Testing used simulated button
clicks, not pointer automation.
- Formatted the modified files with `dotnet format whitespace`; `git
diff --check` passes.

The original weak-subscription implementation was introduced by #29837.
A search found no open PR for this exact exception; this change
addresses the stale-callback lifetime window rather than modifying that
PR's command-retention behavior.

### Issues Fixed

Fixes #40198

Copilot-Session: 41443241-c376-48d0-b685-7cfa647534be
Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com>
kubaflo added a commit that referenced this pull request Nov 19, 2026
<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

### Target Branch

- [x] I selected the applicable base branch using the [contribution
guide](https://github.com/dotnet/maui/blob/main/.github/CONTRIBUTING.md#choose-your-pr-base-before-starting).
- `net11`: .NET 11 servicing fix for review.

### Description of Change

When a command outlives a button, the subscription's `DependentHandle`
can expose the button while it is finalization-reachable even after the
button's short weak reference has cleared. Notifying that button can
reapply its compiled command parameter binding after the binding's
weak-reference handle has been released, causing `Handle is not
initialized`.

Keep a short weak reference to the bindable object in the subscription
proxy and resolve it before accessing the dependent handler. If the
short reference has cleared, dispose the subscription instead of
notifying the abandoned object. Retain the existing `DependentHandle` to
preserve the handler's lifetime while the object is live. This adds one
weak reference per subscription.

Add a regression test that delays finalizer processing, collects an
abandoned button with a null compiled command-parameter binding, raises
`CanExecuteChanged`, and verifies that a live button still disables and
re-enables. The gate is bounded and released in `finally`.

### Validation

- On the `net11` base, the new regression fails without this change with
`InvalidOperationException: Handle is not initialized` from
`WeakReference<T>.SetTarget`, and passes with the change.
- Targeted `CommandTests`, `ButtonUnitTest`, and `TypedBindingUnitTests`
on Windows ARM64 with the .NET 11 SDK: 251 passed, 2 skipped. The
selection excluded `TypedBindingUnitTests.FailToConvert`, which also
failed on the unmodified .NET 10 source in earlier local testing.
- The standalone .NET 10 reproduction and Windows host also reproduce
the reported exception without the fix. Testing used simulated button
clicks, not pointer automation.
- Formatted the modified files with `dotnet format whitespace`; `git
diff --check` passes.

The original weak-subscription implementation was introduced by #29837.
A search found no open PR for this exact exception; this change
addresses the stale-callback lifetime window rather than modifying that
PR's command-retention behavior.

### Issues Fixed

Fixes #40198

Copilot-Session: 41443241-c376-48d0-b685-7cfa647534be
Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com>
kubaflo added a commit that referenced this pull request Nov 22, 2026
<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

### Target Branch

- [x] I selected the applicable base branch using the [contribution
guide](https://github.com/dotnet/maui/blob/main/.github/CONTRIBUTING.md#choose-your-pr-base-before-starting).
- `net11`: .NET 11 servicing fix for review.

### Description of Change

When a command outlives a button, the subscription's `DependentHandle`
can expose the button while it is finalization-reachable even after the
button's short weak reference has cleared. Notifying that button can
reapply its compiled command parameter binding after the binding's
weak-reference handle has been released, causing `Handle is not
initialized`.

Keep a short weak reference to the bindable object in the subscription
proxy and resolve it before accessing the dependent handler. If the
short reference has cleared, dispose the subscription instead of
notifying the abandoned object. Retain the existing `DependentHandle` to
preserve the handler's lifetime while the object is live. This adds one
weak reference per subscription.

Add a regression test that delays finalizer processing, collects an
abandoned button with a null compiled command-parameter binding, raises
`CanExecuteChanged`, and verifies that a live button still disables and
re-enables. The gate is bounded and released in `finally`.

### Validation

- On the `net11` base, the new regression fails without this change with
`InvalidOperationException: Handle is not initialized` from
`WeakReference<T>.SetTarget`, and passes with the change.
- Targeted `CommandTests`, `ButtonUnitTest`, and `TypedBindingUnitTests`
on Windows ARM64 with the .NET 11 SDK: 251 passed, 2 skipped. The
selection excluded `TypedBindingUnitTests.FailToConvert`, which also
failed on the unmodified .NET 10 source in earlier local testing.
- The standalone .NET 10 reproduction and Windows host also reproduce
the reported exception without the fix. Testing used simulated button
clicks, not pointer automation.
- Formatted the modified files with `dotnet format whitespace`; `git
diff --check` passes.

The original weak-subscription implementation was introduced by #29837.
A search found no open PR for this exact exception; this change
addresses the stale-callback lifetime window rather than modifying that
PR's command-retention behavior.

### Issues Fixed

Fixes #40198

Copilot-Session: 41443241-c376-48d0-b685-7cfa647534be
Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com>
kubaflo added a commit that referenced this pull request Nov 23, 2026
<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

### Target Branch

- [x] I selected the applicable base branch using the [contribution
guide](https://github.com/dotnet/maui/blob/main/.github/CONTRIBUTING.md#choose-your-pr-base-before-starting).
- `net11`: .NET 11 servicing fix for review.

### Description of Change

When a command outlives a button, the subscription's `DependentHandle`
can expose the button while it is finalization-reachable even after the
button's short weak reference has cleared. Notifying that button can
reapply its compiled command parameter binding after the binding's
weak-reference handle has been released, causing `Handle is not
initialized`.

Keep a short weak reference to the bindable object in the subscription
proxy and resolve it before accessing the dependent handler. If the
short reference has cleared, dispose the subscription instead of
notifying the abandoned object. Retain the existing `DependentHandle` to
preserve the handler's lifetime while the object is live. This adds one
weak reference per subscription.

Add a regression test that delays finalizer processing, collects an
abandoned button with a null compiled command-parameter binding, raises
`CanExecuteChanged`, and verifies that a live button still disables and
re-enables. The gate is bounded and released in `finally`.

### Validation

- On the `net11` base, the new regression fails without this change with
`InvalidOperationException: Handle is not initialized` from
`WeakReference<T>.SetTarget`, and passes with the change.
- Targeted `CommandTests`, `ButtonUnitTest`, and `TypedBindingUnitTests`
on Windows ARM64 with the .NET 11 SDK: 251 passed, 2 skipped. The
selection excluded `TypedBindingUnitTests.FailToConvert`, which also
failed on the unmodified .NET 10 source in earlier local testing.
- The standalone .NET 10 reproduction and Windows host also reproduce
the reported exception without the fix. Testing used simulated button
clicks, not pointer automation.
- Formatted the modified files with `dotnet format whitespace`; `git
diff --check` passes.

The original weak-subscription implementation was introduced by #29837.
A search found no open PR for this exact exception; this change
addresses the stale-callback lifetime window rather than modifying that
PR's command-retention behavior.

### Issues Fixed

Fixes #40198

Copilot-Session: 41443241-c376-48d0-b685-7cfa647534be
Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com>
@kubaflo kubaflo added this to the .NET 11 SR2 milestone Nov 24, 2026
KarthikRajaKalaimani pushed a commit to KarthikRajaKalaimani/maui that referenced this pull request Nov 26, 2026
<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

### Target Branch

- [x] I selected the applicable base branch using the [contribution
guide](https://github.com/dotnet/maui/blob/main/.github/CONTRIBUTING.md#choose-your-pr-base-before-starting).
- `net11`: .NET 11 servicing fix for review.

### Description of Change

When a command outlives a button, the subscription's `DependentHandle`
can expose the button while it is finalization-reachable even after the
button's short weak reference has cleared. Notifying that button can
reapply its compiled command parameter binding after the binding's
weak-reference handle has been released, causing `Handle is not
initialized`.

Keep a short weak reference to the bindable object in the subscription
proxy and resolve it before accessing the dependent handler. If the
short reference has cleared, dispose the subscription instead of
notifying the abandoned object. Retain the existing `DependentHandle` to
preserve the handler's lifetime while the object is live. This adds one
weak reference per subscription.

Add a regression test that delays finalizer processing, collects an
abandoned button with a null compiled command-parameter binding, raises
`CanExecuteChanged`, and verifies that a live button still disables and
re-enables. The gate is bounded and released in `finally`.

### Validation

- On the `net11` base, the new regression fails without this change with
`InvalidOperationException: Handle is not initialized` from
`WeakReference<T>.SetTarget`, and passes with the change.
- Targeted `CommandTests`, `ButtonUnitTest`, and `TypedBindingUnitTests`
on Windows ARM64 with the .NET 11 SDK: 251 passed, 2 skipped. The
selection excluded `TypedBindingUnitTests.FailToConvert`, which also
failed on the unmodified .NET 10 source in earlier local testing.
- The standalone .NET 10 reproduction and Windows host also reproduce
the reported exception without the fix. Testing used simulated button
clicks, not pointer automation.
- Formatted the modified files with `dotnet format whitespace`; `git
diff --check` passes.

The original weak-subscription implementation was introduced by dotnet#29837.
A search found no open PR for this exact exception; this change
addresses the stale-callback lifetime window rather than modifying that
PR's command-retention behavior.

### Issues Fixed

Fixes dotnet#40198

Copilot-Session: 41443241-c376-48d0-b685-7cfa647534be
Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com>

This branch was previously deployed

1 inactive deployment
copilot-pat-pool — 0088a356 Deployed Nov 12, 2026 by Oxymoron290 via conclusion #2922
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community ✨ Community Contribution

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Weak command subscription can notify a collected button and crash compiled binding

3 participants