Skip to content

Fix ExecutionTask capture order in AsyncRelayCommand - #1220

Open
abu-tahir-0 wants to merge 1 commit into
CommunityToolkit:mainfrom
abu-tahir-0:fix/1219-asyncrelaycommand-race
Open

abu-tahir-0 wants to merge 1 commit into
CommunityToolkit:mainfrom
abu-tahir-0:fix/1219-asyncrelaycommand-race

Conversation

@abu-tahir-0

Copy link
Copy Markdown

Fixes #1219

In AsyncRelayCommand (and AsyncRelayCommand<T>), the ExecutionTask setter raised PropertyChanged for ExecutionTask and IsRunning before capturing isAlreadyCompletedOrNull. A handler that synchronously reacts to IsRunningChanged — for example by re-invoking the task — could replace ExecutionTask, so the post-notification capture read the new task and the IsRunning reset notification was skipped, leaving IsRunning stuck at true.

Change

Capture isAlreadyCompletedOrNull before raising the property-changed events, in both commands.

Tests

  • Added regression tests to Test_AsyncRelayCommand.cs and Test_AsyncRelayCommand{T}.cs covering a synchronous IsRunning handler that replaces the execution task.
  • Baseline (detached b135626d, two new tests only): both fail with System.TimeoutException after 5 seconds — reproducing the missing IsRunning=false notification.
  • Patched: net10.0 test harness — 367 tests passed; focused AsyncRelayCommand filter — 42 passed, including both regressions.
  • dotnet restore reports a pre-existing NU1902 advisory for Microsoft.Build.Tasks.Git 8.0.0; no dependency was changed.

isAlreadyCompletedOrNull was captured after IsRunningChanged was raised,
so a synchronous handler could replace ExecutionTask and make the later
read miss the transition, leaving IsRunning stuck true. Capture it before
the notifications in both the generic and non-generic commands.

This branch has not been deployed

No deployments
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.

race condition about AsyncRelayCommand

1 participant