Skip to content

[6.1] Align SqlException Numbers across platforms #3475

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

Merged
merged 4 commits into from
Jul 16, 2025

Conversation

paulmedynski
Copy link
Contributor

Description

This is a cherry-pick of 2 PRs and a touch-up commit:

Issues

#1773

edwardneal and others added 3 commits July 15, 2025 06:59
* Converted Threads to long-running Tasks

The key advantage is that exceptions propagate properly.
If a thread throws an exception (as a result of a failed test assertion, or otherwise) then the test host crashes and must be restarted.

* Corrected the instantiation of the cancellation task - missing state parameter.

* Changes to TestSqlCommandCancel, eliminating timing-specific cancellation behaviour testing.
This should also allow the test to run on both netcore and netfx.

* Responding to code review.
* Removed two unnecessary iterations from DatabaseHelper.
* Added explanatory comments to ApiShould.
* Switched to using Task.WaitAll rather than waiting for each Task in sequence.

* Improve cancellation detection

Cancellation can trigger one of several different errors, resulting in a flakier test.
Also ensure that the query always takes more than 150ms, ensuring that a quick query execution doesn't cause the test to fail.
Finally, make sure that we try to read everything from the SqlDataReader.

* Correcting previous merge
* - Align SqlException Numbers across platforms
- Better capture error scenarios in TCP managed SNI.
- Fix logging bug in SqlClientEventSource.
- Change nativeError from uint to int

* - Removed duplicate SniErrorDetails object and aligned field names.

* - Added localized string for the new connection timed out exception.

* - Fixed tests sensitive to OS newlines.

---------

Co-authored-by: Paul Medynski <31868385+paulmedynski@users.noreply.github.com>
- Manually fixing some error reporting that wasn't cherr-picked because it was part of larger changes.
@paulmedynski paulmedynski added this to the 6.1.0 milestone Jul 15, 2025
@Copilot Copilot AI review requested due to automatic review settings July 15, 2025 10:25
@paulmedynski paulmedynski requested a review from a team as a code owner July 15, 2025 10:25
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR aligns SQL error and SNI error handling across platforms, replaces direct thread usage in manual tests with long-running tasks, and updates resource strings and error code types.

  • Switch manual test threading from Thread to Task.Factory.StartNew for long-running operations.
  • Introduce a reusable SniErrorDetails struct and unify native error code types from uint to int.
  • Add a new resource string for connection timeouts and update event source method naming.

Reviewed Changes

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

Show a summary per file
File Description
src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/RandomStressTest/RandomStressTest.cs Replace Thread with Task for stress test threads
src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ParameterTest/ParametersTest.cs Replace thread list with Task[] and use Task.Factory.StartNew
src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/MirroringTest/ConnectionOnMirroringTest.cs Replace Thread with Task for mirroring connection test
src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ConnectivityTests/ConnectivityTest.cs Replace _thread with _task and start tasks in connection worker
src/Microsoft.Data.SqlClient/tests/ManualTests/AlwaysEncrypted/TestFixtures/DatabaseHelper.cs Simplify IEnumerable<object[]> yields by removing redundant arguments
src/Microsoft.Data.SqlClient/tests/ManualTests/AlwaysEncrypted/ApiShould.cs Rewrite cancel tests to use Tasks with ManualResetEventSlim
src/Microsoft.Data.SqlClient/src/Resources/Strings.resx Add new resource <data name="SQL_ConnectTimeout">
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs Introduce new SniErrorDetails struct for managed SNI
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsEnums.cs Change SNI_WAIT_TIMEOUT constant from uint to int
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlError.cs Update SqlError constructors to accept int win32ErrorCode
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientEventSource.cs Rename SNIScopeEnter event to ScopeEnter
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ManagedSni/SniTcpHandle.netcore.cs Improve exception capture and logging in Connect
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ManagedSni/SniNpHandle.netcore.cs Unify ReportErrorAndReleasePacket signature to use int nativeError
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ManagedSni/SniError.netcore.cs Change nativeError field to int and map exception codes properly
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ManagedSni/SniCommon.netcore.cs Update ReportSNIError overloads to use int nativeError
src/Microsoft.Data.SqlClient/src/Interop/Windows/Sni/SniError.cs Change interop nativeError field from uint to int
src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs Update SNI error code handling and constructor call to int nativeError
src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.netcore.cs Remove old SNIErrorDetails struct and switch to new one
src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs Refactor ProcessSNIError to use new struct properties
src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.Unix.cs Implement GetSniErrorDetails to return TdsParserStateObject.SniErrorDetails
Files not reviewed (1)
  • src/Microsoft.Data.SqlClient/src/Resources/Strings.Designer.cs: Language not supported
Comments suppressed due to low confidence (4)

src/Microsoft.Data.SqlClient/tests/ManualTests/AlwaysEncrypted/ApiShould.cs:3405

  • The XML doc <param name="numberOfTimesToCancel"> no longer matches any constructor parameter. Please remove or update this <param> entry to reflect the current signature.
        /// <summary>

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs:63

  • [nitpick] The field name Exception shadows the System.Exception type and can be confusing. Consider renaming it to something like InnerException or ErrorException for clarity.
            public readonly Exception Exception;

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsEnums.cs:607

  • Changing this public constant from uint to int is a breaking API change. Verify that all consumers depending on TdsEnums.SNI_WAIT_TIMEOUT have been updated or consider providing a compatibility shim.
        public const int SNI_WAIT_TIMEOUT = 258;      // The wait operation timed out.

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientEventSource.cs:427

  • The event method SNIScopeEnter was replaced by ScopeEnter, which could affect existing event listeners or ETW consumers. Please confirm this change is reflected in any schema or downstream tooling.
                return ScopeEnter(sb.ToString());

Copy link
Contributor

@benrr101 benrr101 left a comment

Choose a reason for hiding this comment

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

🪨 🎸

Copy link

codecov bot commented Jul 15, 2025

Codecov Report

Attention: Patch coverage is 65.13761% with 38 lines in your changes missing coverage. Please review.

Project coverage is 67.44%. Comparing base (df35633) to head (650c379).
Report is 6 commits behind head on release/6.1.

Files with missing lines Patch % Lines
.../Data/SqlClient/ManagedSni/SniTcpHandle.netcore.cs 25.00% 27 Missing ⚠️
.../netcore/src/Microsoft/Data/SqlClient/TdsParser.cs 77.77% 4 Missing ⚠️
...nt/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs 0.00% 3 Missing ⚠️
...c/Microsoft/Data/SqlClient/TdsParserStateObject.cs 88.88% 2 Missing ⚠️
...soft/Data/SqlClient/ManagedSni/SniError.netcore.cs 85.71% 1 Missing ⚠️
...t/Data/SqlClient/ManagedSni/SniNpHandle.netcore.cs 0.00% 1 Missing ⚠️
Additional details and impacted files
@@               Coverage Diff               @@
##           release/6.1    #3475      +/-   ##
===============================================
+ Coverage        66.91%   67.44%   +0.53%     
===============================================
  Files              280      280              
  Lines            62386    62393       +7     
===============================================
+ Hits             41745    42084     +339     
+ Misses           20641    20309     -332     
Flag Coverage Δ
addons 92.58% <ø> (ø)
netcore 72.62% <64.35%> (+3.65%) ⬆️
netfx 66.13% <52.38%> (-3.12%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@paulmedynski paulmedynski merged commit d535929 into release/6.1 Jul 16, 2025
129 checks passed
@paulmedynski paulmedynski deleted the dev/paul/release/6.1-align-error-codes branch July 16, 2025 10:12
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