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

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

Merged
merged 1 commit into from Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .globalconfig
@@ -0,0 +1,3 @@
is_global = true
global_level = 1
dotnet_diagnostic.CA2007.severity = warning
3 changes: 3 additions & 0 deletions benchmark/.globalconfig
@@ -0,0 +1,3 @@
is_global = true
global_level = 2
dotnet_diagnostic.CA2007.severity = none
12 changes: 6 additions & 6 deletions src/ICSharpCode.SharpZipLib/Core/ByteOrderUtils.cs
Expand Up @@ -80,7 +80,7 @@ internal static byte[] ReadBytes(this Stream stream, int count)
/// <inheritdoc cref="WriteLEShort"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async Task WriteLEShortAsync(this Stream stream, int value, CT ct)
=> await stream.WriteAsync(SwappedBytes(value), 0, 2, ct);
=> await stream.WriteAsync(SwappedBytes(value), 0, 2, ct).ConfigureAwait(false);

/// <summary> Write a ushort in little endian byte order. </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand All @@ -89,7 +89,7 @@ public static async Task WriteLEShortAsync(this Stream stream, int value, CT ct)
/// <inheritdoc cref="WriteLEUshort"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async Task WriteLEUshortAsync(this Stream stream, ushort value, CT ct)
=> await stream.WriteAsync(SwappedBytes(value), 0, 2, ct);
=> await stream.WriteAsync(SwappedBytes(value), 0, 2, ct).ConfigureAwait(false);

/// <summary> Write an int in little endian byte order. </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand All @@ -98,7 +98,7 @@ public static async Task WriteLEUshortAsync(this Stream stream, ushort value, CT
/// <inheritdoc cref="WriteLEInt"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async Task WriteLEIntAsync(this Stream stream, int value, CT ct)
=> await stream.WriteAsync(SwappedBytes(value), 0, 4, ct);
=> await stream.WriteAsync(SwappedBytes(value), 0, 4, ct).ConfigureAwait(false);

/// <summary> Write a uint in little endian byte order. </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand All @@ -107,7 +107,7 @@ public static async Task WriteLEIntAsync(this Stream stream, int value, CT ct)
/// <inheritdoc cref="WriteLEUint"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async Task WriteLEUintAsync(this Stream stream, uint value, CT ct)
=> await stream.WriteAsync(SwappedBytes(value), 0, 4, ct);
=> await stream.WriteAsync(SwappedBytes(value), 0, 4, ct).ConfigureAwait(false);

/// <summary> Write a long in little endian byte order. </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand All @@ -116,7 +116,7 @@ public static async Task WriteLEUintAsync(this Stream stream, uint value, CT ct)
/// <inheritdoc cref="WriteLELong"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async Task WriteLELongAsync(this Stream stream, long value, CT ct)
=> await stream.WriteAsync(SwappedBytes(value), 0, 8, ct);
=> await stream.WriteAsync(SwappedBytes(value), 0, 8, ct).ConfigureAwait(false);

/// <summary> Write a ulong in little endian byte order. </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand All @@ -125,6 +125,6 @@ public static async Task WriteLELongAsync(this Stream stream, long value, CT ct)
/// <inheritdoc cref="WriteLEUlong"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async Task WriteLEUlongAsync(this Stream stream, ulong value, CT ct)
=> await stream.WriteAsync(SwappedBytes(value), 0, 8, ct);
=> await stream.WriteAsync(SwappedBytes(value), 0, 8, ct).ConfigureAwait(false);
}
}
4 changes: 2 additions & 2 deletions src/ICSharpCode.SharpZipLib/Core/StreamUtils.cs
Expand Up @@ -280,15 +280,15 @@ internal static async Task WriteProcToStreamAsync(this Stream targetStream, Memo
bufferStream.SetLength(0);
writeProc(bufferStream);
bufferStream.Position = 0;
await bufferStream.CopyToAsync(targetStream, 81920, ct);
await bufferStream.CopyToAsync(targetStream, 81920, ct).ConfigureAwait(false);
bufferStream.SetLength(0);
}

internal static async Task WriteProcToStreamAsync(this Stream targetStream, Action<Stream> writeProc, CancellationToken ct)
{
using (var ms = new MemoryStream())
{
await WriteProcToStreamAsync(targetStream, ms, writeProc, ct);
await WriteProcToStreamAsync(targetStream, ms, writeProc, ct).ConfigureAwait(false);
}
}
}
Expand Down
20 changes: 10 additions & 10 deletions src/ICSharpCode.SharpZipLib/GZip/GzipOutputStream.cs
Expand Up @@ -186,14 +186,14 @@ protected override void Dispose(bool disposing)
}
}
}

#if NETSTANDARD2_1_OR_GREATER
/// <inheritdoc cref="DeflaterOutputStream.Dispose"/>
public override async ValueTask DisposeAsync()
{
try
{
await FinishAsync(CancellationToken.None);
await FinishAsync(CancellationToken.None).ConfigureAwait(false);
}
finally
{
Expand All @@ -202,11 +202,11 @@ public override async ValueTask DisposeAsync()
state_ = OutputState.Closed;
if (IsStreamOwner)
{
await baseOutputStream_.DisposeAsync();
await baseOutputStream_.DisposeAsync().ConfigureAwait(false);
}
}

await base.DisposeAsync();
await base.DisposeAsync().ConfigureAwait(false);
}
}
#endif
Expand Down Expand Up @@ -252,8 +252,8 @@ public override void Finish()
/// <inheritdoc cref="Flush"/>
public override async Task FlushAsync(CancellationToken ct)
{
await WriteHeaderAsync();
await base.FlushAsync(ct);
await WriteHeaderAsync().ConfigureAwait(false);
await base.FlushAsync(ct).ConfigureAwait(false);
}


Expand All @@ -263,15 +263,15 @@ public override async Task FinishAsync(CancellationToken ct)
// If no data has been written a header should be added.
if (state_ == OutputState.Header)
{
await WriteHeaderAsync();
await WriteHeaderAsync().ConfigureAwait(false);
}

if (state_ == OutputState.Footer)
{
state_ = OutputState.Finished;
await base.FinishAsync(ct);
await base.FinishAsync(ct).ConfigureAwait(false);
var gzipFooter = GetFooter();
await baseOutputStream_.WriteAsync(gzipFooter, 0, gzipFooter.Length, ct);
await baseOutputStream_.WriteAsync(gzipFooter, 0, gzipFooter.Length, ct).ConfigureAwait(false);
}
}

Expand Down Expand Up @@ -356,7 +356,7 @@ private async Task WriteHeaderAsync()
if (state_ != OutputState.Header) return;
state_ = OutputState.Footer;
var gzipHeader = GetHeader();
await baseOutputStream_.WriteAsync(gzipHeader, 0, gzipHeader.Length);
await baseOutputStream_.WriteAsync(gzipHeader, 0, gzipHeader.Length).ConfigureAwait(false);
}

#endregion Support Routines
Expand Down
22 changes: 11 additions & 11 deletions src/ICSharpCode.SharpZipLib/Tar/TarBuffer.cs
Expand Up @@ -302,7 +302,7 @@ private async ValueTask SkipBlockAsync(CancellationToken ct, bool isAsync)

if (currentBlockIndex >= BlockFactor)
{
if (!await ReadRecordAsync(ct, isAsync))
if (!await ReadRecordAsync(ct, isAsync).ConfigureAwait(false))
{
throw new TarException("Failed to read a record");
}
Expand Down Expand Up @@ -353,7 +353,7 @@ internal async ValueTask ReadBlockIntAsync(byte[] buffer, CancellationToken ct,

if (currentBlockIndex >= BlockFactor)
{
if (!await ReadRecordAsync(ct, isAsync))
if (!await ReadRecordAsync(ct, isAsync).ConfigureAwait(false))
{
throw new TarException("Failed to read a record");
}
Expand Down Expand Up @@ -384,7 +384,7 @@ private async ValueTask<bool> ReadRecordAsync(CancellationToken ct, bool isAsync
while (bytesNeeded > 0)
{
long numBytes = isAsync
? await inputStream.ReadAsync(recordBuffer, offset, bytesNeeded, ct)
? await inputStream.ReadAsync(recordBuffer, offset, bytesNeeded, ct).ConfigureAwait(false)
: inputStream.Read(recordBuffer, offset, bytesNeeded);

//
Expand Down Expand Up @@ -551,7 +551,7 @@ internal async ValueTask WriteBlockAsync(byte[] buffer, int offset, Cancellation

if (currentBlockIndex >= BlockFactor)
{
await WriteRecordAsync(CancellationToken.None, isAsync);
await WriteRecordAsync(CancellationToken.None, isAsync).ConfigureAwait(false);
}

Array.Copy(buffer, offset, recordBuffer, (currentBlockIndex * BlockSize), BlockSize);
Expand All @@ -571,8 +571,8 @@ private async ValueTask WriteRecordAsync(CancellationToken ct, bool isAsync)

if (isAsync)
{
await outputStream.WriteAsync(recordBuffer, 0, RecordSize, ct);
await outputStream.FlushAsync(ct);
await outputStream.WriteAsync(recordBuffer, 0, RecordSize, ct).ConfigureAwait(false);
await outputStream.FlushAsync(ct).ConfigureAwait(false);
}
else
{
Expand Down Expand Up @@ -600,12 +600,12 @@ private async ValueTask WriteFinalRecordAsync(CancellationToken ct, bool isAsync
{
int dataBytes = currentBlockIndex * BlockSize;
Array.Clear(recordBuffer, dataBytes, RecordSize - dataBytes);
await WriteRecordAsync(ct, isAsync);
await WriteRecordAsync(ct, isAsync).ConfigureAwait(false);
}

if (isAsync)
{
await outputStream.FlushAsync(ct);
await outputStream.FlushAsync(ct).ConfigureAwait(false);
}
else
{
Expand All @@ -629,14 +629,14 @@ private async ValueTask CloseAsync(CancellationToken ct, bool isAsync)
{
if (outputStream != null)
{
await WriteFinalRecordAsync(ct, isAsync);
await WriteFinalRecordAsync(ct, isAsync).ConfigureAwait(false);

if (IsStreamOwner)
{
if (isAsync)
{
#if NETSTANDARD2_1_OR_GREATER
await outputStream.DisposeAsync();
await outputStream.DisposeAsync().ConfigureAwait(false);
#else
outputStream.Dispose();
#endif
Expand All @@ -656,7 +656,7 @@ private async ValueTask CloseAsync(CancellationToken ct, bool isAsync)
if (isAsync)
{
#if NETSTANDARD2_1_OR_GREATER
await inputStream.DisposeAsync();
await inputStream.DisposeAsync().ConfigureAwait(false);
#else
inputStream.Dispose();
#endif
Expand Down
44 changes: 22 additions & 22 deletions src/ICSharpCode.SharpZipLib/Tar/TarInputStream.cs
Expand Up @@ -138,7 +138,7 @@ public override void Flush()
/// <param name="cancellationToken"></param>
public override async Task FlushAsync(CancellationToken cancellationToken)
{
await inputStream.FlushAsync(cancellationToken);
await inputStream.FlushAsync(cancellationToken).ConfigureAwait(false);
}

/// <summary>
Expand Down Expand Up @@ -330,7 +330,7 @@ private async ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken ct

while (numToRead > 0)
{
await tarBuffer.ReadBlockIntAsync(recBuf, ct, isAsync);
await tarBuffer.ReadBlockIntAsync(recBuf, ct, isAsync).ConfigureAwait(false);

var sz = (int)numToRead;

Expand Down Expand Up @@ -379,7 +379,7 @@ protected override void Dispose(bool disposing)
/// </summary>
public override async ValueTask DisposeAsync()
{
await tarBuffer.CloseAsync(CancellationToken.None);
await tarBuffer.CloseAsync(CancellationToken.None).ConfigureAwait(false);
}
#endif

Expand Down Expand Up @@ -465,7 +465,7 @@ private async ValueTask SkipAsync(long skipCount, CancellationToken ct, bool isA
for (long num = skipCount; num > 0;)
{
int toRead = num > length ? length : (int)num;
int numRead = await ReadAsync(skipBuf.Memory.Slice(0, toRead), ct, isAsync);
int numRead = await ReadAsync(skipBuf.Memory.Slice(0, toRead), ct, isAsync).ConfigureAwait(false);

if (numRead == -1)
{
Expand Down Expand Up @@ -542,18 +542,18 @@ private async ValueTask<TarEntry> GetNextEntryAsync(CancellationToken ct, bool i

if (currentEntry != null)
{
await SkipToNextEntryAsync(ct, isAsync);
await SkipToNextEntryAsync(ct, isAsync).ConfigureAwait(false);
}

byte[] headerBuf = ArrayPool<byte>.Shared.Rent(TarBuffer.BlockSize);
await tarBuffer.ReadBlockIntAsync(headerBuf, ct, isAsync);
await tarBuffer.ReadBlockIntAsync(headerBuf, ct, isAsync).ConfigureAwait(false);

if (TarBuffer.IsEndOfArchiveBlock(headerBuf))
{
hasHitEOF = true;

// Read the second zero-filled block
await tarBuffer.ReadBlockIntAsync(headerBuf, ct, isAsync);
await tarBuffer.ReadBlockIntAsync(headerBuf, ct, isAsync).ConfigureAwait(false);
}
else
{
Expand Down Expand Up @@ -592,7 +592,7 @@ private async ValueTask<TarEntry> GetNextEntryAsync(CancellationToken ct, bool i
while (numToRead > 0)
{
var length = (numToRead > TarBuffer.BlockSize ? TarBuffer.BlockSize : (int)numToRead);
int numRead = await ReadAsync(nameBuffer.Memory.Slice(0, length), ct, isAsync);
int numRead = await ReadAsync(nameBuffer.Memory.Slice(0, length), ct, isAsync).ConfigureAwait(false);

if (numRead == -1)
{
Expand All @@ -607,16 +607,16 @@ private async ValueTask<TarEntry> GetNextEntryAsync(CancellationToken ct, bool i
longName = longNameBuilder.ToString();
StringBuilderPool.Instance.Return(longNameBuilder);

await SkipToNextEntryAsync(ct, isAsync);
await this.tarBuffer.ReadBlockIntAsync(headerBuf, ct, isAsync);
await SkipToNextEntryAsync(ct, isAsync).ConfigureAwait(false);
await this.tarBuffer.ReadBlockIntAsync(headerBuf, ct, isAsync).ConfigureAwait(false);
}
}
else if (header.TypeFlag == TarHeader.LF_GHDR)
{
// POSIX global extended header
// Ignore things we dont understand completely for now
await SkipToNextEntryAsync(ct, isAsync);
await this.tarBuffer.ReadBlockIntAsync(headerBuf, ct, isAsync);
await SkipToNextEntryAsync(ct, isAsync).ConfigureAwait(false);
await this.tarBuffer.ReadBlockIntAsync(headerBuf, ct, isAsync).ConfigureAwait(false);
}
else if (header.TypeFlag == TarHeader.LF_XHDR)
{
Expand All @@ -629,7 +629,7 @@ private async ValueTask<TarEntry> GetNextEntryAsync(CancellationToken ct, bool i
while (numToRead > 0)
{
var length = (numToRead > nameBuffer.Length ? nameBuffer.Length : (int)numToRead);
int numRead = await ReadAsync(nameBuffer.AsMemory().Slice(0, length), ct, isAsync);
int numRead = await ReadAsync(nameBuffer.AsMemory().Slice(0, length), ct, isAsync).ConfigureAwait(false);

if (numRead == -1)
{
Expand All @@ -647,14 +647,14 @@ private async ValueTask<TarEntry> GetNextEntryAsync(CancellationToken ct, bool i
longName = name;
}

await SkipToNextEntryAsync(ct, isAsync);
await this.tarBuffer.ReadBlockIntAsync(headerBuf, ct, isAsync);
await SkipToNextEntryAsync(ct, isAsync).ConfigureAwait(false);
await this.tarBuffer.ReadBlockIntAsync(headerBuf, ct, isAsync).ConfigureAwait(false);
}
else if (header.TypeFlag == TarHeader.LF_GNU_VOLHDR)
{
// TODO: could show volume name when verbose
await SkipToNextEntryAsync(ct, isAsync);
await this.tarBuffer.ReadBlockIntAsync(headerBuf, ct, isAsync);
await SkipToNextEntryAsync(ct, isAsync).ConfigureAwait(false);
await this.tarBuffer.ReadBlockIntAsync(headerBuf, ct, isAsync).ConfigureAwait(false);
}
else if (header.TypeFlag != TarHeader.LF_NORMAL &&
header.TypeFlag != TarHeader.LF_OLDNORM &&
Expand All @@ -663,8 +663,8 @@ private async ValueTask<TarEntry> GetNextEntryAsync(CancellationToken ct, bool i
header.TypeFlag != TarHeader.LF_DIR)
{
// Ignore things we dont understand completely for now
await SkipToNextEntryAsync(ct, isAsync);
await tarBuffer.ReadBlockIntAsync(headerBuf, ct, isAsync);
await SkipToNextEntryAsync(ct, isAsync).ConfigureAwait(false);
await tarBuffer.ReadBlockIntAsync(headerBuf, ct, isAsync).ConfigureAwait(false);
}

if (entryFactory == null)
Expand Down Expand Up @@ -736,15 +736,15 @@ private async ValueTask CopyEntryContentsAsync(Stream outputStream, Cancellation

while (true)
{
int numRead = await ReadAsync(tempBuffer, ct, isAsync);
int numRead = await ReadAsync(tempBuffer, ct, isAsync).ConfigureAwait(false);
if (numRead <= 0)
{
break;
}

if (isAsync)
{
await outputStream.WriteAsync(tempBuffer, 0, numRead, ct);
await outputStream.WriteAsync(tempBuffer, 0, numRead, ct).ConfigureAwait(false);
}
else
{
Expand All @@ -761,7 +761,7 @@ private async ValueTask SkipToNextEntryAsync(CancellationToken ct, bool isAsync)

if (numToSkip > 0)
{
await SkipAsync(numToSkip, ct, isAsync);
await SkipAsync(numToSkip, ct, isAsync).ConfigureAwait(false);
}

readBuffer?.Dispose();
Expand Down