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

Add missing ConfigureAwaits(false) for async using #1276

Merged
merged 12 commits into from
Oct 18, 2021
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## Next
SimonCropp marked this conversation as resolved.
Show resolved Hide resolved

### Fixes

- Add missing `ConfigureAwaits(false)` for `async using` ([#1276](https://github.com/getsentry/sentry-dotnet/pull/1276))


SimonCropp marked this conversation as resolved.
Show resolved Hide resolved
## 3.10.0

### Features
Expand Down
14 changes: 11 additions & 3 deletions src/Sentry/Envelopes/Envelope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,17 @@ value is string valueString &&

private async Task SerializeHeaderAsync(Stream stream, CancellationToken cancellationToken = default)
{
await using var writer = new Utf8JsonWriter(stream);
writer.WriteDictionaryValue(Header);
await writer.FlushAsync(cancellationToken).ConfigureAwait(false);
var writer = new Utf8JsonWriter(stream);

#if NET461 || NETSTANDARD2_0
using (writer)
#else
await using (writer.ConfigureAwait(false))
#endif
{
writer.WriteDictionaryValue(Header);
await writer.FlushAsync(cancellationToken).ConfigureAwait(false);
}
}

/// <inheritdoc />
Expand Down
34 changes: 20 additions & 14 deletions src/Sentry/Envelopes/EnvelopeItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,12 @@ private static async Task SerializeHeaderAsync(
IReadOnlyDictionary<string, object?> header,
CancellationToken cancellationToken = default)
{
await using var writer = new Utf8JsonWriter(stream);
writer.WriteDictionaryValue(header);
await writer.FlushAsync(cancellationToken).ConfigureAwait(false);
var writer = new Utf8JsonWriter(stream);
await using (writer.ConfigureAwait(false))
SimonCropp marked this conversation as resolved.
Show resolved Hide resolved
{
writer.WriteDictionaryValue(header);
await writer.FlushAsync(cancellationToken).ConfigureAwait(false);
}
}

private async Task SerializeHeaderAsync(
Expand All @@ -99,20 +102,23 @@ public async Task SerializeAsync(Stream stream, CancellationToken cancellationTo
// Length is NOT known (need to calculate)
else
{
#if !NET461 && !NETSTANDARD2_0
await
var payloadBuffer = await BufferPayloadAsync(cancellationToken).ConfigureAwait(false);
#if NET461 || NETSTANDARD2_0
using (payloadBuffer)
#else
await using (payloadBuffer.ConfigureAwait(false))
#endif
using var payloadBuffer = await BufferPayloadAsync(cancellationToken).ConfigureAwait(false);

// Header
var headerWithLength = Header.ToDictionary();
headerWithLength[LengthKey] = payloadBuffer.Length;
{
// Header
var headerWithLength = Header.ToDictionary();
headerWithLength[LengthKey] = payloadBuffer.Length;

await SerializeHeaderAsync(stream, headerWithLength, cancellationToken).ConfigureAwait(false);
await stream.WriteByteAsync((byte)'\n', cancellationToken).ConfigureAwait(false);
await SerializeHeaderAsync(stream, headerWithLength, cancellationToken).ConfigureAwait(false);
await stream.WriteByteAsync((byte)'\n', cancellationToken).ConfigureAwait(false);

// Payload
await payloadBuffer.CopyToAsync(stream, cancellationToken).ConfigureAwait(false);
// Payload
await payloadBuffer.CopyToAsync(stream, cancellationToken).ConfigureAwait(false);
}
}
}

Expand Down
14 changes: 11 additions & 3 deletions src/Sentry/Envelopes/JsonSerializable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,17 @@ internal sealed class JsonSerializable : ISerializable
/// <inheritdoc />
public async Task SerializeAsync(Stream stream, CancellationToken cancellationToken = default)
{
await using var writer = new Utf8JsonWriter(stream);
Source.WriteTo(writer);
await writer.FlushAsync(cancellationToken).ConfigureAwait(false);
var writer = new Utf8JsonWriter(stream);

#if NET461 || NETSTANDARD2_0
using (writer)
#else
await using (writer.ConfigureAwait(false))
#endif
{
Source.WriteTo(writer);
await writer.FlushAsync(cancellationToken).ConfigureAwait(false);
}
}
}
}
14 changes: 9 additions & 5 deletions src/Sentry/Internal/Extensions/HttpClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ public static async Task<JsonElement> ReadAsJsonAsync(
this HttpContent content,
CancellationToken cancellationToken = default)
{
#if !NET461 && !NETSTANDARD2_0
await
var stream = await content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
#if NET461 || NETSTANDARD2_0
using (stream)
#else
await using (stream.ConfigureAwait(false))
#endif
using var stream = await content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
using var jsonDocument = await JsonDocument.ParseAsync(stream, default, cancellationToken).ConfigureAwait(false);
{
using var document = await JsonDocument.ParseAsync(stream, default, cancellationToken).ConfigureAwait(false);

return jsonDocument.RootElement.Clone();
return document.RootElement.Clone();
}
}
}
}
50 changes: 30 additions & 20 deletions src/Sentry/Internal/Http/CachingTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,22 +156,7 @@ private async Task ProcessCacheAsync(CancellationToken cancellationToken = defau

try
{
#if !NET461 && !NETSTANDARD2_0
await
#endif
using var envelopeFile = File.OpenRead(envelopeFilePath);
using var envelope = await Envelope.DeserializeAsync(envelopeFile, cancellationToken)
.ConfigureAwait(false);

_options.LogDebug(
"Sending cached envelope: {0}",
envelope.TryGetEventId());

await _innerTransport.SendEnvelopeAsync(envelope, cancellationToken).ConfigureAwait(false);

_options.LogDebug(
"Successfully sent cached envelope: {0}",
envelope.TryGetEventId());
await InnerProcessCacheAsync(cancellationToken, envelopeFilePath).ConfigureAwait(false);
}
catch (Exception ex) when (IsRetryable(ex))
{
Expand All @@ -198,6 +183,30 @@ private async Task ProcessCacheAsync(CancellationToken cancellationToken = defau
}
}

private async Task InnerProcessCacheAsync(CancellationToken cancellationToken, string envelopeFilePath)
{
var envelopeFile = File.OpenRead(envelopeFilePath);
#if NET461 || NETSTANDARD2_0
using (envelopeFile)
#else
await using (envelopeFile.ConfigureAwait(false))
#endif
{
using var envelope = await Envelope.DeserializeAsync(envelopeFile, cancellationToken)
.ConfigureAwait(false);

_options.LogDebug(
"Sending cached envelope: {0}",
envelope.TryGetEventId());

await _innerTransport.SendEnvelopeAsync(envelope, cancellationToken).ConfigureAwait(false);

_options.LogDebug(
"Successfully sent cached envelope: {0}",
envelope.TryGetEventId());
}
}

// Loading an Envelope only reads the headers. The payload is read lazily, so we do Disk -> Network I/O
// via stream directly instead of loading the whole file in memory. For that reason capturing an envelope
// from disk could raise an IOException related to Disk I/O.
Expand Down Expand Up @@ -263,11 +272,12 @@ private async Task StoreToCacheAsync(
EnsureFreeSpaceInCache();

Directory.CreateDirectory(_isolatedCacheDirectoryPath);

#if !NET461 && !NETSTANDARD2_0
await
var stream = File.Create(envelopeFilePath);
#if NET461 || NETSTANDARD2_0
using(stream)
#else
await using (stream.ConfigureAwait(false))
#endif
using (var stream = File.Create(envelopeFilePath))
{
await envelope.SerializeAsync(stream, cancellationToken).ConfigureAwait(false);
}
Expand Down
8 changes: 5 additions & 3 deletions src/Sentry/Internal/Http/GzipBufferedRequestBodyHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ protected override async Task<HttpResponseMessage> SendAsync(
var memoryStream = new MemoryStream();
if (request.Content is not null)
{
#if !NET461 && !NETSTANDARD2_0
await
var gzipStream = new GZipStream(memoryStream, _compressionLevel, leaveOpen: true);
#if NET461 || NETSTANDARD2_0
using (gzipStream)
#else
await using (gzipStream)
#endif
using (var gzipStream = new GZipStream(memoryStream, _compressionLevel, leaveOpen: true))
{
await request.Content.CopyToAsync(gzipStream).ConfigureAwait(false);
}
Expand Down
18 changes: 11 additions & 7 deletions src/Sentry/Internal/Http/HttpTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,18 @@ private async Task HandleFailureAsync(

Directory.CreateDirectory(Path.GetDirectoryName(destination)!);

#if !NET461 && !NETSTANDARD2_0
await
var envelopeFile = File.Create(destination);
#if NET461 || NETSTANDARD2_0
using (envelopeFile)
#else
await using (envelopeFile)
#endif
using var envelopeFile = File.Create(destination);
await processedEnvelope.SerializeAsync(envelopeFile, cancellationToken).ConfigureAwait(false);
await envelopeFile.FlushAsync(cancellationToken).ConfigureAwait(false);
_options.LogInfo("Envelope's {0} bytes written to: {1}",
envelopeFile.Length, destination);
{
await processedEnvelope.SerializeAsync(envelopeFile, cancellationToken).ConfigureAwait(false);
await envelopeFile.FlushAsync(cancellationToken).ConfigureAwait(false);
_options.LogInfo("Envelope's {0} bytes written to: {1}",
envelopeFile.Length, destination);
}
}
}

Expand Down
14 changes: 9 additions & 5 deletions src/Sentry/Internal/SerializableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ public static async Task<string> SerializeToStringAsync(
this ISerializable serializable,
CancellationToken cancellationToken = default)
{
#if !NET461 && !NETSTANDARD2_0
await
var stream = new MemoryStream();
#if NET461 || NETSTANDARD2_0
using (stream)
#else
await using (stream.ConfigureAwait(false))
#endif
using var stream = new MemoryStream();
await serializable.SerializeAsync(stream, cancellationToken).ConfigureAwait(false);
return Encoding.UTF8.GetString(stream.ToArray());
{
await serializable.SerializeAsync(stream, cancellationToken).ConfigureAwait(false);
return Encoding.UTF8.GetString(stream.ToArray());
}
}
}
}