Skip to content

Fix Socket(SafeSocketHandle) constructor on Apple platforms#124185

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/fix-socketasynceventargstest-failure
Draft

Fix Socket(SafeSocketHandle) constructor on Apple platforms#124185
Copilot wants to merge 2 commits intomainfrom
copilot/fix-socketasynceventargstest-failure

Conversation

Copy link
Contributor

Copilot AI commented Feb 9, 2026

Description

On Apple platforms, GetSocketType cannot query SO_DOMAIN, returning AddressFamily.Unknown. This causes Socket(SafeSocketHandle) to skip _rightEndPoint initialization in its constructor switch, breaking Accept/AcceptAsync (they check _rightEndPoint == null).

Changes

Extract address family from getsockname buffer when _addressFamily is Unknown:

// Socket.cs constructor after getsockname succeeds
AddressFamily socketAddressFamily = _addressFamily;
if (socketAddressFamily == AddressFamily.Unknown && bufferLength >= 2)
{
    socketAddressFamily = SocketAddressPal.GetAddressFamily(buffer.Slice(0, bufferLength));
    _addressFamily = socketAddressFamily;
}
switch (socketAddressFamily) { ... }

Impact: Fixes tvOS test failures in Connect_Parallel_* tests. No effect on Linux/Windows where address family is already known.

Testing

  • Manual testing
  • Automated testing
  • Took best possible action given testing or build constraints
Original prompt

This section details on the original issue you should resolve

<issue_title>Assert.Equal failure in SocketAsyncEventArgsTest.Connect_Parallel_FailsOver (got 127.0.0.1:59851)</issue_title>
<issue_description>

Note: This issue was automatically generated by AI-Test-Triage. Please review and triage accordingly.

Build Information

Build: 20260129.1, 20260129.5

Build error leg or test failing: System.Net.Sockets.Tests.SocketAsyncEventArgsTest.Connect_Parallel_FailsOver, System.Net.Sockets.Tests.SocketAsyncEventArgsTest.Connect_Parallel_Success, System.Net.Sockets.Tests.SocketAsyncEventArgsTest.Connect_Parallel_Fails

Affected CI jobs: build_tvOS_arm64_Release_AllSubsets_Mono

Causing Commit Range Information

Commit range: {TODO: Insert commit range or link to changes}

Summary

  • Assert.Equal failure in SocketAsyncEventArgsTest.Connect_Parallel_FailsOver: Expected null, Actual 127.0.0.1:59851
  • Job: build_tvOS_arm64_Release_AllSubsets_Mono; failing assembly: System.Net.Sockets.Tests
  • Related test Connect_Parallel_Fails timed out with message 'Completed did not get called in time' (execution time ~30.00s)
  • Connect_Parallel_Success also failed with System.InvalidOperationException: 'You must call the Bind method before performing this operation.' in Socket.AcceptAsync

Failure Statistics

Note: These statistics are based on the configured time range and might not reflect the full history of the failure.

  • Occurrence Count: 8
  • First Failure: 2026-01-29 13:02:39 UTC
  • Last Failure: 2026-01-30 07:39:10 UTC

Work Items

Work Item Console Log Build Job Name
System.Net.Sockets.Tests.SocketAsyncEventArgsTest.Connect_Parallel_FailsOver
View Extract
[06:40:47.3989140] 	2026-01-29 03:40:47.535 System.Net.Sockets.Tests[7659:5589543] 	[FAIL] System.Net.Sockets.Tests.SocketAsyncEventArgsTest.Connect_Parallel_Fails
[06:40:47.3990100] Completed did not get called in time
[06:40:47.3990280] at System.Net.Sockets.Tests.SocketAsyncEventArgsTest.Connect_Parallel_Fails()
...
[06:40:47.4664500] 2026-01-29 03:40:47.604 System.Net.Sockets.Tests[7659:5589620] [PASS] System.Net.Sockets.Tests.SocketAsyncEventArgsTest.Connect_Parallel_FailsOver
[06:40:47.4758460] 2026-01-29 03:40:47.613 System.Net.Sockets.Tests[7659:5589620] [FAIL] System.Net.Sockets.Tests.SocketAsyncEventArgsTest.Connect_Parallel_FailsOver(preferIPv6: False)
[06:40:47.4759200] Assert.Equal() Failure: Values differ
[06:40:47.4759340] Expected: null
[06:40:47.4759440] Actual: 127.0.0.1:59851
[06:40:47.4776640] at System.Net.Sockets.Tests.SocketAsyncEventArgsTest.Connect_Parallel_FailsOver(Boolean preferIPv6)
...
[06:48:15.2854480] Execution time: 10.1276581
[06:48:15.2854540] 2026-01-29 03:48:15.331 System.Net.Sockets.Tests[7659:5593880] 7) [FAIL] System.Net.Sockets.Tests.SocketAsyncEventArgsTest.Connect_Parallel_Fails Test name: System.Net.Sockets.Tests.SocketAsyncEventArgsTest.Connect_Parallel_Fails
[06:48:15.2863300] Execution time: 30.0050317
[06:48:15.2863460] [FAIL] System.Net.Sockets.Tests.SocketAsyncEventArgsTest.Connect_Parallel_FailsOver(preferIPv6: False)
[06:48:15.2863550] Expected: null
[06:48:15.2863640] Actual: 127.0.0.1:59851
...
[06:48:15.2872710] Exception messages: System.InvalidOperationException : You must call the Bind method before performing this operation.
[06:48:15.2872710] Exception stack traces: at System.Net.Sockets.Socket.AcceptAsync(SocketAsyncEventArgs e, CancellationToken cancellationToken)
Full Log
20260129.1 build_tvOS_arm64_Release_AllSubsets_Mono
System.Net.Sockets.Tests.SocketAsyncEventArgsTest.Connect_Parallel_Success
View Extract
[06:48:30] dbug: [06:40:47.4981120] 	2026-01-29 03:40:47.636 System.Net.Sockets.Tests[7659:5589620] 	[FAIL] System.Net.Sockets.Tests.SocketAsyncEventArgsTest.Connect_Parallel_Success
[06:48:30] dbug: [06:40:47.4981440] System.InvalidOperationException : You must call the Bind method before performing this operation.
[06:48:30] dbug: [06:40:47.4981610] 2026-01-29 03:40:47.636 System.Net.Sockets.Tests[7659:5589620] at System.Net.Sockets.Socket.AcceptAsync(SocketAsyncEventArgs e, CancellationToken cancellationToken)
[06:48:30] dbug: [06:40:47.4981790] at Sys...

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@dotnet-policy-service
Copy link
Contributor

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

…Apple platforms

On macOS/tvOS/iOS, GetSocketType cannot query SO_DOMAIN to determine the address family,
returning Unknown. This caused Socket(SafeSocketHandle) constructor to fail to initialize
_rightEndPoint, breaking tests that create sockets from handles of already-bound sockets.

The fix extracts the address family from the getsockname result when _addressFamily is Unknown.

Co-authored-by: wfurt <14356188+wfurt@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix Assert.Equal failure in SocketAsyncEventArgsTest Fix Socket(SafeSocketHandle) constructor on Apple platforms Feb 9, 2026
Copilot AI requested a review from wfurt February 9, 2026 17:34
@wfurt
Copy link
Member

wfurt commented Feb 9, 2026

while the change itself seems reasonable, the failed test used TCP so this is unlikely root cause. Also it would be good to add more tests for the SO_DOMAIN. And the code is failing style cop checks as there are undesirable white spaces. Fix it @copilot and check for other reasons why we would get 'You must call the Bind method before performing this operation' for the test run. Perhaps look at some other tests that are passing on tvOS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Assert.Equal failure in SocketAsyncEventArgsTest.Connect_Parallel_FailsOver (got 127.0.0.1:59851)

2 participants