is there any way to force stop the ProcessMessageAsync handler when the thread keeps running in background?

ValdoG 0 Reputation points

My http function app tried to make a call to Service Bus to collect messages every 5 minutes and cancel after 5 seconds to response the consumer.

public async Task<IActionResult> Run(             
[HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req,             
ILogger log)         
		async Task MessageHandler(ProcessMessageEventArgs args)
		            string body = args.Message.Body.ToString();
		            var message = JsonConvert.DeserializeObject<JObject>(body);
		            // complete the message. Message is deleted from the queue
		            await args.CompleteMessageAsync(args.Message);
		// handle any errors when receiving messages
		Task ErrorHandler(ProcessErrorEventArgs args)
		            var errorObject = new JObject();
		            errorObject["ErrorMessage"] = $"Error processing the request from the queue: {args.Exception.Message}";
		            return Task.CompletedTask;
		// handler to process any errors
		processor.ProcessErrorAsync += ErrorHandler;
		// cancel the token after 5 secs
		// start process async
		await processor.StartProcessingAsync(tokenSource.Token);
		// end the while loop for any cancelation process (5 secs)
		while (!tokenSource.IsCancellationRequested) {}
		// stop or end process async
		await processor.StopProcessingAsync(tokenSource.Token);


Issue happened when the client/consumer/postman cancel the function app calls say multiple times and it cancel gracefully the call from client/postman.

However, in function app the thread pool still running and when there's a message in the queue it will automatically pick up the message. So, when you call the function app to get the message it say no message to pick up.

Is any one experience this scenario?

To simulate:

  • Call function app and cancel while processing (3x)
  • Send message to service bus and it will automatically be pickup by the message handler.

Workaround is to stop and start the function app to resolve it.

Azure Service Bus
Azure Service Bus
An Azure service that provides cloud messaging as a service and hybrid integration.
558 questions
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,382 questions
Microsoft Technologies based on the .NET software framework.
3,459 questions
{count} votes

1 answer

Sort by: Most helpful
  1. navba-MSFT 17,735 Reputation points Microsoft Employee

    @ValdoG Thanks for your reply. I tried to leverage your above code and sent the GET Request from Postman and immediately cancelled it. I am able to cancel the operation just fine. See the below screenshot for your reference:

    User's image

    Also note that my messages are not picked from SB. Instead it is moving to Deadletter queue as expected.

    User's image

    Sharing the below details:

    Azure Functions Core Tools

    Core Tools Version: 4.0.5390 Commit hash: N/A (64-bit)

    Function Runtime Version:

    Most recent version of Microsoft.Azure.WebJobs and Azure.Messaging.ServiceBus packages.

    0 comments No comments