Skip to content

Fix SqlCommand.Cancell null ref exception - #4372

Merged
paulmedynski merged 2 commits into
mainfrom
dev/mdaigle/comand-null-ref
Jun 18, 2026
Merged

Fix SqlCommand.Cancell null ref exception#4372
paulmedynski merged 2 commits into
mainfrom
dev/mdaigle/comand-null-ref

Conversation

@mdaigle

@mdaigle mdaigle commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

No description provided.

Copilot AI review requested due to automatic review settings June 17, 2026 21:26
@mdaigle
mdaigle requested a review from a team as a code owner June 17, 2026 21:26
@github-project-automation github-project-automation Bot moved this to To triage in SqlClient Board Jun 17, 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.

Pull request overview

This pull request fixes a NullReferenceException in SqlCommand.Cancel() when the command has no associated connection (Connection == null), aligning the method with expected no-op behavior in that scenario.

Changes:

  • Prevent SqlCommand.Cancel() tracing from dereferencing _activeConnection when it is null.
  • Add a functional test asserting Cancel() does not throw when Connection is null.

Reviewed changes

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

File Description
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommand.cs Makes Cancel() correlation tracing null-safe for ClientConnectionId when _activeConnection is not set.
src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlCommandTest.cs Adds regression coverage ensuring SqlCommand.Cancel() is a no-op when Connection is null.

@mdaigle mdaigle added the Hotfix 7.0.3 PRs targeting main that should be backported to release/7.0 branch for next release. label Jun 17, 2026
@mdaigle mdaigle added this to the 7.1.0-preview2 milestone Jun 17, 2026
@codecov

codecov Bot commented Jun 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 63.45%. Comparing base (47f4e52) to head (b5d889c).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4372      +/-   ##
==========================================
- Coverage   65.39%   63.45%   -1.95%     
==========================================
  Files         285      280       -5     
  Lines       43331    66193   +22862     
==========================================
+ Hits        28337    42000   +13663     
- Misses      14994    24193    +9199     
Flag Coverage Δ
CI-SqlClient ?
PR-SqlClient-Project 63.45% <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.

@paulmedynski paulmedynski moved this from To triage to In review in SqlClient Board Jun 18, 2026
@paulmedynski
paulmedynski enabled auto-merge (squash) June 18, 2026 09:30
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.

SqlCommand.Cancel throws NullReferenceException when Connection is null

4 participants