Skip to content

Commit c4fd3e2

Browse files
committed
fix: use ConfigureAwait(false) on every await
See https://devblogs.microsoft.com/dotnet/configureawait-faq/
1 parent d2a0c68 commit c4fd3e2

12 files changed

+91
-82
lines changed

.globalconfig

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
is_global = true
2+
global_level = 1
3+
dotnet_diagnostic.CA2007.severity = warning

benchmark/.globalconfig

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
is_global = true
2+
global_level = 2
3+
dotnet_diagnostic.CA2007.severity = none

src/ICSharpCode.SharpZipLib/Core/ByteOrderUtils.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ internal static byte[] ReadBytes(this Stream stream, int count)
8080
/// <inheritdoc cref="WriteLEShort"/>
8181
[MethodImpl(MethodImplOptions.AggressiveInlining)]
8282
public static async Task WriteLEShortAsync(this Stream stream, int value, CT ct)
83-
=> await stream.WriteAsync(SwappedBytes(value), 0, 2, ct);
83+
=> await stream.WriteAsync(SwappedBytes(value), 0, 2, ct).ConfigureAwait(false);
8484

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

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

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

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

121121
/// <summary> Write a ulong in little endian byte order. </summary>
122122
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -125,6 +125,6 @@ public static async Task WriteLELongAsync(this Stream stream, long value, CT ct)
125125
/// <inheritdoc cref="WriteLEUlong"/>
126126
[MethodImpl(MethodImplOptions.AggressiveInlining)]
127127
public static async Task WriteLEUlongAsync(this Stream stream, ulong value, CT ct)
128-
=> await stream.WriteAsync(SwappedBytes(value), 0, 8, ct);
128+
=> await stream.WriteAsync(SwappedBytes(value), 0, 8, ct).ConfigureAwait(false);
129129
}
130130
}

src/ICSharpCode.SharpZipLib/Core/StreamUtils.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -280,15 +280,15 @@ internal static async Task WriteProcToStreamAsync(this Stream targetStream, Memo
280280
bufferStream.SetLength(0);
281281
writeProc(bufferStream);
282282
bufferStream.Position = 0;
283-
await bufferStream.CopyToAsync(targetStream, 81920, ct);
283+
await bufferStream.CopyToAsync(targetStream, 81920, ct).ConfigureAwait(false);
284284
bufferStream.SetLength(0);
285285
}
286286

287287
internal static async Task WriteProcToStreamAsync(this Stream targetStream, Action<Stream> writeProc, CancellationToken ct)
288288
{
289289
using (var ms = new MemoryStream())
290290
{
291-
await WriteProcToStreamAsync(targetStream, ms, writeProc, ct);
291+
await WriteProcToStreamAsync(targetStream, ms, writeProc, ct).ConfigureAwait(false);
292292
}
293293
}
294294
}

src/ICSharpCode.SharpZipLib/GZip/GzipOutputStream.cs

+10-10
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,14 @@ protected override void Dispose(bool disposing)
186186
}
187187
}
188188
}
189-
189+
190190
#if NETSTANDARD2_1_OR_GREATER
191191
/// <inheritdoc cref="DeflaterOutputStream.Dispose"/>
192192
public override async ValueTask DisposeAsync()
193193
{
194194
try
195195
{
196-
await FinishAsync(CancellationToken.None);
196+
await FinishAsync(CancellationToken.None).ConfigureAwait(false);
197197
}
198198
finally
199199
{
@@ -202,11 +202,11 @@ public override async ValueTask DisposeAsync()
202202
state_ = OutputState.Closed;
203203
if (IsStreamOwner)
204204
{
205-
await baseOutputStream_.DisposeAsync();
205+
await baseOutputStream_.DisposeAsync().ConfigureAwait(false);
206206
}
207207
}
208208

209-
await base.DisposeAsync();
209+
await base.DisposeAsync().ConfigureAwait(false);
210210
}
211211
}
212212
#endif
@@ -252,8 +252,8 @@ public override void Finish()
252252
/// <inheritdoc cref="Flush"/>
253253
public override async Task FlushAsync(CancellationToken ct)
254254
{
255-
await WriteHeaderAsync();
256-
await base.FlushAsync(ct);
255+
await WriteHeaderAsync().ConfigureAwait(false);
256+
await base.FlushAsync(ct).ConfigureAwait(false);
257257
}
258258

259259

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

269269
if (state_ == OutputState.Footer)
270270
{
271271
state_ = OutputState.Finished;
272-
await base.FinishAsync(ct);
272+
await base.FinishAsync(ct).ConfigureAwait(false);
273273
var gzipFooter = GetFooter();
274-
await baseOutputStream_.WriteAsync(gzipFooter, 0, gzipFooter.Length, ct);
274+
await baseOutputStream_.WriteAsync(gzipFooter, 0, gzipFooter.Length, ct).ConfigureAwait(false);
275275
}
276276
}
277277

@@ -356,7 +356,7 @@ private async Task WriteHeaderAsync()
356356
if (state_ != OutputState.Header) return;
357357
state_ = OutputState.Footer;
358358
var gzipHeader = GetHeader();
359-
await baseOutputStream_.WriteAsync(gzipHeader, 0, gzipHeader.Length);
359+
await baseOutputStream_.WriteAsync(gzipHeader, 0, gzipHeader.Length).ConfigureAwait(false);
360360
}
361361

362362
#endregion Support Routines

src/ICSharpCode.SharpZipLib/Tar/TarBuffer.cs

+11-11
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ private async ValueTask SkipBlockAsync(CancellationToken ct, bool isAsync)
302302

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

354354
if (currentBlockIndex >= BlockFactor)
355355
{
356-
if (!await ReadRecordAsync(ct, isAsync))
356+
if (!await ReadRecordAsync(ct, isAsync).ConfigureAwait(false))
357357
{
358358
throw new TarException("Failed to read a record");
359359
}
@@ -384,7 +384,7 @@ private async ValueTask<bool> ReadRecordAsync(CancellationToken ct, bool isAsync
384384
while (bytesNeeded > 0)
385385
{
386386
long numBytes = isAsync
387-
? await inputStream.ReadAsync(recordBuffer, offset, bytesNeeded, ct)
387+
? await inputStream.ReadAsync(recordBuffer, offset, bytesNeeded, ct).ConfigureAwait(false)
388388
: inputStream.Read(recordBuffer, offset, bytesNeeded);
389389

390390
//
@@ -551,7 +551,7 @@ internal async ValueTask WriteBlockAsync(byte[] buffer, int offset, Cancellation
551551

552552
if (currentBlockIndex >= BlockFactor)
553553
{
554-
await WriteRecordAsync(CancellationToken.None, isAsync);
554+
await WriteRecordAsync(CancellationToken.None, isAsync).ConfigureAwait(false);
555555
}
556556

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

572572
if (isAsync)
573573
{
574-
await outputStream.WriteAsync(recordBuffer, 0, RecordSize, ct);
575-
await outputStream.FlushAsync(ct);
574+
await outputStream.WriteAsync(recordBuffer, 0, RecordSize, ct).ConfigureAwait(false);
575+
await outputStream.FlushAsync(ct).ConfigureAwait(false);
576576
}
577577
else
578578
{
@@ -600,12 +600,12 @@ private async ValueTask WriteFinalRecordAsync(CancellationToken ct, bool isAsync
600600
{
601601
int dataBytes = currentBlockIndex * BlockSize;
602602
Array.Clear(recordBuffer, dataBytes, RecordSize - dataBytes);
603-
await WriteRecordAsync(ct, isAsync);
603+
await WriteRecordAsync(ct, isAsync).ConfigureAwait(false);
604604
}
605605

606606
if (isAsync)
607607
{
608-
await outputStream.FlushAsync(ct);
608+
await outputStream.FlushAsync(ct).ConfigureAwait(false);
609609
}
610610
else
611611
{
@@ -629,14 +629,14 @@ private async ValueTask CloseAsync(CancellationToken ct, bool isAsync)
629629
{
630630
if (outputStream != null)
631631
{
632-
await WriteFinalRecordAsync(ct, isAsync);
632+
await WriteFinalRecordAsync(ct, isAsync).ConfigureAwait(false);
633633

634634
if (IsStreamOwner)
635635
{
636636
if (isAsync)
637637
{
638638
#if NETSTANDARD2_1_OR_GREATER
639-
await outputStream.DisposeAsync();
639+
await outputStream.DisposeAsync().ConfigureAwait(false);
640640
#else
641641
outputStream.Dispose();
642642
#endif
@@ -656,7 +656,7 @@ private async ValueTask CloseAsync(CancellationToken ct, bool isAsync)
656656
if (isAsync)
657657
{
658658
#if NETSTANDARD2_1_OR_GREATER
659-
await inputStream.DisposeAsync();
659+
await inputStream.DisposeAsync().ConfigureAwait(false);
660660
#else
661661
inputStream.Dispose();
662662
#endif

src/ICSharpCode.SharpZipLib/Tar/TarInputStream.cs

+22-22
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public override void Flush()
138138
/// <param name="cancellationToken"></param>
139139
public override async Task FlushAsync(CancellationToken cancellationToken)
140140
{
141-
await inputStream.FlushAsync(cancellationToken);
141+
await inputStream.FlushAsync(cancellationToken).ConfigureAwait(false);
142142
}
143143

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

331331
while (numToRead > 0)
332332
{
333-
await tarBuffer.ReadBlockIntAsync(recBuf, ct, isAsync);
333+
await tarBuffer.ReadBlockIntAsync(recBuf, ct, isAsync).ConfigureAwait(false);
334334

335335
var sz = (int)numToRead;
336336

@@ -379,7 +379,7 @@ protected override void Dispose(bool disposing)
379379
/// </summary>
380380
public override async ValueTask DisposeAsync()
381381
{
382-
await tarBuffer.CloseAsync(CancellationToken.None);
382+
await tarBuffer.CloseAsync(CancellationToken.None).ConfigureAwait(false);
383383
}
384384
#endif
385385

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

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

543543
if (currentEntry != null)
544544
{
545-
await SkipToNextEntryAsync(ct, isAsync);
545+
await SkipToNextEntryAsync(ct, isAsync).ConfigureAwait(false);
546546
}
547547

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

551551
if (TarBuffer.IsEndOfArchiveBlock(headerBuf))
552552
{
553553
hasHitEOF = true;
554554

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

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

610-
await SkipToNextEntryAsync(ct, isAsync);
611-
await this.tarBuffer.ReadBlockIntAsync(headerBuf, ct, isAsync);
610+
await SkipToNextEntryAsync(ct, isAsync).ConfigureAwait(false);
611+
await this.tarBuffer.ReadBlockIntAsync(headerBuf, ct, isAsync).ConfigureAwait(false);
612612
}
613613
}
614614
else if (header.TypeFlag == TarHeader.LF_GHDR)
615615
{
616616
// POSIX global extended header
617617
// Ignore things we dont understand completely for now
618-
await SkipToNextEntryAsync(ct, isAsync);
619-
await this.tarBuffer.ReadBlockIntAsync(headerBuf, ct, isAsync);
618+
await SkipToNextEntryAsync(ct, isAsync).ConfigureAwait(false);
619+
await this.tarBuffer.ReadBlockIntAsync(headerBuf, ct, isAsync).ConfigureAwait(false);
620620
}
621621
else if (header.TypeFlag == TarHeader.LF_XHDR)
622622
{
@@ -629,7 +629,7 @@ private async ValueTask<TarEntry> GetNextEntryAsync(CancellationToken ct, bool i
629629
while (numToRead > 0)
630630
{
631631
var length = (numToRead > nameBuffer.Length ? nameBuffer.Length : (int)numToRead);
632-
int numRead = await ReadAsync(nameBuffer.AsMemory().Slice(0, length), ct, isAsync);
632+
int numRead = await ReadAsync(nameBuffer.AsMemory().Slice(0, length), ct, isAsync).ConfigureAwait(false);
633633

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

650-
await SkipToNextEntryAsync(ct, isAsync);
651-
await this.tarBuffer.ReadBlockIntAsync(headerBuf, ct, isAsync);
650+
await SkipToNextEntryAsync(ct, isAsync).ConfigureAwait(false);
651+
await this.tarBuffer.ReadBlockIntAsync(headerBuf, ct, isAsync).ConfigureAwait(false);
652652
}
653653
else if (header.TypeFlag == TarHeader.LF_GNU_VOLHDR)
654654
{
655655
// TODO: could show volume name when verbose
656-
await SkipToNextEntryAsync(ct, isAsync);
657-
await this.tarBuffer.ReadBlockIntAsync(headerBuf, ct, isAsync);
656+
await SkipToNextEntryAsync(ct, isAsync).ConfigureAwait(false);
657+
await this.tarBuffer.ReadBlockIntAsync(headerBuf, ct, isAsync).ConfigureAwait(false);
658658
}
659659
else if (header.TypeFlag != TarHeader.LF_NORMAL &&
660660
header.TypeFlag != TarHeader.LF_OLDNORM &&
@@ -663,8 +663,8 @@ private async ValueTask<TarEntry> GetNextEntryAsync(CancellationToken ct, bool i
663663
header.TypeFlag != TarHeader.LF_DIR)
664664
{
665665
// Ignore things we dont understand completely for now
666-
await SkipToNextEntryAsync(ct, isAsync);
667-
await tarBuffer.ReadBlockIntAsync(headerBuf, ct, isAsync);
666+
await SkipToNextEntryAsync(ct, isAsync).ConfigureAwait(false);
667+
await tarBuffer.ReadBlockIntAsync(headerBuf, ct, isAsync).ConfigureAwait(false);
668668
}
669669

670670
if (entryFactory == null)
@@ -736,15 +736,15 @@ private async ValueTask CopyEntryContentsAsync(Stream outputStream, Cancellation
736736

737737
while (true)
738738
{
739-
int numRead = await ReadAsync(tempBuffer, ct, isAsync);
739+
int numRead = await ReadAsync(tempBuffer, ct, isAsync).ConfigureAwait(false);
740740
if (numRead <= 0)
741741
{
742742
break;
743743
}
744744

745745
if (isAsync)
746746
{
747-
await outputStream.WriteAsync(tempBuffer, 0, numRead, ct);
747+
await outputStream.WriteAsync(tempBuffer, 0, numRead, ct).ConfigureAwait(false);
748748
}
749749
else
750750
{
@@ -761,7 +761,7 @@ private async ValueTask SkipToNextEntryAsync(CancellationToken ct, bool isAsync)
761761

762762
if (numToSkip > 0)
763763
{
764-
await SkipAsync(numToSkip, ct, isAsync);
764+
await SkipAsync(numToSkip, ct, isAsync).ConfigureAwait(false);
765765
}
766766

767767
readBuffer?.Dispose();

0 commit comments

Comments
 (0)