question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Expose ServicePointManager.DefaultConnectionLimit as host setting

See original GitHub issue

Repro steps

I have a C# function that triggers off of a Service Bus topic subscription and then makes ~10 HTTP calls to external services using HttpClient. The calls are made concurrently (starting the Tasks and then doing an await WhenAll() on them).

Some HTTP calls would fail with this error:

System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: An attempt was made to access a socket in a way forbidden by its access permissions
   at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
   at System.Net.Sockets.Socket.InternalBind(EndPoint localEP)
   at System.Net.Sockets.Socket.BeginConnectEx(EndPoint remoteEP, Boolean flowContext, AsyncCallback callback, Object state)
   at System.Net.Sockets.Socket.UnsafeBeginConnect(EndPoint remoteEP, AsyncCallback callback, Object state)
   at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
   --- End of inner exception stack trace ---
   at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult, TransportContext& context)
   at System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult ar)
   --- End of inner exception stack trace ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Submission#0.<UpdateFoo>d__5.MoveNext() in :line 39

I also started receiving this toast message in the function app portal:

Microsoft.ServiceBus: Could not connect to net.tcp://********.servicebus.windows.net:9354/. The connection attempt lasted for a time span of 00:00:00. TCP error code 10013: An attempt was made to access a socket in a way forbidden by its access permissions. . System: An attempt was made to access a socket in a way forbidden by its access permissions. Session Id: 8a9791233343447d9e0b04ba22e21412

Timestamp: 2016-10-31T05:44:21.084Z

Known workarounds

Turning down the maxConcurrentCalls in host.json to 16 appears to help (but doesn’t eliminate the problem the System.Net.Http.HttpRequestException entirely). More of these exceptions and the Microsoft.ServiceBus errors happen when it is turned up to 64 or higher.

Related information

Perhaps this is related to how I use HttpClient. It is newed up as a static variable at the top of the function and not disposed. Is there a recommended way to use HttpClient in a function?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:28 (15 by maintainers)

github_iconTop GitHub Comments

13reactions
MisinformedDNAcommented, Jan 31, 2017

Part of the selling point for the dynamic tier is

Don’t worry about the infrastructure and provisioning of servers, especially when your Functions call rate scales up. (From https://azure.microsoft.com/en-us/services/functions/)

I used Functions so I could take advantage of this. But because of all the SocketExceptions, I had to switch to a Standard tier, in which case, I wish I would have just stuck with WebJobs so I could use real C# and not C# script.

If it’s supposed to be dynamic, there shouldn’t be a limit to developers at all. Azure Functions should quietly take care of that in the background.

2reactions
brettsamcommented, Feb 23, 2018

It is managed by Functions in production as the Web host increases this limit. But the CLI doesn’t when you’re running locally. This has already been fixed in the CLI; we just haven’t pushed out a release yet: https://github.com/Azure/azure-functions-core-tools/pull/384.

So our next release won’t show this warning anymore. Sorry for the confusion.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ServicePointManager.DefaultConnectionLimit Property
The DefaultConnectionLimit property sets the default maximum number of concurrent connections that the ServicePointManager object assigns to the ConnectionLimit ...
Read more >
Connecting several clients from the same host machine to ...
Set ServicePointManager.DefaultConnectionLimit = 10; at startup and that will give you 10 outbound connections to any host you're ...
Read more >
How to manage HttpClient connections in .NET
In this article, we will learn know how to manage HttpClient connections to avoid sockets exhaustion and DNS issues.
Read more >
ClientConfig Class | AWS SDK for .NET V3
DefaultConnectionLimit is set in which case ServicePointManager.DefaultConnectionLimit will be used as the default. Public Property, CorrectedUtcNow, System.
Read more >
Client ServicePointManager.DefaultConnectionLimit
The DefaultConnectionLimit property sets the default maximum number of concurrent connections that the ServicePointManager object assigns to the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found