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 caching transport with attachments #1489

Merged
merged 8 commits into from
Feb 17, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Fixes

- Fix caching transport with attachments ([#1489](https://github.com/getsentry/sentry-dotnet/pull/1489))

## 3.14.0

### Features
Expand Down
21 changes: 7 additions & 14 deletions src/Sentry/Internal/Http/CachingTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,13 @@ private async Task InnerProcessCacheAsync(string file, CancellationToken cancell
{
_options.LogDebug("Reading cached envelope: {0}", file);

using (var envelope = await ReadEnvelope(file, cancellation).ConfigureAwait(false))
var stream = File.OpenRead(file);
#if NET461 || NETSTANDARD2_0
using (stream)
#else
await using (stream.ConfigureAwait(false))
#endif
using (var envelope = await Envelope.DeserializeAsync(stream, cancellation).ConfigureAwait(false))
{
try
{
Expand Down Expand Up @@ -224,19 +230,6 @@ private void LogFailureWithDiscard(string file, Exception ex)
}
}

private static async Task<Envelope> ReadEnvelope(string file, CancellationToken cancellation)
{
var stream = File.OpenRead(file);
#if NET461 || NETSTANDARD2_0
using (stream)
#else
await using (stream.ConfigureAwait(false))
#endif
{
return await Envelope.DeserializeAsync(stream, cancellation).ConfigureAwait(false);
}
}

// 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.
private static bool IsNetworkRelated(Exception exception) =>
Expand Down
55 changes: 55 additions & 0 deletions test/Sentry.Tests/Internals/Http/CachingTransportTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,61 @@ public CachingTransportTests(ITestOutputHelper testOutputHelper)
_logger = new TestOutputDiagnosticLogger(testOutputHelper);
}

[Fact(Timeout = 7000)]
public async Task WithAttachment()
{
// Arrange
using var cacheDirectory = new TempDirectory();
var options = new SentryOptions
{
Dsn = DsnSamples.ValidDsnWithoutSecret,
DiagnosticLogger = _logger,
Debug = true,
CacheDirectoryPath = cacheDirectory.Path
};

Exception exception = null;
var innerTransport = new HttpTransport(options, new HttpClient(new CallbackHttpClientHandler(message =>
{
try
{
message.Content!.ReadAsStringAsync().GetAwaiter().GetResult();
}
catch (Exception readStreamException)
{
exception = readStreamException;
}
})));
await using var transport = new CachingTransport(innerTransport, options);

var tempFile = Path.GetTempFileName();

try
{
var attachment = new Attachment(AttachmentType.Default, new FileAttachmentContent(tempFile), "Attachment.txt", null);
using var envelope = Envelope.FromEvent(new SentryEvent(), attachments: new[] { attachment });

// Act
await transport.SendEnvelopeAsync(envelope);

// Wait until directory is empty
while (Directory.EnumerateFiles(cacheDirectory.Path, "*", SearchOption.AllDirectories).Any() && exception == null)
{
await Task.Delay(100);
}

// Assert
if (exception != null)
{
throw exception;
}
}
finally
{
File.Delete(tempFile);
}
}

[Fact(Timeout = 7000)]
public async Task WorksInBackground()
{
Expand Down