Skip to content

Port exhaustion regression #198

Open

Description

@ntwilson

Using FsHttp with the default httpClientFactory on an Azure Web App to send many HTTP requests to the same domain results in SNAT port exhaustion, with the error

System.Net.Sockets.SocketException (10060): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

From what I can tell, this is because the default factory will create a new HttpClient instance for each request. Microsoft documentation states that if you are not using HttpClientFactory with DI, you should have a singleton HttpClient for the lifetime of your application to avoid the port exhaustion problem.

It looks like this problem was fixed in #37, but then reintroduced later.

Activity

  1. SchlenkR commented on Sep 9, 2024

    @SchlenkR
    Member

    Thank you very much for submitting this issue. The problem here is that FsHttp allows per-request configuration of the underlying SocketsHttpHandler, which then results in a per-request instance of HttpClient. We could think about something like:

    • mark the per-instance httpClientHandlerTransformersand httpClientTransformers as obsolete, and change the behavior of the request generation in a way that if they are not used for a request, a per-process instance of the related SocketsHttpHandler, HttpClient, etc. are used.
    • allow a per-process default configuration of these 2 transformers.
  2. SchlenkR commented on Mar 9, 2026

    @SchlenkR
    Member

    /repo-assist Could you comment on this in general? Backward-compatibility is important here...

  3. github-actions commented on Mar 9, 2026

    @github-actions
  4. added a commit that references this issue on Mar 10, 2026
    c4ee32b
  5. github-actions commented on Mar 10, 2026

    @github-actions
    Contributor

    🤖 This is an automated response from Repo Assist.

    Following up on my earlier analysis: I've submitted a draft PR that implements the fix described as "Option 1" — a process-wide lazy singleton SocketsHttpHandler for the default configuration.

    The key change is in Defaults.fs: the default factory now returns a shared handler (with disposeHandler=false) when no proxy, custom handler transformers, or non-default cert/decompression settings are in play. Each request still gets its own HttpClient wrapper, so per-request Timeout and httpClientTransformers continue to work unchanged. Custom factory configurations are unaffected.

    Please take a look at the PR and let me know if the approach looks good or if you'd prefer a different design.

    Generated by Repo Assist · ◷

    To install this agentic workflow, run

    gh aw add githubnext/agentics/workflows/repo-assist.md@346204513ecfa08b81566450d7d599556807389f
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions