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 BackgroundWorker exiting when OperationCanceledException is not from shutdown request #3284

Merged
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

### Fixes

- `HttpResponse.Content` is no longer disposed by when using `SentryHttpFailedRequestHandler` on .NET Framework, which was causing an ObjectDisposedException when using Sentry with NSwag ([#3306](https://github.com/getsentry/sentry-dotnet/pull/3306))
- `HttpResponse.Content` is no longer disposed by when using `SentryHttpFailedRequestHandler` on .NET Framework, which was causing an ObjectDisposedException when using Sentry with NSwag ([#3306](https://github.com/getsentry/sentry-dotnet/pull/3306))
- Fix BackgroundWorker exiting when OperationCanceledException is not from shutdown request ([3284](https://github.com/getsentry/sentry-dotnet/pull/3284))

## 4.4.0

Expand Down
4 changes: 2 additions & 2 deletions src/Sentry/Internal/BackgroundWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal class BackgroundWorker : IBackgroundWorker, IDisposable
private volatile bool _disposed;
private int _currentItems;

private event EventHandler? OnFlushObjectReceived;
internal event EventHandler? OnFlushObjectReceived;

internal Task WorkerTask { get; }

Expand Down Expand Up @@ -157,7 +157,7 @@ private async Task DoWorkAsync()

await task.ConfigureAwait(false);
}
catch (OperationCanceledException)
catch (OperationCanceledException) when (shutdownTimeout.IsCancellationRequested)
jamescrosswell marked this conversation as resolved.
Show resolved Hide resolved
{
_options.LogInfo(
"Shutdown token triggered. Time to exit. {0} items in queue.",
Expand Down
33 changes: 33 additions & 0 deletions test/Sentry.Tests/Internals/BackgroundWorkerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,39 @@ public void CaptureEvent_InnerTransportThrows_WorkerSuppresses()
// TODO: tap into exception handling (some ILogger or Action<Exception>)
}

[Fact]
public void SendEnvelopeAsync_ThrowsOperationCanceledException_WorkerDoesNotShutdown()
{
// Arrange
var envelope = Envelope.FromEvent(new SentryEvent());

var operationCanceledException = new OperationCanceledException();
_fixture.Transport
.When(e => e.SendEnvelopeAsync(envelope, Arg.Any<CancellationToken>()))
.Do(_ => throw operationCanceledException);

using var sut = _fixture.GetSut();
var envelopeSent = new ManualResetEvent(false);
sut.OnFlushObjectReceived += (sender, args) =>
{
if (sender == envelope)
{
envelopeSent.Set();
}
};

// Act
var queued = sut.EnqueueEnvelope(envelope);

// Assert
if (!envelopeSent.WaitOne(TimeSpan.FromMinutes(1)))
{
throw new TimeoutException("Timeout waiting for envelope to be sent.");
}
_fixture.Logger.DidNotReceive().Log(SentryLevel.Info, "Shutdown token triggered. Time to exit. {0} items in queue.", null, Arg.Any<object[]>());
_fixture.Logger.Received().Log(SentryLevel.Error, "Error while processing envelope (event ID: '{0}'). {1} items in queue.", operationCanceledException, Arg.Any<object[]>());
}

[Fact]
public void QueuedItems_StartsEmpty()
{
Expand Down
Loading