Azure / azure-functions-servicebus-extension

Service Bus extension for Azure Functions
MIT License
65 stars 35 forks source link

ServiceBus SessionHandlerOptions.MaxAutoRenewDuration not working as expected #144

Closed hariAtVonto closed 2 years ago

hariAtVonto commented 3 years ago

Issue: I receive an error in my logs saying I have lost the session lock - it seems it occurs when the message is completed.

Stacktrace:

The session lock has expired on the MessageSession. Accept a new MessageSession. 

{"assembly":"Microsoft.Azure.ServiceBus, Version=4.2.1.0, Culture=neutral, PublicKeyToken=7e34167dcc6d6d8c","method":"Microsoft.Azure.ServiceBus.Core.MessageReceiver+<DisposeMessagesAsync>d__101.MoveNext","level":0,"line":0}
{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw","level":1,"line":0}
{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess","level":2,"line":0}
{"assembly":"Microsoft.Azure.ServiceBus, Version=4.2.1.0, Culture=neutral, PublicKeyToken=7e34167dcc6d6d8c","method":"Microsoft.Azure.ServiceBus.RetryPolicy+<RunOperation>d__19.MoveNext","level":3,"line":0}
{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw","level":4,"line":0}
{"assembly":"Microsoft.Azure.ServiceBus, Version=4.2.1.0, Culture=neutral, PublicKeyToken=7e34167dcc6d6d8c","method":"Microsoft.Azure.ServiceBus.RetryPolicy+<RunOperation>d__19.MoveNext","level":5,"line":0}
{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw","level":6,"line":0}
{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess","level":7,"line":0}
{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification","level":8,"line":0}
{"assembly":"Microsoft.Azure.ServiceBus, Version=4.2.1.0, Culture=neutral, PublicKeyToken=7e34167dcc6d6d8c","method":"Microsoft.Azure.ServiceBus.Core.MessageReceiver+<CompleteAsync>d__69.MoveNext","level":9,"line":0}
{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw","level":10,"line":0}
{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess","level":11,"line":0}
{"assembly":"Microsoft.Azure.WebJobs.ServiceBus, Version=4.2.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35","method":"Microsoft.Azure.WebJobs.ServiceBus.Listeners.ServiceBusListener+<>c__DisplayClass35_0+<<StartMessageBatchReceiver>b__0>d.MoveNext","level":12,"line":0}

Repro Steps:

  1. Populated host.json with the following:
    {
    "version": "2.0",
    "logging": {
        "applicationInsights": {
            "samplingSettings": {
                "isEnabled": true
            }
        }
    },
    "extensions": {
        "serviceBus": {
            "prefetchCount": 0,
            "sessionHandlerOptions": {
                "maxAutoRenewDuration": "00:15:00",
                "messageWaitTimeout": "00:00:05",
                "autoComplete": true
            },
            "BatchOptions": {
                "AutoComplete": true,
                "OperationTimeout": "00:00:10"
            }
        }
    }
    }
  2. Created listener to a Service Bus, with the following method signature:
    [FunctionName("UpdateMasterTable")]
        public async static Task Run(
            [ServiceBusTrigger("%QUEUE_NAME%", IsSessionsEnabled=true)] string[] batchMessages,
            ILogger log)
  3. Send messages to the queue

Workaround Updated the host.json to have a messageHandlerOptions.maxAutoRenewDuration as well. This doesn't feel right, my interpretation is that we shouldn't need to add to this Option since this is a session-based listener.

{
    "version": "2.0",
    "logging": {
        "applicationInsights": {
            "samplingSettings": {
                "isEnabled": true
            }
        }
    },
    "extensions": {
        "serviceBus": {
            "prefetchCount": 0,
            "messageHandlerOptions": {
                "maxAutoRenewDuration": "00:15:00"
            },
            "sessionHandlerOptions": {
                "maxAutoRenewDuration": "00:15:00",
                "messageWaitTimeout": "00:00:05",
                "autoComplete": true
            },
            "BatchOptions": {
                "AutoComplete": true,
                "OperationTimeout": "00:00:10"
            }
        }
    }
}

EDIT: workaround didn't work after all

saikiran-karanam commented 2 years ago

@hariAtVonto, Were you able to fix this issue? If yes can you pls share the fix/workaround.

hariAtVonto commented 2 years ago

Just edited original issue - workaround mentioned there does not work. No solution given. Would like a solution for this where possible - please help!

hariAtVonto commented 2 years ago

I have also observed that some items lose their session lock BEFORE the message is completed - which is much worse

adeliab commented 2 years ago

Any solution to this? I'm getting SessionLockLostException in my function when calling AbandonAsync

alrod commented 2 years ago

@hariAtVonto, as described here: "maxAutoLockRenewalDuration - The maximum duration within which the message lock will be renewed automatically. This setting only applies for functions that receive a single message at a time."

you have batch receive(string[] batchMessages), for batch maxAutoLockRenewalDuration = 5mins and you can not adjust the setting in host.json