Skip to content

Conversation

@RedOrc
Copy link
Contributor

@RedOrc RedOrc commented Aug 21, 2025

Problem

In Issue 619 it was reported by a user that there was no way to unregister events from the state machine. This leads to a memory leak in long running applications if you no longer want to be notified of transition or transition completed events.

Solution

Provide a way to unregister from both synchronous and asynchronous events. To successfully unregister callbacks, callers must maintain a reference to the original callback to ensure that it is cleared up. Otherwise, they can simply call stateMachine.UnregisterAllCallbacks() in order to remove all callbacks (no reference needed).

Documentation has been updated to reflect these new methods and the OnOffExample program has been tweaked to demonstrate registering and unregistering from callback events.

Changes

  • Updated documentation to reflect new state machine API methods.
  • Updated OnOffExample program to demonstrate the usage of the added APIs in the state machine.
  • Added safety guards to prevent double subscription to the same callback method in both synchronous and asynchronous workflows.
  • Added ability to Unregister from both synchronous and asynchronous workflows. Additional UnregisterAll method has been added to remove all callbacks at once with no reference pointer needed.
  • Added new OnTransitionedEventTest.cs class to specifically test the OnTransitionedEvent.cs class. These tests provide coverage for the entire functionality of the transition event class.

@RedOrc
Copy link
Contributor Author

RedOrc commented Aug 21, 2025

@mclift I've got another one for you 🤠. It's a fix for the aforementioned issue in my description.

@mclift mclift merged commit 5d70090 into dotnet-state-machine:dev Aug 22, 2025
1 check passed
@mclift
Copy link
Member

mclift commented Aug 22, 2025

Thanks for preparing this PR!

@mclift mclift mentioned this pull request Aug 22, 2025
@RedOrc RedOrc deleted the Issue619/ProvideMethodToUnregisterEvent branch August 22, 2025 16:14
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.

2 participants