Azure CosmosDB UpsertItemAsync error

Mark McElhinney 1 Reputation point
2022-01-27T20:04:34.903+00:00

System.Net.Http.HttpRequestException: An attempt was made to access a socket in a way forbidden by its access permissions.

I am seeing this error frequently when calling UpsertItemAsync on my Cosmos records from an Azure Durable Function Activity. This activity is being called many 1000's of times per day, but getting this error 10's of times. I'm new to Cosmos & Azure and don't know where to start to find the root cause.

Any help or guidance appreciated. Thanks in advance.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,263 questions
{count} votes

1 answer

Sort by: Most helpful
  1. MayankBargali-MSFT 68,476 Reputation points
    2022-02-02T13:45:47.63+00:00

    @Mark McElhinney Apology for the delay in reaching out. As per the error, it looks like the socket exception and the reason for this error is that you reached the limit of the maximum available socket at your function app and sandbox disabled access for new sockets.

    It looks like your function app code is creating a new connection to the Cosmo DB for every request coming to your function. You should only create the connection once and reuse it.
    I will suggest you to review managing cosmo DB clients as mentioned in this document and define the static client object while creating the connection.

    Feel free to get back to me if you need any assistance.

    1 person found this answer helpful.