Skip to content

Fix #3736 | Propagate Errors from ExecuteScalar#3912

Merged
samsharma2700 merged 6 commits intomainfrom
dev/samsharma2700/issue3736fix
Feb 3, 2026
Merged

Fix #3736 | Propagate Errors from ExecuteScalar#3912
samsharma2700 merged 6 commits intomainfrom
dev/samsharma2700/issue3736fix

Conversation

@samsharma2700
Copy link
Copy Markdown
Contributor

Description

Fix ExecuteScalar to propagate errors when server sends data followed by error token

Fixes #3736

This PR fixes an issue where ExecuteScalar() swallows server errors that occur after the first result row is returned. When SQL Server sends data followed by an error token (e.g., a conversion error during WHERE clause evaluation), the error was being silently consumed during reader.Close() instead of being thrown to the caller.

Problem

When executing a query like SELECT Id FROM tab1 WHERE Val = 12345 against a table containing values that can't be converted (e.g., '42-43'), SQL Server:

  • Returns matching rows (where conversion succeeds)
  • Sends an error token for rows where conversion fails

The original code only read the first result and immediately closed the reader. The Close() method consumed the remaining TDS stream (including the error token) but the error was never propagated to the caller.

This caused serious issues in transactional scenarios:

  • The transaction would be silently "zombied" by the server
  • Subsequent commands appeared to succeed but ran outside the transaction
  • Data integrity issues from partial commits

Solution

In CompleteExecuteScalar(), drain all remaining result sets by calling NextResult() until it returns false before closing the reader. This ensures any pending error tokens are processed and thrown as exceptions.

Changes

SqlCommand.Scalar.cs: Added result draining loop in CompleteExecuteScalar() for the sync path
SqlCommandExecuteScalarTest.cs: Added 4 regression tests

@samsharma2700 samsharma2700 requested a review from a team as a code owner January 23, 2026 21:10
Copilot AI review requested due to automatic review settings January 23, 2026 21:10
@github-project-automation github-project-automation bot moved this to To triage in SqlClient Board Jan 23, 2026
Copy link
Copy Markdown
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 addresses GitHub issue #3736, which reports that ExecuteScalar() swallows server errors that occur after the first result row is returned. This causes transaction "zombification" where the transaction appears to succeed but is actually rolled back by the server, leading to data integrity issues.

Changes:

  • Adds result draining logic to CompleteExecuteScalar() in the synchronous path to ensure error tokens are processed before returning
  • Adds comprehensive regression tests for the synchronous ExecuteScalar method, including transaction scenarios
  • Updates the project file to include the new test file

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
SqlCommand.Scalar.cs Adds a while loop to drain remaining result sets in the non-batch sync path, ensuring error tokens are processed
SqlCommandExecuteScalarTest.cs Adds 4 regression tests covering conversion errors with and without transactions, plus sanity tests for normal functionality
Microsoft.Data.SqlClient.ManualTesting.Tests.csproj Adds reference to the new test file

@benrr101 benrr101 moved this from To triage to In review in SqlClient Board Jan 26, 2026
@benrr101 benrr101 changed the title Fix github issue 3736 Fix #3736 | Propagate Errors from ExecuteScalar Jan 26, 2026
Copy link
Copy Markdown
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.

I think this change needs some work to ensure we're fixing it in sync and async, as well as ensure we're testing what we actually needs to be tested.

@github-project-automation github-project-automation bot moved this from In review to In progress in SqlClient Board Jan 26, 2026
Copilot AI review requested due to automatic review settings January 28, 2026 22:40
Copy link
Copy Markdown
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

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

Copilot AI review requested due to automatic review settings January 29, 2026 22:58
Copy link
Copy Markdown
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

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.

@priyankatiwari08 priyankatiwari08 self-assigned this Jan 30, 2026
@codecov
Copy link
Copy Markdown

codecov bot commented Jan 30, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 0.00%. Comparing base (88de2b9) to head (47fb5e5).
⚠️ Report is 5 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #3912       +/-   ##
==========================================
- Coverage   74.93%       0   -74.94%     
==========================================
  Files         269       0      -269     
  Lines       43247       0    -43247     
==========================================
- Hits        32407       0    -32407     
+ Misses      10840       0    -10840     
Flag Coverage Δ
addons ?
netcore ?
netfx ?

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.

@cheenamalhotra cheenamalhotra added this to the 7.0.0-preview4 milestone Feb 2, 2026
@cheenamalhotra cheenamalhotra moved this from In progress to In review in SqlClient Board Feb 2, 2026
@samsharma2700 samsharma2700 merged commit a0357b2 into main Feb 3, 2026
272 checks passed
@github-project-automation github-project-automation bot moved this from In review to Done in SqlClient Board Feb 3, 2026
@samsharma2700 samsharma2700 deleted the dev/samsharma2700/issue3736fix branch February 3, 2026 00:18
samsharma2700 added a commit that referenced this pull request Feb 10, 2026
samsharma2700 added a commit that referenced this pull request Feb 26, 2026
Backport of PR #3912 (Fix #3736 | Propagate Errors from ExecuteScalar) to release/6.1 branch.

Changes:
- netcore/netfx SqlCommand.cs: Drain remaining results in CompleteExecuteScalar sync path
- netcore/netfx SqlCommand.cs: Remove _batchRPCMode guard in ExecuteScalarUntilEndAsync
- MultipleResultsTest.cs: Updated to expect SqlException from ExecuteScalar draining
- SqlCommandExecuteScalarTest.cs: New regression tests (sync/async, transactions, multi-result)
- ManualTesting.Tests.csproj: Include new test file

Fixes #3736
paulmedynski added a commit that referenced this pull request Apr 2, 2026
Co-authored-by: paulmedynski <[email protected]>
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants