Azure Function occasionally fails because of invalid access to socket

hnnsj 41 Reputation points
2022-04-18T07:16:07.563+00:00

I'm running an Azure Function on a consumption plan that has been up and running in production for a couple of years. In the past two-three weeks the function occasionally (but rarely) fails with the following error message:

An attempt was made to access a socket in a way forbidden by its access permissions.

Some research led me to believe that the App Service Plan it belonged to was running out of available sockets, and I moved the app to its own App Service Plan that no other app belongs to. But after a couple of days the exception started occuring again. Since it's running on a consumption plan there's no way that I'm aware of to control the number of available sockets.

The function runs thousands of times per day but I only get this error perhaps once or twice per day. It has also been running fine for a couple of years until now. So I'm rather sure it's not some obvious misconfiguration.

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

Accepted answer
  1. MughundhanRaveendran-MSFT 12,421 Reputation points
    2022-04-20T13:02:25.283+00:00

    @hnnsj ,

    Thanks for reaching out to Q&A.

    Given the frequency of the issue, it appears to be transient issue which is common in cloud computing world. Having a retry mechanism should take care of transient issues.

    However, if the issue is seen consistently/in a certain pattern, you will have to look into the outbound connections of the function app. The number of available connections in a Consumption plan is limited partly because a function app in this plan runs in a sandbox environment. One of the restrictions that the sandbox imposes on your code is a limit on the number of outbound connections, which is currently 600 active (1,200 total) connections per instance. When you reach this limit, the functions runtime writes the following message to the logs: Host thresholds exceeded: Connections. For more information, see the Functions service limits.

    Please look into this article to manage connections in function app : https://learn.microsoft.com/en-us/azure/azure-functions/manage-connections?tabs=csharp

    I hope this helps!

    Please 'Accept as answer' and ‘Upvote’ if it helped so that it can help others in the community looking for help on similar topics.


1 additional answer

Sort by: Most helpful
  1. Takahito Iwasa 4,841 Reputation points MVP
    2022-04-18T21:27:29.677+00:00

    Hi, @hnnsj

    Perhaps you will have problems when you have reached the outbound limit.
    In the case of App Service Plan, there is an upper limit for each plan.

    https://github.com/MicrosoftDocs/azure-docs/blob/main/includes/functions-limits.md
    https://blog.vjirovsky.cz/azure-functions-and-forbidden-socket-exception/

    As a fundamental measure, in the case of App Service Plan, it is necessary to scale up and consider a mechanism for reusing communication on the application side.

    0 comments No comments