Skip to content

Align SqlException Numbers across platforms #3461

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

David-Engel
Copy link
Contributor

Description

Attempt to better align SqlException Numbers across platforms. Wondering if it might be this simple.

Issues

#1773

Copy link
Contributor

@paulmedynski paulmedynski left a comment

Choose a reason for hiding this comment

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

Do we have existing tests we could update? This is pretty critical functionality.

SqlClientEventSource.Log.TryAdvancedTraceEvent(
"{0}.{1}{2}Last Socket Exception: {3}",
nameof(SniTcpHandle), nameof(Connect), EventType.ERR, lastSocketException);
throw lastSocketException;
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd love to see some tests for these changes.

paulmedynski
paulmedynski previously approved these changes Jul 7, 2025
if (sniException is SocketException socketException)
{
// SocketErrorCode values are cross-plat consistent in .NET (matching native Windows error codes)
nativeError = (uint)socketException.SocketErrorCode;
Copy link
Member

@cheenamalhotra cheenamalhotra Jul 7, 2025

Choose a reason for hiding this comment

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

This is a little concerning, as it will translate SocketError -1 to a positive long (4294967295) and will likely throw an exception here, as there is no unchecked block.

Copy link
Contributor

Choose a reason for hiding this comment

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

The same negative int -> uint cast is happening below with Win32Exception too. But in my test program, there is no unchecked exception thrown. The int -1 value happily becomes uint 4294967295. There is no loss of data since both types are 32-bit. I can't tell from this code whether or not the caller expects a large unsigned value for nativeError - none of these public fields are documented. It looks like nativeError ends up in SNIErrorDetails.nativeError, which is also a uint, so I guess that's fine.

Fix logging bug in SqlClientEventSource.
if (!isConnected)
{
// Retrieve the socket error code
int socketErrorCode = (int)socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Error);
Copy link
Contributor

Choose a reason for hiding this comment

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

Weird that GetSocketOption() returns object? , but then the docs don't describe when it could be null.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants