Fix IsDBNull + stream read bug that skips column data#4082
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a SqlDataReader sequential-access streaming regression where calling IsDBNull/IsDBNullAsync before GetFieldValue<T> for streaming types could consume the column payload and cause empty streams / asserts.
Changes:
- Updates
SqlDataReader.TryReadColumnInternalto avoid eagerly consuming column data whenforStreaming == trueand the column header was already read. - Adds new manual test coverage for
IsDBNull/IsDBNullAsync+ streamingGetFieldValue<T>combinations underCommandBehavior.SequentialAccess. - Adds test-utility configurability (env var override) and hardens an Always Encrypted CSP test’s data discovery on non-Windows.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDataReader.cs | Adjusts sequential-access read logic to preserve wire data for streaming readers after IsDBNull*. |
| src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/DataReaderTest/DataReaderStreamsTest.cs | Adds manual regression tests for streaming reads after IsDBNull/IsDBNullAsync in sequential access. |
| src/Microsoft.Data.SqlClient/tests/tools/Microsoft.Data.SqlClient.TestUtilities/Config.cs | Adds env-var override support for test config values. |
| src/Microsoft.Data.SqlClient/tests/ManualTests/AlwaysEncrypted/CspProviderExt.cs | Prevents registry access during test discovery on non-Windows. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4082 +/- ##
==========================================
- Coverage 65.99% 64.26% -1.73%
==========================================
Files 276 271 -5
Lines 42951 65746 +22795
==========================================
+ Hits 28344 42252 +13908
- Misses 14607 23494 +8887
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
paulmedynski
commented
Mar 25, 2026
cheenamalhotra
previously approved these changes
Mar 25, 2026
- Added a unit test that exposes the IsDBNullAsync() and GetFieldValueAsync() interaction problem.
…mlReader]>() that would skip column data.
- Fixed a long-standing issue with test discovery failures in VS Code with the TestCommon project.
c2a9aee to
71b9bfa
Compare
Contributor
Author
|
/azp run |
|
Azure Pipelines successfully started running 2 pipeline(s). |
cheenamalhotra
previously approved these changes
Mar 28, 2026
priyankatiwari08
approved these changes
Apr 23, 2026
apoorvdeshmukh
approved these changes
Apr 23, 2026
paulmedynski
added a commit
that referenced
this pull request
May 7, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix: IsDBNull/IsDBNullAsync breaks subsequent streaming reads in SequentialAccess mode
Fixes #3057
Problem
When using
CommandBehavior.SequentialAccess, callingIsDBNull()orIsDBNullAsync()on a column before reading it via a streaming type (GetFieldValue<Stream>,GetFieldValue<TextReader>,GetFieldValue<XmlReader>, or their async variants) causes:DebugAssertException— "Already read past column"Root Cause
TryReadColumnInternalinSqlDataReader.cshas two code paths for sequential access depending on whether the column header has already been read:Path A — header already read (
i < _nextColumnHeaderToRead):Path B — header not yet read (
_nextColumnHeaderToRead == i):Path B correctly checks both
!readHeaderOnlyand!forStreamingbefore consuming data. Path A only checks!readHeaderOnly, ignoringforStreaming.When
IsDBNull/IsDBNullAsyncis called first, it reads the column header (advancing_nextColumnHeaderToRead) but leaves data on the wire. The subsequent streamingGetFieldValue<Stream>call enters Path A (since the header is already read) and eagerly consumes all data viaTryReadColumnData(), leaving nothing for theSqlSequentialStreamto read.Fix
Add
&& (!forStreaming)to Path A's condition to match Path B's behavior:This ensures that when a streaming type is requested after
IsDBNullhas already read the column header, the column data stays on the wire for the sequential stream to consume incrementally.Compatibility
forStreaming == true, which is limited toGetFieldValue<T>/GetFieldValueAsync<T>withTbeingStream,TextReader, orXmlReader.forStreaming: false(the default) — zero behavior change.Tests
Added 7 new test methods (28 test cases) covering all combinations of:
IsDBNullAsyncGetFieldValueAsync<TextReader>IsDBNullAsyncGetFieldValueAsync<XmlReader>IsDBNull(sync)GetFieldValue<Stream>(sync)IsDBNull(sync)GetFieldValue<TextReader>(sync)IsDBNull(sync)GetFieldValue<XmlReader>(sync)IsDBNull(sync)GetFieldValueAsync<Stream>(async)IsDBNullAsyncGetFieldValue<Stream>(sync)Each test method has 4 inline data cases: null/non-null × checkNull true/false.
Verification:
checkNull: True(bug path)checkNull: False(control)Checklist