UnityRef is currently in early development. Some features may be incomplete and/or not functioning.

UNITYREF

Your Pit Stop For Solving ANYTHING in Unity

architecture

UniTask Cancellation & Exception Flow

Under Audit

memory managementevent handling

Unity 2021.3.x - Unity 2023.2.x

Published 12 days ago

Developers face difficulties managing UniTask cancellation, OperationCanceledException handling, and CancellationTokenSource lifecycles, leading to unexpected behaviors like pending tasks or MissingReferenceException in finally blocks.

Issue

 The proper workflow for canceling UniTask operations and handling the OperationCanceledException is a common challenge. Developers often encounter unexpected behavior when using GetCancellationTokenOnDestroy() as a CancellationToken, where the OperationCanceledException does not trigger when stopping the editor. This can result in UniTask instances remaining pending in the tracker even after exiting play mode.Another point of confusion revolves around the necessity and implementation of try/catch blocks for OperationCanceledException. While it seems logical to catch this exception, particularly for scenarios like a user force-closing the application during a transition, developers question the practice of using empty catch blocks. The alternative, removing the catch block, often leads to OperationCanceledException appearing in the console when an asynchronous operation is canceled (e.g., disabling a parent object for UI).Furthermore, the use of finally blocks in conjunction with UniTask cancellation presents its own difficulties. Attempts to place cleanup code that references external objects within a finally block can result in a MissingReferenceException if play mode is exited while the asynchronous operation is active. However, in other parts of the project, finally blocks might work without issue under similar conditions, raising questions about consistent behavior. Developers seek clarification on best practices for handling CancellationToken effectively to avoid exceptions without resorting to empty catch blocks or causing reference issues in finally blocks.

Experimental Fixes
  • Ensure all CancellationTokenSource instances are properly Disposed when they are no longer needed, typically in OnDisable or OnDestroy, to prevent memory leaks and ensure tokens are correctly released.
  • Before accessing external references within finally blocks or after a cancellation, add checks for object validity (e.g., if (gameObject != null) or if (this != null && gameObject.activeInHierarchy)) to avoid MissingReferenceException.
  • Consider using UniTask.SuppressCancellationThrow() or a custom extension method if an OperationCanceledException should be silently ignored without an explicit try-catch block, providing clearer intent.

Editor's Note:

The above fixes have not been verified by our audit team yet. They are provided exclusively for your own technical research. We recommend creating a backup of your project before proceeding with any attempts. Utilize at your own discretion!

Related Posts Haven't quite found a solution to your problem? We think these posts might help you.

Content inspired by a Unity discussion post.