Skip to content

Reset the per-packet PLP byte count for a packet with ID 0 - #4973

Open
MaceWindu wants to merge 2 commits into
dotnet:mainfrom
MaceWindu:dev/macewindu/plp-packet-id-zero
Open

MaceWindu wants to merge 2 commits into
dotnet:mainfrom
MaceWindu:dev/macewindu/plp-packet-id-zero

Conversation

@MaceWindu

Copy link
Copy Markdown
Contributor

Description

Fixes the packet-ID-0 defect in TdsParser.TryReadPlpUnicodeChars reported in #4925.

With packet multiplexing on (both UseCompatibilityAsyncBehaviour and UseCompatibilityProcessSni set to false), an async read that pends inside an nvarchar(max) value records, per snapshot packet, how many bytes of the value it copied. A later continue resumes at the offset those counts add up to.

When a replay restarts the value from its beginning (startOffsetByteCount == 0), IncrementSnapshotDataSize has to reset the count of each packet it sees for the first time, instead of adding to the count left by an earlier replay. It detects a new packet by comparing the packet's header ID with the last ID it saw. That "last seen" ID started at 0, but the packet ID is one byte and wraps, so every 256th packet has ID 0. A chunk that completes in such a packet was counted as already seen, and kept the count from the previous replay. The continue then resumed at too large an offset: the value came back corrupted (in the test, with a run of NUL characters), longer than stored, or the read threw ArgumentException from Buffer.BlockCopy or spun forever.

The fix starts the "last seen" ID at -1, which no packet has. Every other assignment comes from a real packet, as noted on the issue.

Suggested release note: Fixed corrupted nvarchar(max) values, ArgumentException or a hang in async reads with packet multiplexing enabled, when a PLP chunk ended in a TDS packet whose header packet ID wrapped to 0.

Issues

Fixes #4925

Testing

New UnitTests in SimulatedServerTests/PlpPacketIdZeroTests.cs, using the simulated TDS server and no SQL Server. The server uses 512-byte packets and returns one row of two nvarchar(max) columns. It sends the packets of the second value one at a time with a pause between them, so the async read pends inside the value. The layout is chosen so the read takes exactly the defective path:

  • The value starts in packet 255, and its first PLP chunk ends inside packet 256, which has ID 0.
  • The second chunk ends exactly at the end of packet 257, the first point where the read can capture a continue point. So the value is replayed from its start twice before that, and the first chunk is counted on packet 256 both times.
  • Every later chunk fills exactly one packet, and no length field spans two packets.
Test Before the fix After
ReadAsync_FirstChunkEndsInPacketWithIdZero_ReturnsValue (value starting at 16, 200, 480) fails: value contains a run of NUL characters passes
Read_FirstChunkEndsInPacketWithIdZero_ReturnsValue (same offsets) passes passes

The sync tests never replay a value, so they guard the response layout rather than the defect.

The first commit adds the tests and the second adds the fix. Local results:

  • On the first commit (unmodified main), the 3 async cases fail in 3 of 3 runs.
  • As a control, the same layout moved to packet 100 passes on main, so the failure is caused by packet ID 0.
  • With the fix, all 6 pass on net10.0 (3 runs) and on net462.
  • The full UnitTests suite on net10.0 passes (2045 passed, 9 skipped).

The layout keeps every length field inside one packet on purpose. While building this test we found two separate defects in the same continue logic. Both are unrelated to packet IDs and show up when a PLP length field spans two packets, or when a continue resumes several packets after its continue point. We will report those separately with their own tests.

Guidelines

Please review the contribution guidelines before submitting a pull request:

The simulated server sends one row with 512-byte packets and paces the
packets of an nvarchar(max) value. The value's first PLP chunk ends in
packet 256, which has header ID 0, and the second chunk ends exactly at
the end of packet 257. So the async read replays the value from its
start twice before it can capture a continue point.

With packet multiplexing on, main counts the first chunk on packet 256
in both replays. The continue then resumes too far into the value, and
the value comes back with a run of NUL characters. Moving the same
layout to packet 100 passes. The sync read passes and guards the
response layout.
When a replay restarts a PLP value from its beginning,
TryReadPlpUnicodeChars resets the byte count of each snapshot packet the
first time it sees it. It detects a new packet by comparing the packet's
header ID with the last ID it saw, and that "last seen" ID started at 0.
So a packet with header ID 0 counted as already seen. It kept the count
from the earlier replay, and the next continue resumed at too large an
offset.

Start the "last seen" ID at -1, which no packet has.

Fixes dotnet#4925
Copilot AI balanced review requested due to automatic review settings November 3, 2026 15:40
@github-project-automation github-project-automation Bot moved this to To triage in SqlClient Board Nov 3, 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@MaceWindu
MaceWindu marked this pull request as ready for review November 3, 2026 15:43
@MaceWindu
MaceWindu requested a review from a team as a code owner November 3, 2026 15:43
@MaceWindu

Copy link
Copy Markdown
Contributor Author

Note on the test layout: SQL Server (checked against SQL Server 2016) aligns PLP chunks to packets. The first chunk fills the rest of the current packet, and each later chunk fills one packet. So SQL Server would not send the chunk that ends inside packet 256 that this test uses. The layout is valid TDS: MS-TDS allows any number of chunks of any size (*PLP_CHUNK, 2.2.5.2.3). It was chosen because it reaches the defective path deterministically without running into the two separate continue-logic defects mentioned in the description. The defect itself was reproduced against a real SQL Server in #4925, through a proxy that delivered the response in small pieces. Renumbering the packet IDs there so that none was 0 removed all symptoms.

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

2 participants