icsharpcode / SharpZipLib

#ziplib is a Zip, GZip, Tar and BZip2 library written entirely in C# for the .NET platform.
http://icsharpcode.github.io/SharpZipLib/
MIT License
3.73k stars 976 forks source link

fix: use ConfigureAwait(false) on every await #787

Closed flensrocker closed 2 years ago

flensrocker commented 2 years ago

See https://devblogs.microsoft.com/dotnet/configureawait-faq/

General purpose libraries should always use .CongfigureAwait(false) to avoid deadlocks. It may not prevent from every deadlock but is after all a "best practice".

I also added a .globalconfig to warn if ConfigureAwait calls are missing in future changes.

Fixes #786

I certify that I own, and have sufficient rights to contribute, all source code and related material intended to be compiled or integrated with the source code for the SharpZipLib open source product (the "Contribution"). My Contribution is licensed under the MIT License.

codecov[bot] commented 2 years ago

Codecov Report

Merging #787 (c4fd3e2) into master (d2a0c68) will not change coverage. The diff coverage is 73.23%.

@@           Coverage Diff           @@
##           master     #787   +/-   ##
=======================================
  Coverage   74.70%   74.70%           
=======================================
  Files          72       72           
  Lines        8465     8465           
=======================================
  Hits         6324     6324           
  Misses       2141     2141           
Impacted Files Coverage Δ
src/ICSharpCode.SharpZipLib/Zip/ZipFormat.cs 89.54% <42.85%> (ø)
src/ICSharpCode.SharpZipLib/Tar/TarInputStream.cs 63.20% <52.38%> (ø)
...c/ICSharpCode.SharpZipLib/GZip/GzipOutputStream.cs 86.48% <66.66%> (ø)
src/ICSharpCode.SharpZipLib/Tar/TarOutputStream.cs 67.83% <75.00%> (ø)
src/ICSharpCode.SharpZipLib/Core/ByteOrderUtils.cs 100.00% <100.00%> (ø)
src/ICSharpCode.SharpZipLib/Core/StreamUtils.cs 46.00% <100.00%> (ø)
src/ICSharpCode.SharpZipLib/Tar/TarBuffer.cs 73.12% <100.00%> (ø)
...ib/Zip/Compression/Streams/DeflaterOutputStream.cs 82.47% <100.00%> (ø)
src/ICSharpCode.SharpZipLib/Zip/ZipOutputStream.cs 87.82% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

piksel commented 2 years ago

This is absolutely better overall, but it does only cover up the real bug. The "point" of the TarInputStream async implementation is that it's supposed to run entirely sync when being called with isAsync = false ("This async method lacks ‘await’ operators and will run synchronously").

This should mean that a deadlock would never occur.

Adding the ConfigureAwait(false) to all the await calls will increase the performance though, and is a great catch (far too long since I did async library code, I had actually forgot about it), but it will only bypass the bug here by using another thread.

I will keep the Fixes/auto-close though and create a new issue for it, and get this released ASAP.

flensrocker commented 2 years ago

If I find time I will take a look at the generated IL (never had that done before) to see, if GetNextEntry will really be sync all the way. I don't know what the compiler does when it comes to SkipToNextEntry etc.. The awaits are a bit nested until it comes to the TarBuffer.ReadRecordAsync where the isAsync is evaluated.

Thank you for merging!