Skip to content

Inconsistent Disposal of Asynchronous Operations #133

Open
@skibitsky

Description

@skibitsky

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();                                             
}    

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions