Expose ServicePointManager.DefaultConnectionLimit as host setting
See original GitHub issueRepro 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:
- Created 7 years ago
- Comments:28 (15 by maintainers)
Top GitHub Comments
Part of the selling point for the dynamic tier is
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.
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.