Skip to content

Commit

Permalink
Fix caching transport with attachments (#1489)
Browse files Browse the repository at this point in the history
Co-authored-by: Sentry Github Bot <bot+github-bot@sentry.io>
  • Loading branch information
SimonCropp and getsentry-bot authored Feb 17, 2022
1 parent 7cdd113 commit 32c7143
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 14 deletions.
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

0 comments on commit 32c7143

Please sign in to comment.