Skip to content

Conversation

@mdaigle
Copy link
Contributor

@mdaigle mdaigle commented Jan 29, 2026

This pull request improves the handling of enhanced routing support in the SQL client and updates the associated unit tests and server simulation logic. The main focus is ensuring that enhanced routing information is only used when the server acknowledges support for the feature, and making the tests more robust and maintainable.

Enhanced Routing Feature Handling:

  • Updated both netcore and netfx implementations in SqlInternalConnectionTds.cs to ignore enhanced routing info if the server has not acknowledged support for the feature, preventing unintended routing. [1] [2] [3] [4]
  • Modified the OnFeatureExtAck method to properly handle the new enhanced routing feature acknowledgment alongside existing feature checks. [1] [2]

Unit Test Improvements:

  • Refactored ConnectionEnhancedRoutingTests.cs to use a new TestRoutingServers helper class, simplifying test setup and teardown, reducing code duplication, and covering both sync and async connection scenarios with [Theory].

Simulated Server Logic:

  • Enhanced the simulated RoutingTdsServer to send the appropriate feature extension acknowledgment token for enhanced routing, depending on the configured behavior, making tests more accurate and reflective of real server behavior.

Copilot AI review requested due to automatic review settings January 29, 2026 23:30
Copy link
Contributor

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 improves handling of enhanced routing support in the SQL client by ensuring that enhanced routing information is only used when the server acknowledges support for the feature. The changes include updates to both netcore and netfx implementations, refactored unit tests, and enhanced simulated server logic.

Changes:

  • Added validation in SqlInternalConnectionTds to ignore enhanced routing info when the server has not acknowledged the feature
  • Updated OnFeatureExtAck to process enhanced routing support acknowledgments even when RoutingInfo is present
  • Refactored ConnectionEnhancedRoutingTests to use a helper class and Theory-based tests for better maintainability
  • Enhanced RoutingTdsServer to send feature extension acknowledgment tokens for enhanced routing

Reviewed changes

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

File Description
src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs Added checks to ignore enhanced routing info without feature ack; updated OnFeatureExtAck to handle enhanced routing support
src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs Same changes as netcore for consistency across platforms
src/Microsoft.Data.SqlClient/tests/UnitTests/SimulatedServerTests/ConnectionEnhancedRoutingTests.cs Refactored tests using TestRoutingServers helper class and Theory attributes for sync/async testing
src/Microsoft.Data.SqlClient/tests/tools/TDS/TDS.Servers/RoutingTdsServer.cs Added logic to send enhanced routing feature acknowledgment token (contains a critical bug)

Comment on lines 202 to 217

// Create the option data
byte[] data = EnhancedRoutingBehavior == FeatureExtensionBehavior.Enabled ? [1] : [0];
TDSFeatureExtAckGenericOption enhancedRoutingSupportOption = new TDSFeatureExtAckGenericOption(TDSFeatureID.EnhancedRoutingSupport, (uint)data.Length, data);

TDSFeatureExtAckToken featureExtAckToken = new TDSFeatureExtAckToken(enhancedRoutingSupportOption);
// Add it before DONE token if possible, but simplest is to add to end and let the sorting logic handle it or just append
// Ideally, find DONE token and insert before it
int doneIndex = targetMessage.FindIndex(t => t is TDSDoneToken);
if (doneIndex >= 0)
{
targetMessage.Insert(doneIndex, featureExtAckToken);
}
else
{
targetMessage.Add(featureExtAckToken);
Copy link

Copilot AI Jan 29, 2026

Choose a reason for hiding this comment

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

The enhanced routing feature acknowledgment token is being added twice - once by the base class GenericTdsServer.OnAuthenticationCompleted (lines 692-717 in GenericTdsServer.cs) and again by this override in RoutingTdsServer. This creates a duplicate TDSFeatureExtAckToken in the response message.

The code should follow the same pattern as the base class: check if a TDSFeatureExtAckToken already exists in targetMessage, and if so, add the option to it instead of creating a new token. Alternatively, since the base class already handles this feature acknowledgment, this code in the override may not be necessary at all.

Suggested change
// Create the option data
byte[] data = EnhancedRoutingBehavior == FeatureExtensionBehavior.Enabled ? [1] : [0];
TDSFeatureExtAckGenericOption enhancedRoutingSupportOption = new TDSFeatureExtAckGenericOption(TDSFeatureID.EnhancedRoutingSupport, (uint)data.Length, data);
TDSFeatureExtAckToken featureExtAckToken = new TDSFeatureExtAckToken(enhancedRoutingSupportOption);
// Add it before DONE token if possible, but simplest is to add to end and let the sorting logic handle it or just append
// Ideally, find DONE token and insert before it
int doneIndex = targetMessage.FindIndex(t => t is TDSDoneToken);
if (doneIndex >= 0)
{
targetMessage.Insert(doneIndex, featureExtAckToken);
}
else
{
targetMessage.Add(featureExtAckToken);
// Create the option data
byte[] data = EnhancedRoutingBehavior == FeatureExtensionBehavior.Enabled ? [1] : [0];
TDSFeatureExtAckGenericOption enhancedRoutingSupportOption = new TDSFeatureExtAckGenericOption(TDSFeatureID.EnhancedRoutingSupport, (uint)data.Length, data);
// Follow the base class pattern: reuse an existing FeatureExtAck token if present
TDSFeatureExtAckToken featureExtAckToken = targetMessage.OfType<TDSFeatureExtAckToken>().FirstOrDefault();
if (featureExtAckToken != null)
{
// Add the enhanced routing option to the existing FeatureExtAck token
featureExtAckToken.Options.Add(enhancedRoutingSupportOption);
}
else
{
// No existing FeatureExtAck token; create and insert a new one
featureExtAckToken = new TDSFeatureExtAckToken(enhancedRoutingSupportOption);
// Add it before DONE token if possible; otherwise append to the end
int doneIndex = targetMessage.FindIndex(t => t is TDSDoneToken);
if (doneIndex >= 0)
{
targetMessage.Insert(doneIndex, featureExtAckToken);
}
else
{
targetMessage.Add(featureExtAckToken);
}

Copilot uses AI. Check for mistakes.
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.

2 participants