MassTransit with Azure Functions error: "The lock supplied is invalid. Either the lock expired, or the message has already been removed from the queue."
See original GitHub issueHello!
I have an issue when using MassTransit with Azure Functions v1 (.NET Framework 4.6.1). I tried to run a basic Azure Function directly inspired from the sample provided in the project documentation : http://masstransit-project.com/MassTransit/usage/azure-functions.html.
The message is successfuly consumed by the IConsumer, and the Azure Function returns without any problem. However, I get the following error message immediately after the Azure Function execution ends : Microsoft.ServiceBus: The lock supplied is invalid. Either the lock expired, or the message has already been removed from the queue.
It looks like the Azure Functions SDK is expecting the message to still be present in the queue at the end of the function’s execution, in order to remove it. But the message is gone, the BrokeredMessageReceiver has already removed it from the queue by calling BrokeredMessage.CompleteAsync() in its Handle method.
Is there something I’m doing wrong?
Thanks!
SDK and lib versions:
- Azure Functions v1 (.NET Framework 4.6.1)
- Microsoft.NET.Sdk.Functions (1.0.4)
- Newtonsoft.Json (11.0.2)
- MassTransit.WebJobs.ServiceBus (5.1.4)
Sample log
Here is the log of the Azure Function’s execution. You can see that the function succeeds before the error occurs.
[17/08/2018 15:10:19] Host started (3431ms)
[17/08/2018 15:10:19] Job host started
[17/08/2018 15:10:31] Function started (Id=0f93ccf7-1018-4a70-9600-14e2aa2f7095)
[17/08/2018 15:10:31] Executing 'ProcessMessage' (Reason='New ServiceBus message detected on '<queue-name>'.', Id=0f93ccf7-1018-4a70-9600-14e2aa2f7095)
[17/08/2018 15:10:34] Consuming message 123456.
[17/08/2018 15:10:34] Function completed (Success, Id=0f93ccf7-1018-4a70-9600-14e2aa2f7095, Duration=2526ms)
[17/08/2018 15:10:34] Executed 'ProcessMessage' (Succeeded, Id=0f93ccf7-1018-4a70-9600-14e2aa2f7095)
[17/08/2018 15:10:35] A ScriptHost error has occurred
[17/08/2018 15:10:35] Microsoft.ServiceBus: The lock supplied is invalid. Either the lock expired, or the message has already been removed from the queue. TrackingId:555c414a-a5f7-47cb-9fdd-31e7eea9c753_G35_B0, SystemTracker:<servicebus-namespace>:Queue:<queue-name>, Timestamp:8/17/2018 3:10:36 PM.
[17/08/2018 15:10:35] MessageReceiver error (Action=Complete) : Microsoft.ServiceBus.Messaging.MessageLockLostException: The lock supplied is invalid. Either the lock expired, or the message has already been removed from the queue. TrackingId:555c414a-a5f7-47cb-9fdd-31e7eea9c753_G35_B0, SystemTracker:<servicebus-namespace>:Queue:<queue-name>, Timestamp:8/17/2018 3:10:36 PM ---> System.ServiceModel.FaultException`1[System.ServiceModel.ExceptionDetail]: The lock supplied is invalid. Either the lock expired, or the message has already been removed from the queue. TrackingId:555c414a-a5f7-47cb-9fdd-31e7eea9c753_G35_B0, SystemTracker:<servicebus-namespace>:Queue:<queue-name>, Timestamp:8/17/2018 3:10:36 PM
[17/08/2018 15:10:35] à Microsoft.ServiceBus.Messaging.Sbmp.DuplexRequestBindingElement.DuplexRequestSessionChannel.ThrowIfFaultMessage(Message wcfMessage)
[17/08/2018 15:10:35] à Microsoft.ServiceBus.Messaging.Sbmp.DuplexRequestBindingElement.DuplexRequestSessionChannel.HandleMessageReceived(IAsyncResult result)
[17/08/2018 15:10:35] --- Fin de la trace de la pile à partir de l'emplacement précédent au niveau duquel l'exception a été levée ---
[17/08/2018 15:10:35] à Microsoft.ServiceBus.Common.AsyncResult.End[TAsyncResult](IAsyncResult result)
[17/08/2018 15:10:35] à Microsoft.ServiceBus.Messaging.Sbmp.DuplexRequestBindingElement.DuplexRequestSessionChannel.EndRequest(IAsyncResult result)
[17/08/2018 15:10:35] à Microsoft.ServiceBus.Messaging.Channels.ReconnectBindingElement.ReconnectChannelFactory`1.RequestSessionChannel.RequestAsyncResult.<>c.<GetAsyncSteps>b__9_3(RequestAsyncResult thisPtr, IAsyncResult r)
[17/08/2018 15:10:35] à Microsoft.ServiceBus.Messaging.IteratorAsyncResult`1.StepCallback(IAsyncResult result)
[17/08/2018 15:10:35] --- Fin de la trace de la pile à partir de l'emplacement précédent au niveau duquel l'exception a été levée ---
[17/08/2018 15:10:35] à Microsoft.ServiceBus.Common.AsyncResult.End[TAsyncResult](IAsyncResult result)
[17/08/2018 15:10:35] à Microsoft.ServiceBus.Common.AsyncResult`1.End(IAsyncResult asyncResult)
[17/08/2018 15:10:35] à Microsoft.ServiceBus.Messaging.Channels.ReconnectBindingElement.ReconnectChannelFactory`1.RequestSessionChannel.EndRequest(IAsyncResult result)
[17/08/2018 15:10:35] à Microsoft.ServiceBus.Messaging.Sbmp.RedirectBindingElement.RedirectContainerChannelFactory`1.RedirectContainerSessionChannel.RequestAsyncResult.<>c__DisplayClass8_1.<GetAsyncSteps>b__4(RequestAsyncResult thisPtr, IAsyncResult r)
[17/08/2018 15:10:35] à Microsoft.ServiceBus.Messaging.IteratorAsyncResult`1.StepCallback(IAsyncResult result)
[17/08/2018 15:10:35] --- Fin de la trace de la pile à partir de l'emplacement précédent au niveau duquel l'exception a été levée ---
[17/08/2018 15:10:35] à Microsoft.ServiceBus.Common.AsyncResult.End[TAsyncResult](IAsyncResult result)
[17/08/2018 15:10:35] à Microsoft.ServiceBus.Common.AsyncResult`1.End(IAsyncResult asyncResult)
[17/08/2018 15:10:35] à Microsoft.ServiceBus.Messaging.Sbmp.RedirectBindingElement.RedirectContainerChannelFactory`1.RedirectContainerSessionChannel.EndRequest(IAsyncResult result)
[17/08/2018 15:10:35] à Microsoft.ServiceBus.Messaging.Channels.ReconnectBindingElement.ReconnectChannelFactory`1.RequestSessionChannel.RequestAsyncResult.<>c.<GetAsyncSteps>b__9_3(RequestAsyncResult thisPtr, IAsyncResult r)
[17/08/2018 15:10:35] à Microsoft.ServiceBus.Messaging.IteratorAsyncResult`1.StepCallback(IAsyncResult result)
[17/08/2018 15:10:35] --- Fin de la trace de la pile à partir de l'emplacement précédent au niveau duquel l'exception a été levée ---
[17/08/2018 15:10:35] à Microsoft.ServiceBus.Common.AsyncResult.End[TAsyncResult](IAsyncResult result)
[17/08/2018 15:10:35] à Microsoft.ServiceBus.Common.AsyncResult`1.End(IAsyncResult asyncResult)
[17/08/2018 15:10:35] à Microsoft.ServiceBus.Messaging.Channels.ReconnectBindingElement.ReconnectChannelFactory`1.RequestSessionChannel.EndRequest(IAsyncResult result)
[17/08/2018 15:10:35] à Microsoft.ServiceBus.Messaging.Sbmp.SbmpTransactionalAsyncResult`1.<>c.<GetAsyncSteps>b__18_3(TIteratorAsyncResult thisPtr, IAsyncResult a)
[17/08/2018 15:10:35] à Microsoft.ServiceBus.Messaging.IteratorAsyncResult`1.StepCallback(IAsyncResult result)
[17/08/2018 15:10:35] --- Fin de la trace de la pile à partir de l'emplacement précédent au niveau duquel l'exception a été levée ---
[17/08/2018 15:10:35] à Microsoft.ServiceBus.Common.AsyncResult.End[TAsyncResult](IAsyncResult result)
[17/08/2018 15:10:35] à Microsoft.ServiceBus.Messaging.Sbmp.SbmpMessageReceiver.EndUpdateCommand(IAsyncResult result)[17/08/2018 15:10:35] --- Fin de la trace de la pile d'exception interne ---
[17/08/2018 15:10:35] à Microsoft.ServiceBus.Common.AsyncResult.End[TAsyncResult](IAsyncResult result)
[17/08/2018 15:10:35] à Microsoft.ServiceBus.Common.AsyncResult`1.End(IAsyncResult asyncResult)
[17/08/2018 15:10:35] à Microsoft.ServiceBus.Messaging.IteratorAsyncResult`1.<>c__22`1.<CallParallelAsync>b__22_1(TIteratorAsyncResult thisPtr, IAsyncResult r)
[17/08/2018 15:10:35] à Microsoft.ServiceBus.Messaging.IteratorAsyncResult`1.StepCallback(IAsyncResult result)
[17/08/2018 15:10:35] Microsoft.ServiceBus: The lock supplied is invalid. Either the lock expired, or the message has already been removed from the queue. TrackingId:555c414a-a5f7-47cb-9fdd-31e7eea9c753_G35_B0, SystemTracker:<servicebus-namespace>:Queue:<queue-name>, Timestamp:8/17/2018 3:10:36 PM.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:5 (3 by maintainers)

Top Related StackOverflow Question
This isn’t an issue now, and you need to select auto-confirm or whatever in your function config. Works only in the develop branch at this point until 5.2.1 is released.
Once support is moved to Azure Functions v2, wonder if this will still be an issue.