Skip to content

Avoid socket telemetry errors for pending nonblocking connects#129259

Open
tulior wants to merge 5 commits into
dotnet:mainfrom
tulior:bugfix/sockets-nonblocking-connect-telemetry
Open

Avoid socket telemetry errors for pending nonblocking connects#129259
tulior wants to merge 5 commits into
dotnet:mainfrom
tulior:bugfix/sockets-nonblocking-connect-telemetry

Conversation

@tulior

@tulior tulior commented Jun 10, 2026

Copy link
Copy Markdown

Fixes #129252.

Non-blocking Socket.Connect can report WouldBlock on Windows or InProgress on Unix while the OS connect attempt remains pending. Sockets telemetry treated that result as a failure even though Connect has only reported a pending state.

This keeps the connect Activity, but leaves the status unset and omits error.type for pending results. It also suppresses the corresponding ConnectFailed EventSource event for the same pending states. Other SocketError values keep the existing failure path.

The EventSource part is included because it is the same pending-result case at the event layer. It can be separated if reviewers prefer to keep this PR scoped only to the experimental Activity surface.

This does not change the existing counter limitation: a pending connect that later succeeds is still not counted in outgoing-connections-established. Before this change it was not counted either; the pending result was reported as a failure instead.

The regression test covers the real synchronous Socket.Connect path for a non-blocking loopback connect. It asserts immediately after WouldBlock or InProgress because the Activity starts and stops inside Connect, so no later network completion is needed to verify the telemetry state.

Validation run locally on Windows:

dotnet.cmd build /t:Test /p:XunitMethodName=System.Net.Sockets.Tests.TelemetryTest.Connect_NonBlockingPending_ActivityNotMarkedAsError

dotnet.cmd build /t:Test /p:XUnitClassName=System.Net.Sockets.Tests.TelemetryTest

Copilot AI review requested due to automatic review settings June 10, 2026 20:11
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Jun 10, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR updates sockets telemetry to avoid treating non-blocking Connect “pending” results (WouldBlock/InProgress) as failures, and adds a regression test ensuring the emitted connect Activity is not marked as an error in that scenario.

Changes:

  • Adjust SocketsTelemetry.AfterConnect to skip error status/tagging and failure counting for non-blocking connect pending errors.
  • Add a functional test covering non-blocking connects that initially return pending but ultimately succeed.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/libraries/System.Net.Sockets/tests/FunctionalTests/TelemetryTest.cs Adds a new test validating telemetry Activity is not marked error for pending non-blocking connect.
src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketsTelemetry.cs Avoids reporting WouldBlock/InProgress as connect failures in telemetry.

Comment thread src/libraries/System.Net.Sockets/tests/FunctionalTests/TelemetryTest.cs Outdated
Comment thread src/libraries/System.Net.Sockets/tests/FunctionalTests/TelemetryTest.cs Outdated
@tulior tulior force-pushed the bugfix/sockets-nonblocking-connect-telemetry branch 3 times, most recently from 13d0f09 to c06bcd5 Compare June 10, 2026 20:24
Copilot AI review requested due to automatic review settings June 10, 2026 20:24

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

Comment thread src/libraries/System.Net.Sockets/tests/FunctionalTests/TelemetryTest.cs Outdated
Comment thread src/libraries/System.Net.Sockets/tests/FunctionalTests/TelemetryTest.cs Outdated
@tulior tulior force-pushed the bugfix/sockets-nonblocking-connect-telemetry branch from c06bcd5 to c92a337 Compare June 10, 2026 20:30
@tulior tulior requested a review from Copilot June 10, 2026 20:31

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

@tulior tulior requested a review from Copilot June 10, 2026 20:44

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Comment thread src/libraries/System.Net.Sockets/tests/FunctionalTests/TelemetryTest.cs Outdated
@tulior tulior requested a review from Copilot June 10, 2026 20:54

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

@tulior tulior requested a review from Copilot June 10, 2026 21:14

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Non-blocking Socket.Connect can return WouldBlock on Windows or InProgress on Unix while the OS connect attempt remains pending. Treat those results as pending telemetry states instead of failures.

Leave the emitted connect Activity status unset, omit error.type, and avoid emitting ConnectFailed for these pending results. Add a regression test for the real synchronous Connect path so the pending result is verified through the public socket API.
@tulior tulior force-pushed the bugfix/sockets-nonblocking-connect-telemetry branch from 9bea8f0 to 52e7103 Compare June 10, 2026 21:17
@tulior tulior changed the title Avoid socket errors for pending nonblocking connects Avoid socket telemetry errors for pending nonblocking connects Jun 10, 2026
@tulior tulior marked this pull request as ready for review June 10, 2026 21:18
Copilot AI review requested due to automatic review settings June 10, 2026 21:18

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

@tulior

tulior commented Jun 14, 2026

Copy link
Copy Markdown
Author

Could someone from @dotnet/ncl please take a look when you have a chance? @karelz, tagging based on area-System.Net.Sockets ownership. Thanks!

@liveans liveans left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with this change, @wfurt are you aware of any specific usage of reporting InProgress + WouldBlock over telemetry?

Copilot AI review requested due to automatic review settings June 15, 2026 16:29

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

Comment on lines +164 to +190
await RemoteExecutor.Invoke(static () =>
{
using Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
server.BindToAnonymousPort(IPAddress.Loopback);
server.Listen();

using Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
{
Blocking = false
};

using ActivityRecorder recorder = new ActivityRecorder(ActivitySourceName, ActivityName);

// A non-blocking connect reports WouldBlock (Windows) or InProgress (Unix) by design while
// the attempt continues in the background. The "socket connect" activity is started and
// stopped synchronously inside Connect, so its final state can be asserted immediately.
SocketException ex = Assert.Throws<SocketException>(() => client.Connect(server.LocalEndPoint));
Assert.True(ex.SocketErrorCode is SocketError.WouldBlock or SocketError.InProgress,
$"Unexpected SocketError: {ex.SocketErrorCode}");

recorder.VerifyActivityRecorded(1);
Activity activity = recorder.LastFinishedActivity;
VerifyTcpConnectActivity(activity, (IPEndPoint)server.LocalEndPoint, ipv6: false);
Assert.Equal(ActivityStatusCode.Unset, activity.Status);
Assert.Null(activity.GetTagItem("error.type"));
}).DisposeAsync();
}
Comment on lines +180 to +182
SocketException ex = Assert.Throws<SocketException>(() => client.Connect(server.LocalEndPoint));
Assert.True(ex.SocketErrorCode is SocketError.WouldBlock or SocketError.InProgress,
$"Unexpected SocketError: {ex.SocketErrorCode}");
Comment thread src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketsTelemetry.cs Outdated
Copilot AI review requested due to automatic review settings June 15, 2026 18:17

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Comment on lines +161 to +189
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
public async Task Connect_NonBlockingPending_ActivityNotMarkedAsError()
{
await RemoteExecutor.Invoke(static () =>
{
using Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
server.BindToAnonymousPort(IPAddress.Loopback);
server.Listen();

using Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
{
Blocking = false
};

using ActivityRecorder recorder = new ActivityRecorder(ActivitySourceName, ActivityName);

// A non-blocking connect reports WouldBlock (Windows) or InProgress (Unix) by design while
// the attempt continues in the background. The "socket connect" activity is started and
// stopped synchronously inside Connect, so its final state can be asserted immediately.
SocketException ex = Assert.Throws<SocketException>(() => client.Connect(server.LocalEndPoint));
Assert.True(ex.SocketErrorCode is SocketError.WouldBlock or SocketError.InProgress,
$"Unexpected SocketError: {ex.SocketErrorCode}");

recorder.VerifyActivityRecorded(1);
Activity activity = recorder.LastFinishedActivity;
VerifyTcpConnectActivity(activity, (IPEndPoint)server.LocalEndPoint, ipv6: false);
Assert.Equal(ActivityStatusCode.Unset, activity.Status);
Assert.Null(activity.GetTagItem("error.type"));
}).DisposeAsync();
@liveans liveans self-assigned this Jun 17, 2026
@liveans

liveans commented Jun 18, 2026

Copy link
Copy Markdown
Member

Thanks for your contribution @tulior! I'm inclined to merge this as-is, but CLA (Contributor License Agreement see: #129259 (comment)) is required, can you take a look on it?

Comment thread src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketsTelemetry.cs Outdated
@tulior

tulior commented Jun 18, 2026

Copy link
Copy Markdown
Author

Thanks for your contribution @tulior! I'm inclined to merge this as-is, but CLA (Contributor License Agreement see: #129259 (comment)) is required, can you take a look on it?

Absolutely. Happy to be a part of it.

@dotnet-policy-service agree

@tulior

tulior commented Jun 18, 2026

Copy link
Copy Markdown
Author

@dotnet-policy-service agree

@liveans

liveans commented Jun 22, 2026

Copy link
Copy Markdown
Member

/azp run runtime-libraries-coreclr outerloop

@dotnet dotnet deleted a comment from azure-pipelines Bot Jun 22, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

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

Labels

area-System.Net.Sockets community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Experimental.System.Net.Sockets: "socket connect" marked Error for a non-blocking Connect that succeeds

4 participants