Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cancellation of UniTask.Delay( ... cancelImmediately: true ) messed up with another UniTask.Delay() methods #553

Closed
Silentor opened this issue Mar 9, 2024 · 1 comment

Comments

@Silentor
Copy link

Silentor commented Mar 9, 2024

Hello and thank you for wonerful Task lib for Unity!
Please take a look at sample code:

public class TestBugScript : MonoBehaviour
    {
        private CancellationTokenSource _cts;

        void Start()
        {
            Task1( this.GetCancellationTokenOnDestroy() ).Forget( Debug.LogException );
            _cts = new CancellationTokenSource();
            Task2CancelImmediately( _cts.Token ).Forget( Debug.LogException );
        }

        private async UniTask Task1( CancellationToken cancel )
        {
            while ( true )
            {
                var timeout = 2;
                Debug.Log( $" Task1 desired timeout {timeout}" );
                var timeoutStart = Time.time;
                await UniTask.Delay( TimeSpan.FromSeconds( timeout ), cancellationToken: cancel );
                Debug.Log( $"Task1 actual timeout {Time.time - timeoutStart}" );

                CancelTask2();
            }
        }

        private void CancelTask2( )
        {
            if ( _cts != null )
            {
                Debug.Log( "Cancel Task2 immediately, Task1 Delay() will be broken" );
                _cts?.Cancel();
                _cts = null;
            }
        }

        private async UniTask Task2CancelImmediately( CancellationToken cancel )
        {
            await UniTask.Delay( TimeSpan.FromSeconds( 5 ), cancellationToken: cancel, cancelImmediately: true  );
        }
    }

Task1() check Delay actual timing in a loop. After first succesfull delay it's cancels Task2() which has a Delay with cancelImmediately: true. After this cancellation, delays in Task1 are not more 2 seconds delay:

Unity_CLP6s05fOo

I maked sure if Task2() delay has cancelImmediately: false, then Task1 delay work as intended.

PS Unity 2022.3.13 Windows 64, UniTask v2.5.3

@hadashiA
Copy link
Contributor

Thank you so much for the code to reproduce the problem.

I fixed it in #557 and it was released today.
I hope you can check it out.

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

No branches or pull requests

2 participants