Skip to content

Send attention when a streamed TVP fails after the packet number wraps - #4993

Open
priyankatiwari08 wants to merge 1 commit into
mainfrom
dev/priyankatiwari08/issue-4921-tvp-attention-packet-wrap
Open

priyankatiwari08 wants to merge 1 commit into
mainfrom
dev/priyankatiwari08/issue-4921-tvp-attention-packet-wrap

Conversation

@priyankatiwari08

Copy link
Copy Markdown
Contributor

Description

If the IEnumerable<SqlDataRecord> behind a streamed table-valued parameter throws while SqlClient is writing the RPC, TdsParser.FailureCleanup is supposed to send an attention so the server drops the partial request. It decided whether anything had been sent with stateObj._outputPacketNumber != 1. _outputPacketNumber is the one-byte packet number in the TDS header, and it wraps after 255. When the failure happens while packet 257, 513, ... is being filled, the number is 1 again even though 256+ packets have gone out. No attention is sent, and the pooled connection keeps an unfinished RPC that breaks the requests that come after it.

TdsParserStateObject.ExecuteFlush relied on the same _outputPacketNumber == 1 assumption when a command was cancelled while a packet was being filled.

Fix: both checks now use _outputPacketCount, a uint that does not wrap. WritePacket already uses it for its header-only check. The other _outputPacketNumber == 1 uses are debug asserts or reset paths that send an attention unconditionally, so they are unchanged.

There are no public API or behavior changes, apart from the attention now being sent correctly.

Suggested release note: Fixed pooled connections being left with an unfinished RPC when a streamed TVP enumerator throws or the command is cancelled while the 257th (513th, ...) packet is being written. (#4921)

Issues

Fixes #4921

Testing

  • Unit tests (new): UnitTests/SimulatedServerTests/StreamedTvpFailureTests.cs.
    • Sync and async theories make the TVP row source throw after 10, 255, 256, 257 and 512 packets have been sent.
    • They assert that exactly one attention is received, that the original exception is surfaced, and that a follow-up SELECT 1 succeeds.
    • Before the fix, the 256 and 512 cases failed (0 attentions).
  • Manual tests (new): in ManualTests/SQL/ParameterTest/TvpTest.cs, sync throw, async throw and sync cancel while packet 257 is being filled, each followed by a check that the connection is still usable. All 3 fail without the fix.
    • They use Packet Size=512. With 8 KB packets, roughly 2 MB has been sent before the failure, and SQL Server can take longer to acknowledge the attention than the driver's fixed 5-second attention timeout. That is a separate, pre-existing behavior that also happens below the wrap point.
    • A small reflection helper, GetOutputPacketCount, was added to ConnectionHelper.

Commands run (all passed, against local SQL Server 2022):

  • dotnet build build.proj -t:TestSqlClientUnit -p:TestFramework=net10.0 "-p:TestFilters=FullyQualifiedName~StreamedTvpFailureTests": 10/10 passed

  • dotnet build build.proj -t:TestSqlClientUnit -p:TestFramework=net462 "-p:TestFilters=FullyQualifiedName~StreamedTvpFailureTests": 10/10 passed

  • dotnet build build.proj -t:TestSqlClientManual -p:TestFramework=net10.0 "-p:TestFilters=FullyQualifiedName~ManualTesting.Tests.TvpTest.": 5/5 passed

  • dotnet build build.proj -t:TestSqlClientManual -p:TestFramework=net462 "-p:TestFilters=FullyQualifiedName~ManualTesting.Tests.TvpTest.": 5/5 passed

  • Tests added or updated

  • Public API changes documented (none)

  • Verified against customer repro (the manual tests reproduce the issue's scenario)

  • Ensure no breaking changes introduced

Guidelines

Please review the contribution guidelines before submitting a pull request:

#4921)

FailureCleanup and ExecuteFlush used the one-byte TDS packet number (_outputPacketNumber == 1) to decide whether any packets had been sent. The number wraps after 255, so a failure or cancellation while filling packet 257, 513, ... skipped the attention and left the pooled connection with an unfinished RPC. Use the non-wrapping _outputPacketCount instead.

Co-authored-by: Copilot App <[email protected]>
@priyankatiwari08
priyankatiwari08 requested a review from a team as a code owner November 4, 2026 10:32
Copilot AI balanced review requested due to automatic review settings November 4, 2026 10:32
@github-project-automation github-project-automation Bot moved this to To triage in SqlClient Board Nov 4, 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.

🟡 Changes recommended

With MARS enabled, the packet counter reads the wrong session and stays at zero, preventing all three new manual tests from triggering their intended failure or cancellation.

1 open issue
What changed in this PR

Fixes #4921 by using the packet count rather than the wrapping TDS header number to detect partially sent requests. This ensures streamed TVP failures and cancellation trigger the required cleanup, protecting subsequent connection use.

Changes:

  • Updates failure cleanup and cancellation checks to use _outputPacketCount.
  • Adds synchronous and asynchronous failure regressions around packet-number wrap boundaries.
  • Adds manual cancellation coverage and a packet-count inspection helper.
File Description
src/​Microsoft.Data.SqlClient/​tests/​UnitTests/​SimulatedServerTests/​StreamedTvpFailureTests.cs Tests attention delivery, exception propagation, and connection reuse.
src/​Microsoft.Data.SqlClient/​tests/​ManualTests/​SQL/​ParameterTest/​TvpTest.cs Adds real-server regressions for wrapped-packet failures and cancellation.
src/​Microsoft.Data.SqlClient/​tests/​ManualTests/​SQL/​Common/​SystemDataInternals/​ConnectionHelper.cs Exposes the physical session’s packet count for tests.
src/​Microsoft.Data.SqlClient/​src/​Microsoft/​Data/​SqlClient/​Parser/​TdsParserStateObject.cs Uses packet count to decide whether cancellation can remain local.
src/​Microsoft.Data.SqlClient/​src/​Microsoft/​Data/​SqlClient/​Parser/​TdsParser.cs Uses packet count to trigger attention after a partially sent request fails.

Review effort: Balanced

{
SqlConnectionStringBuilder builder = new(DataTestUtility.TCPConnectionString)
{
PacketSize = 512
@codecov

codecov Bot commented Nov 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 67.11%. Comparing base (f8ededd) to head (c38139e).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4993      +/-   ##
==========================================
- Coverage   68.44%   67.11%   -1.34%     
==========================================
  Files         313      307       -6     
  Lines       45780    69002   +23222     
==========================================
+ Hits        31333    46308   +14975     
- Misses      14447    22694    +8247     
Flag Coverage Δ
CI-SqlClient ?
PR-SqlClient-Project 67.11% <100.00%> (?)

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

☔ View full report in Codecov by Harness.
📢 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.

@benrr101 benrr101 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.

Looks pretty reasonable to me

This branch has not been deployed

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

Labels

None yet

Projects

Status: To triage

Development

Successfully merging this pull request may close these issues.

Failed streamed TVP request poison other requests in the application

4 participants