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

Inconsistent Disposal of Asynchronous Operations #133

Open
skibitsky opened this issue Oct 10, 2023 · 0 comments
Open

Inconsistent Disposal of Asynchronous Operations #133

skibitsky opened this issue Oct 10, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@skibitsky
Copy link
Member

Across various sections of the codebase, the Dispose method fails to properly terminate ongoing asynchronous operations. This oversight leads to unintended lingering tasks, risking resource leaks and unexpected behaviors. A standardized approach to cancel these operations during disposal is needed.

Consider current implementation of HeartBeat.cs as an example.

// ...

public Task Init()                                                 
{                                                                  
    HeartBeatCancellationToken = new CancellationToken();          
                                                                   
    Task.Run(async () =>                                           
    {                                                              
        while (!HeartBeatCancellationToken.IsCancellationRequested)
        {                                                          
            Pulse();                                               
                                                                   
            await Task.Delay(Interval, HeartBeatCancellationToken);
        }                                                          
    }, HeartBeatCancellationToken);                                
                                                                   
    return Task.CompletedTask;                                     
}                                                                  
                                                                   
// ...                                                             
                                                                   
public void Dispose()                                              
{                                                                  
    Events?.Dispose();                                             
}                                                                  

Correct implementation of Dispose should look like so:

public void Dispose()                                              
{        
    HeartBeatCancellationTokenSource?.Cancel();                                                          
    Events?.Dispose();                                             
}    
@skibitsky skibitsky added the enhancement New feature or request label Oct 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant