'Bot-Builder python State Storage in DocDB: 412 Data Store pre-condition not met?

I have a serverless backend on AWS lambda that hosts an MS Teams bot. The state of this bot is stored in CosmosDB. I'm now having issues when I trigger my bot multiple times. I already opened a ticket on GitHub but not sure if this should be seen as a bug or more an issue that is fixable on my end. The issue can be found here: https://github.com/microsoft/botbuilder-python/issues/1909

Context

When I trigger my bot multiple times in a row multiple lambda functions will be activated. These functions will then try to process these messages in parallel. The issue is that they all try to update the bot state in CosmosDb which results in a 412 Precondition failed error.

I have found this issue: https://github.com/microsoft/botframework-sdk/issues/3383 that covers it briefly but not in python. Is this a bug or if not how can this be fixed? From this issue (see link), I understand that setting the e_tag to * could solve this problem but I can't figure out how this can be done with the bot builder framework.

Is it not possible to effectively run a teams bot on a serverless backend?

The error message: (Sorry for the bad formatting)

{
    "code": "PreconditionFailed",
    "message": "Operation cannot be performed because one of the specified precondition is not met., 
    RequestStartTime: 2022-01-30T20:06:38.3979751Z, RequestEndTime: 2022-01-30T20:06:38.3979751Z,  Number of regions attempted:1
    {"systemHistory":
        [{
            "dateUtc":"2022-01-30T20:05:43.2079379Z",
            "cpu":6.250,
            "memory":476859124.000,
            "threadInfo":{
                "isThreadStarving":"False",
                "threadWaitIntervalInMs":0.0108,
                "availableThreads":32764,
                "minThreads":52,
                "maxThreads":32767
            }
        },
        {
            "dateUtc":"2022-01-30T20:05:53.2179598Z",
            "cpu":4.727,
            "memory":476641220.000,
            "threadInfo":{"isThreadStarving":"False",
                "threadWaitIntervalInMs":0.0202,
                "availableThreads":32763,
                "minThreads":52,
                "maxThreads":32767}},
            {"dateUtc":"2022-01-30T20:06:03.2280107Z",
                "cpu":3.988,
                "memory":475937928.000,
                "threadInfo":{"isThreadStarving":"False",
                "threadWaitIntervalInMs":0.009,
                "availableThreads":32763,
                "minThreads":52,
                "maxThreads":32767}},
            {"dateUtc":"2022-01-30T20:06:13.2379856Z",
                "cpu":3.700,
                "memory":476703156.000,
                "threadInfo":{"isThreadStarving":"False",
                "threadWaitIntervalInMs":0.014,
                "availableThreads":32765,
                "minThreads":52,
                "maxThreads":32767}},
            {"dateUtc":"2022-01-30T20:06:23.2479144Z",
                "cpu":3.914,
                "memory":475827548.000,
                "threadInfo":{"isThreadStarving":"False",
                "threadWaitIntervalInMs":0.0186,
                "availableThreads":32764,
                "minThreads":52,
                "maxThreads":32767}},
            {"dateUtc":"2022-01-30T20:06:33.5679731Z",
                "cpu":8.453,
                "memory":476240572.000,
                "threadInfo":{"isThreadStarving":"False",
                "threadWaitIntervalInMs":0.0173,
                "availableThreads":32757,
                "minThreads":52,
                "maxThreads":32767}}]}
    RequestStart: 2022-01-30T20:06:38.3979751Z; ResponseTime: 2022-01-30T20:06:38.3979751Z; StoreResult: StorePhysicalAddress: rntbd://cdb-ms-prod-westeurope1-fd3.documents.azure.com:14063/apps/cecb85d1-f589-4032-ae16-245510f69d75/services/44a960dd-6df5-4912-b28f-882362c13880/partitions/b8c381bf-e212-42d3-ad45-1123a468435b/replicas/132806831075762246p/,
     LSN: 444,
     GlobalCommittedLsn: 444,
     PartitionKeyRangeId: 0,
     IsValid: True,
     StatusCode: 412,
     SubStatusCode: 0,
     RequestCharge: 1.67,
     ItemLSN: -1,
     SessionToken: -1#444,
     UsingLocalLSN: False,
     TransportException: null,
     BELatencyMs: 0.646,
     ActivityId: 8091611d-5cec-4e17-a371-0c9490c5e51c,
     RetryAfterInMs: ,
     TransportRequestTimeline: {"requestTimeline":[{"event": "Created",
         "startTimeUtc": "2022-01-30T20:06:38.3979751Z",
         "durationInMs": 0.0085},
        {"event": "ChannelAcquisitionStarted",
         "startTimeUtc": "2022-01-30T20:06:38.3979836Z",
         "durationInMs": 0.0027},
        {"event": "Pipelined",
         "startTimeUtc": "2022-01-30T20:06:38.3979863Z",
         "durationInMs": 0.093},
        {"event": "Transit Time",
         "startTimeUtc": "2022-01-30T20:06:38.3980793Z",
         "durationInMs": 1.1643},
        {"event": "Received",
         "startTimeUtc": "2022-01-30T20:06:38.3992436Z",
         "durationInMs": 0.0548},
        {"event": "Completed",
         "startTimeUtc": "2022-01-30T20:06:38.3992984Z",
         "durationInMs": 0}],
    "requestSizeInBytes":4346,
        "requestBodySizeInBytes":3629,
        "responseMetadataSizeInBytes":178,
        "responseBodySizeInBytes":60};
     ResourceType: Document,
      OperationType: Upsert
    , Microsoft.Azure.Documents.Common/2.14.0"
}


Solution 1:[1]

• Precondition failed error 412 occurs when you are trying to create a chat which does not support application-only context for that time being as it only supports user context at that time.

Please find below screenshot regarding the delegated permissions to be given to your application as then you would need to use the auth code flow with user participation to get the access token, and then you will be able to use the user token to call the /chats API endpoint to create chats for the user. In this case, you would need to grant the ‘Chat.create’ permissions for above.

Chat.create permissions

Please find the link below for more details regarding the above issue as stated by you: -

https://docs.microsoft.com/en-us/answers/questions/506591/precondition-failed-error-412-when-trying-to-creat.html

https://github.com/tus/tus-resumable-upload-protocol/issues/79

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 KartikBhiwapurkar-MT