Skip to content

Possible improvement for Watcher.Dispose() #1591

Closed as not planned
Closed as not planned
@mikhail-barg

Description

@mikhail-barg

Hi!

I think that current implementation of Watcher.Dispose() is a bit lacking:

protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
_cts?.Cancel();
_cts?.Dispose();
}
disposedValue = true;
}
}

specifically between _cts?.Cancel() and _cts?.Dispose() calls, we should await for the _watcherLoop task to finish. Otherwise it might attempt to access the Token of already disposed _cts, which is generally undesirable.

Also possible exception would be unobserved, which in turn may be a problem for applications with strict policies on unobserved exceptions.

What do you think? Will you consider a PR like this:

protected virtual void Dispose(bool disposing)
{
    if (!disposedValue)
    {
        if (disposing)
        {
            _cts.Cancel();
            try 
            {
                _watcherLoop.GetAwaiter().GetResult();
            }
            catch 
            {
                //consume possible exception
            }
            _cts.Dispose();
        }

        disposedValue = true;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    lifecycle/rottenDenotes an issue or PR that has aged beyond stale and will be auto-closed.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions