Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ internal sealed class ServiceBusListener : IListener, IScaleMonitorProvider, ITa
private readonly string _entityPath;
private readonly bool _isSessionsEnabled;
private readonly bool _autoCompleteMessages;
private readonly CancellationTokenSource _cancellationTokenSource;
private readonly CancellationTokenSource _stoppingCancellationTokenSource;
private readonly CancellationTokenSource _disposingCancellationTokenSource;
private readonly ServiceBusOptions _serviceBusOptions;
private readonly bool _singleDispatch;
private readonly ILogger<ServiceBusListener> _logger;
Expand Down Expand Up @@ -85,7 +86,8 @@ public ServiceBusListener(
_isSessionsEnabled = isSessionsEnabled;
_autoCompleteMessages = autoCompleteMessages;
_triggerExecutor = triggerExecutor;
_cancellationTokenSource = new CancellationTokenSource();
_stoppingCancellationTokenSource = new CancellationTokenSource();
_disposingCancellationTokenSource = new CancellationTokenSource();
_logger = loggerFactory.CreateLogger<ServiceBusListener>();
_functionId = functionId;

Expand Down Expand Up @@ -195,7 +197,7 @@ public async Task StartAsync(CancellationToken cancellationToken)
}
else
{
_batchLoop = RunBatchReceiveLoopAsync(_cancellationTokenSource);
_batchLoop = RunBatchReceiveLoopAsync(_stoppingCancellationTokenSource);
}
}
catch
Expand Down Expand Up @@ -226,7 +228,7 @@ public async Task StopAsync(CancellationToken cancellationToken)
}

// This will also cancel the background monitoring task through the linked cancellation token source.
_cancellationTokenSource.Cancel();
_stoppingCancellationTokenSource.Cancel();

// CloseAsync method stop new messages from being processed while allowing in-flight messages to be processed.
if (_singleDispatch)
Expand Down Expand Up @@ -273,7 +275,7 @@ public async Task StopAsync(CancellationToken cancellationToken)
public void Cancel()
{
ThrowIfDisposed();
_cancellationTokenSource.Cancel();
_stoppingCancellationTokenSource.Cancel();
}

[SuppressMessage("Microsoft.Usage", "CA2213:DisposableFieldsShouldBeDisposed", MessageId = "_cancellationTokenSource")]
Expand All @@ -290,7 +292,8 @@ public void Dispose()
// Mark it canceled but don't dispose of the source while the callers are running.
// Otherwise, callers would receive ObjectDisposedException when calling token.Register.
// For now, rely on finalization to clean up _cancellationTokenSource's wait handle (if allocated).
_cancellationTokenSource.Cancel();
_stoppingCancellationTokenSource.Cancel();
_disposingCancellationTokenSource.Cancel();

if (_batchReceiver.IsValueCreated)
{
Expand Down Expand Up @@ -321,10 +324,14 @@ public void Dispose()
}

_stopAsyncSemaphore.Dispose();
_cancellationTokenSource.Dispose();
_stoppingCancellationTokenSource.Dispose();
_batchReceiveRegistration.Dispose();
_concurrencyUpdateManager?.Dispose();

// No need to dispose the _disposingCancellationTokenSource since we don't create it as a linked token and
// it won't use a timer, so the Dispose method is essentially a no-op. The downside to disposing it is that
// any customers who are trying to use it to cancel their own operations would get an ObjectDisposedException.

Disposed = true;

_logger.LogDebug($"ServiceBus listener disposed({_details.Value})");
Expand All @@ -336,7 +343,7 @@ internal async Task ProcessMessageAsync(ProcessMessageEventArgs args)

_concurrencyUpdateManager?.MessageProcessed();

using (CancellationTokenSource linkedCts = CancellationTokenSource.CreateLinkedTokenSource(args.CancellationToken, _cancellationTokenSource.Token))
using (CancellationTokenSource linkedCts = CancellationTokenSource.CreateLinkedTokenSource(args.CancellationToken, _stoppingCancellationTokenSource.Token))
{
var actions = new ServiceBusMessageActions(args);
if (!await _messageProcessor.Value.BeginProcessingMessageAsync(actions, args.Message, linkedCts.Token).ConfigureAwait(false))
Expand All @@ -349,7 +356,7 @@ internal async Task ProcessMessageAsync(ProcessMessageEventArgs args)

TriggeredFunctionData data = input.GetTriggerFunctionData();

FunctionResult result = await _triggerExecutor.TryExecuteAsync(data, linkedCts.Token).ConfigureAwait(false);
FunctionResult result = await _triggerExecutor.TryExecuteAsync(data, _disposingCancellationTokenSource.Token).ConfigureAwait(false);
try
{
await _messageProcessor.Value.CompleteProcessingMessageAsync(actions, args.Message, result, linkedCts.Token)
Expand All @@ -369,7 +376,7 @@ internal async Task ProcessSessionMessageAsync(ProcessSessionMessageEventArgs ar
_concurrencyUpdateManager?.MessageProcessed();

using (CancellationTokenSource linkedCts =
CancellationTokenSource.CreateLinkedTokenSource(args.CancellationToken, _cancellationTokenSource.Token))
CancellationTokenSource.CreateLinkedTokenSource(args.CancellationToken, _stoppingCancellationTokenSource.Token))
{
var actions = new ServiceBusSessionMessageActions(args);
if (!await _sessionMessageProcessor.Value.BeginProcessingMessageAsync(actions, args.Message, linkedCts.Token)
Expand All @@ -382,7 +389,7 @@ internal async Task ProcessSessionMessageAsync(ProcessSessionMessageEventArgs ar
ServiceBusTriggerInput input = ServiceBusTriggerInput.CreateSingle(args.Message, actions, receiveActions, _client.Value);

TriggeredFunctionData data = input.GetTriggerFunctionData();
FunctionResult result = await _triggerExecutor.TryExecuteAsync(data, linkedCts.Token).ConfigureAwait(false);
FunctionResult result = await _triggerExecutor.TryExecuteAsync(data, _disposingCancellationTokenSource.Token).ConfigureAwait(false);

if (actions.ShouldReleaseSession)
{
Expand Down Expand Up @@ -643,7 +650,7 @@ private async Task TriggerAndCompleteMessagesInternal(ServiceBusReceivedMessage[
scope.SetMessageData(messagesArray);

scope.Start();
FunctionResult result = await _triggerExecutor.TryExecuteAsync(input.GetTriggerFunctionData(), cancellationToken).ConfigureAwait(false);
FunctionResult result = await _triggerExecutor.TryExecuteAsync(input.GetTriggerFunctionData(), _disposingCancellationTokenSource.Token).ConfigureAwait(false);
if (result.Exception != null)
{
scope.Failed(result.Exception);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,29 @@ public async Task TestSingle_AutoCompleteEnabledOnTrigger_CompleteInFunction()
}
}

[Test]
public async Task TestSingle_Dispose()
{
await WriteQueueMessage("{'Name': 'Test1', 'Value': 'Value'}");
var host = BuildHost<TestSingleDispose>();

bool result = _waitHandle1.WaitOne(SBTimeoutMills);
Assert.True(result);
host.Dispose();
}

[Test]
public async Task TestBatch_Dispose()
{
await WriteQueueMessage("{'Name': 'Test1', 'Value': 'Value'}");
var host = BuildHost<TestBatchDispose>();

bool result = _waitHandle1.WaitOne(SBTimeoutMills);
Assert.True(result);
host.Dispose();
_waitHandle2.Set();
}

[Test]
public async Task TestSingle_AutoCompleteDisabledOnTrigger_AbandonsWhenException()
{
Expand Down Expand Up @@ -1588,6 +1611,34 @@ public static async Task RunAsync(
}
}

public class TestSingleDispose
{
public static async Task RunAsync(
[ServiceBusTrigger(FirstQueueNameKey)]
ServiceBusReceivedMessage message,
CancellationToken cancellationToken)
{
_waitHandle1.Set();
// wait a small amount of time for the host to call dispose
await Task.Delay(2000, CancellationToken.None);
Assert.IsTrue(cancellationToken.IsCancellationRequested);
}
}

public class TestBatchDispose
{
public static void RunAsync(
[ServiceBusTrigger(FirstQueueNameKey)]
ServiceBusReceivedMessage[] message,
CancellationToken cancellationToken)
{
_waitHandle1.Set();
bool result = _waitHandle2.WaitOne(SBTimeoutMills);
Assert.IsTrue(result);
Assert.IsTrue(cancellationToken.IsCancellationRequested);
}
}

public class TestCrossEntityTransaction
{
public static async Task RunAsync(
Expand Down Expand Up @@ -1750,8 +1801,7 @@ public static async Task RunAsync(
{
logger.LogInformation($"DrainModeValidationFunctions.QueueNoSessions: message data {msg.Body}");
_drainValidationPreDelay.Set();
await DrainModeHelper.WaitForCancellationAsync(cancellationToken);
Assert.True(cancellationToken.IsCancellationRequested);
Assert.False(cancellationToken.IsCancellationRequested);
await messageActions.CompleteMessageAsync(msg);
_drainValidationPostDelay.Set();
}
Expand All @@ -1768,8 +1818,7 @@ public static async Task RunAsync(
{
logger.LogInformation($"DrainModeValidationFunctions.NoSessions: message data {msg.Body}");
_drainValidationPreDelay.Set();
await DrainModeHelper.WaitForCancellationAsync(cancellationToken);
Assert.True(cancellationToken.IsCancellationRequested);
Assert.False(cancellationToken.IsCancellationRequested);
await messageActions.CompleteMessageAsync(msg);
_drainValidationPostDelay.Set();
}
Expand All @@ -1787,8 +1836,7 @@ public static async Task RunAsync(
Assert.True(array.Length > 0);
logger.LogInformation($"DrainModeTestJobBatch.QueueNoSessionsBatch: received {array.Length} messages");
_drainValidationPreDelay.Set();
await DrainModeHelper.WaitForCancellationAsync(cancellationToken);
Assert.True(cancellationToken.IsCancellationRequested);
Assert.False(cancellationToken.IsCancellationRequested);
foreach (ServiceBusReceivedMessage msg in array)
{
await messageActions.CompleteMessageAsync(msg);
Expand All @@ -1808,8 +1856,7 @@ public static async Task RunAsync(
Assert.True(array.Length > 0);
logger.LogInformation($"DrainModeTestJobBatch.TopicNoSessionsBatch: received {array.Length} messages");
_drainValidationPreDelay.Set();
await DrainModeHelper.WaitForCancellationAsync(cancellationToken);
Assert.True(cancellationToken.IsCancellationRequested);
Assert.False(cancellationToken.IsCancellationRequested);
foreach (ServiceBusReceivedMessage msg in array)
{
// validate that manual lock renewal works
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,29 @@ public async Task TestBatch_ProcessMessagesSpan_FailedScope()
Assert.IsTrue(scope.IsFailed);
}

[Test]
public async Task TestSingle_Dispose()
{
await WriteQueueMessage("{'Name': 'Test1', 'Value': 'Value'}", "sessionId1");
var host = BuildHost<TestSingleDispose>();

bool result = _waitHandle1.WaitOne(SBTimeoutMills);
Assert.True(result);
host.Dispose();
}

[Test]
public async Task TestBatch_Dispose()
{
await WriteQueueMessage("{'Name': 'Test1', 'Value': 'Value'}", "sessionId1");
var host = BuildHost<TestBatchDispose>();

bool result = _waitHandle1.WaitOne(SBTimeoutMills);
Assert.True(result);
host.Dispose();
_waitHandle2.Set();
}

private async Task TestMultiple<T>(bool isXml = false)
{
if (isXml)
Expand Down Expand Up @@ -830,8 +853,7 @@ public static async Task QueueWithSessions(
Assert.AreEqual(msg.PartitionKey, partitionKey);
Assert.AreEqual(msg.TransactionPartitionKey, transactionPartitionKey);
_drainValidationPreDelay.Set();
await DrainModeHelper.WaitForCancellationAsync(cancellationToken);
Assert.True(cancellationToken.IsCancellationRequested);
Assert.False(cancellationToken.IsCancellationRequested);
try
{
await messageActions.CompleteMessageAsync(msg);
Expand All @@ -856,8 +878,7 @@ public static async Task TopicWithSessions(
$"DrainModeValidationFunctions.TopicWithSessions: message data {msg.Body} with session id {msg.SessionId}");
Assert.AreEqual(_drainModeSessionId, msg.SessionId);
_drainValidationPreDelay.Set();
await DrainModeHelper.WaitForCancellationAsync(cancellationToken);
Assert.True(cancellationToken.IsCancellationRequested);
Assert.False(cancellationToken.IsCancellationRequested);
try
{
await messageSession.CompleteMessageAsync(msg);
Expand Down Expand Up @@ -885,8 +906,7 @@ public static async Task QueueWithSessionsBatch(
$"DrainModeTestJobBatch.QueueWithSessionsBatch: received {array.Length} messages with session id {array[0].SessionId}");
Assert.AreEqual(_drainModeSessionId, array[0].SessionId);
_drainValidationPreDelay.Set();
await DrainModeHelper.WaitForCancellationAsync(cancellationToken);
Assert.True(cancellationToken.IsCancellationRequested);
Assert.False(cancellationToken.IsCancellationRequested);
for (int i = 0; i < array.Length; i++)
{
var message = array[i];
Expand Down Expand Up @@ -918,8 +938,7 @@ public static async Task TopicWithSessionsBatch(
$"DrainModeTestJobBatch.TopicWithSessionsBatch: received {array.Length} messages with session id {array[0].SessionId}");
Assert.AreEqual(_drainModeSessionId, array[0].SessionId);
_drainValidationPreDelay.Set();
await DrainModeHelper.WaitForCancellationAsync(cancellationToken);
Assert.True(cancellationToken.IsCancellationRequested);
Assert.False(cancellationToken.IsCancellationRequested);
foreach (ServiceBusReceivedMessage msg in array)
{
await messageSession.CompleteMessageAsync(msg);
Expand Down Expand Up @@ -1160,6 +1179,34 @@ public static void RunAsync(
}
}

public class TestSingleDispose
{
public static async Task RunAsync(
[ServiceBusTrigger(FirstQueueNameKey, IsSessionsEnabled = true)]
ServiceBusReceivedMessage message,
CancellationToken cancellationToken)
{
_waitHandle1.Set();
// wait a small amount of time for the host to call dispose
await Task.Delay(2000, CancellationToken.None);
Assert.IsTrue(cancellationToken.IsCancellationRequested);
}
}

public class TestBatchDispose
{
public static void RunAsync(
[ServiceBusTrigger(FirstQueueNameKey, IsSessionsEnabled = true)]
ServiceBusReceivedMessage[] message,
CancellationToken cancellationToken)
{
_waitHandle1.Set();
bool result = _waitHandle2.WaitOne(SBTimeoutMills);
Assert.IsTrue(result);
Assert.IsTrue(cancellationToken.IsCancellationRequested);
}
}

public class CustomMessagingProvider : MessagingProvider
{
public const string CustomMessagingCategory = "CustomMessagingProvider";
Expand Down