-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Enable a way to Unregister Message Handler and Session Handler #14021
Changes from 15 commits
8c46a41
db9b322
da77a24
5e1576a
61f0998
2988d02
d3faba2
4b921a6
78aff54
25f6b8d
d4a5589
ecd820b
cd8f044
fff0016
fae7bf1
642dc36
ffc4249
fbd31d0
f4dac4b
9692122
7741ad9
2bb96a5
c0ec74a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,7 +54,11 @@ public class MessageReceiver : ClientEntity, IMessageReceiver | |
int prefetchCount; | ||
long lastPeekedSequenceNumber; | ||
MessageReceivePump receivePump; | ||
// Cancellation token to cancel the message pump. Once this is fired, all future message handling operations registered by application will be | ||
// cancelled. | ||
CancellationTokenSource receivePumpCancellationTokenSource; | ||
// Cancellation token to cancel the inflight message handling operations registered by application in the message pump. | ||
CancellationTokenSource runningTaskCancellationTokenSource; | ||
|
||
/// <summary> | ||
/// Creates a new MessageReceiver from a <see cref="ServiceBusConnectionStringBuilder"/>. | ||
|
@@ -899,6 +903,41 @@ public void RegisterMessageHandler(Func<Message, CancellationToken, Task> handle | |
this.OnMessageHandler(messageHandlerOptions, handler); | ||
} | ||
|
||
/// <summary> | ||
/// Unregister message handler from the receiver if there is an active message handler registered. This operation waits for the completion | ||
/// of inflight receive and message handling operations to finish and unregisters future receives on the message handler which previously | ||
/// registered. | ||
/// </summary> | ||
public async Task UnregisterMessageHandlerAsync() | ||
{ | ||
this.ThrowIfClosed(); | ||
|
||
MessagingEventSource.Log.UnregisterMessageHandlerStart(this.ClientId); | ||
lock (this.messageReceivePumpSyncLock) | ||
{ | ||
if (this.receivePump == null || this.receivePumpCancellationTokenSource.IsCancellationRequested) | ||
{ | ||
// Silently return if handler has already been unregistered. | ||
return; | ||
} | ||
|
||
this.receivePumpCancellationTokenSource.Cancel(); | ||
this.receivePumpCancellationTokenSource.Dispose(); | ||
} | ||
|
||
while (this.receivePump != null | ||
&& this.receivePump.maxConcurrentCallsSemaphoreSlim.CurrentCount < this.receivePump.registerHandlerOptions.MaxConcurrentCalls) | ||
{ | ||
await Task.Delay(10).ConfigureAwait(false); | ||
} | ||
|
||
lock (this.messageReceivePumpSyncLock) | ||
{ | ||
this.receivePump = null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggest cancelling and disposing the runningTaskCancellationTokenSource here. |
||
} | ||
MessagingEventSource.Log.UnregisterMessageHandlerStop(this.ClientId); | ||
} | ||
|
||
/// <summary> | ||
/// Registers a <see cref="ServiceBusPlugin"/> to be used with this receiver. | ||
/// </summary> | ||
|
@@ -1003,6 +1042,9 @@ protected override async Task OnClosingAsync() | |
{ | ||
this.receivePumpCancellationTokenSource.Cancel(); | ||
this.receivePumpCancellationTokenSource.Dispose(); | ||
// For back-compatibility | ||
this.runningTaskCancellationTokenSource.Cancel(); | ||
this.runningTaskCancellationTokenSource.Dispose(); | ||
this.receivePump = null; | ||
} | ||
} | ||
|
@@ -1279,7 +1321,14 @@ protected virtual void OnMessageHandler( | |
} | ||
|
||
this.receivePumpCancellationTokenSource = new CancellationTokenSource(); | ||
this.receivePump = new MessageReceivePump(this, registerHandlerOptions, callback, this.ServiceBusConnection.Endpoint, this.receivePumpCancellationTokenSource.Token); | ||
|
||
// Running task cancellation token source can be reused if previously UnregisterMessageHandlerAsync was called | ||
if (this.runningTaskCancellationTokenSource == null) | ||
{ | ||
this.runningTaskCancellationTokenSource = new CancellationTokenSource(); | ||
} | ||
|
||
this.receivePump = new MessageReceivePump(this, registerHandlerOptions, callback, this.ServiceBusConnection.Endpoint, this.receivePumpCancellationTokenSource.Token, this.runningTaskCancellationTokenSource.Token); | ||
} | ||
|
||
try | ||
|
@@ -1295,6 +1344,8 @@ protected virtual void OnMessageHandler( | |
{ | ||
this.receivePumpCancellationTokenSource.Cancel(); | ||
this.receivePumpCancellationTokenSource.Dispose(); | ||
//this.runningTaskCancellationTokenSource.Cancel(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if we should cancel and/or dispose the runningTaskCancellationTokenSource here since if unregister was called before and there are some pending tasks to be done, if the next register call run into this exception, we would terminate those pending tasks if the cancellation token is fired. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you have a lock around the entire implementation of UnregisterMessageHandler including the while loop waiting for in-flight executions to complete, this situation should not arise. i.e. a call to RegisterMessageHandler while UnregisterMessageHandler is executing will block until UnregisterMessageHandler completes or has an exception (lock released) after which it is safe to cancel and dispose the runningTaskCancellationTokenSource. What is the reason that you don't want to hold the lock while draining in-flight executions? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have await statement inside the while loop so we cannot put that while loop inside the lock since it will potentially cause deadlock. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. Cancelling and disposing is ok here. |
||
//this.runningTaskCancellationTokenSource.Dispose(); | ||
this.receivePump = null; | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not very comfortable with this potential infinite loop. Imagine OnMessage delegate never completes.then UnregisterMessageHandler never completes. This Unregister doesn't guarantee a clean close, but only a timed unregister. I mean it will wait for some time for all OnMessage delegates to finish, but there is no guarantee. We should ideally take some max timeout or some default timeout for the Unregister.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will be important to make this timeout configurable (and a default used if not specified). In Azure Functions, we provide a large amount of time for function execution to complete and need to provide the same amount of time while shutting down (this API will be invoked during shutdown)