CsvHelper icon indicating copy to clipboard operation
CsvHelper copied to clipboard

Add missing ConfigureAwait(false) calls to avoid deadlocking in synch…

Open MarkPflug opened this issue 2 years ago • 0 comments

…ronized contexts.

This should fix issue #1751. I believe the unmentioned detail in that thread is that they are running a windows forms or blazor context that runs in a SynchronizationContext. This requires that every async callsite uses ConfigureAwait(false) or it will deadlock. This PR just adds the few missing ConfigureAwait(false) calls.

This is the application that I used to repro (winexe):

static async Task Main()
    {
       SynchronizationContext.SetSynchronizationContext(new WindowsFormsSynchronizationContext());

        using var r = File.OpenText("data.csv");

        var config = new CsvConfiguration(CultureInfo.InvariantCulture)
        {
            Delimiter = ";",
            Encoding = Encoding.UTF8
        };
        using var csv = new CsvReader(r, config);

        while (await csv.ReadAsync())
        {
            Debug.WriteLine(csv[0]);
        }
        Console.WriteLine("Done");
    }

MarkPflug avatar Jul 22 '22 18:07 MarkPflug