Skip to content

avoid stack trace cost via exception when#4099

Open
SimonCropp wants to merge 6 commits into
dotnet:mainfrom
SimonCropp:avoid-stack-trace-cost-via-exception-when
Open

avoid stack trace cost via exception when#4099
SimonCropp wants to merge 6 commits into
dotnet:mainfrom
SimonCropp:avoid-stack-trace-cost-via-exception-when

Conversation

@SimonCropp
Copy link
Copy Markdown
Contributor

No description provided.

@SimonCropp SimonCropp requested a review from a team as a code owner March 28, 2026 12:22
Copilot AI review requested due to automatic review settings March 28, 2026 12:23
@github-project-automation github-project-automation Bot moved this to To triage in SqlClient Board Mar 28, 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

Refactors broad catch (Exception) blocks across core SqlClient components to use exception filters (catch (...) when (...)) so non-catchable exceptions bypass the handler, reducing overhead and avoiding unnecessary catch/rethrow paths.

Changes:

  • Replaces catch (Exception) { if (!IsCatchable) throw; ... } patterns with catch (...) when (ADP.IsCatchableExceptionType(...)) (or IsCatchableOrSecurityExceptionType) across the driver.
  • Simplifies selected exception-handling branches (e.g., transient retry, transaction cleanup) while keeping behavior consistent for catchable vs. non-catchable exceptions.
  • Splits SqlQueryMetadataCache handling into filtered “expected exception” vs. “unexpected exception” catch blocks.

Reviewed changes

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

Show a summary per file
File Description
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs Uses exception filters for Deactivate/timeout attention failure paths.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParser.cs Applies exception filters to attention/login/transaction-manager error handling.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlQueryMetadataCache.cs Uses filtered catch for expected key-decrypt failures; separates unexpected exceptions.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlParameter.cs Filters catchable exceptions before wrapping conversion failures.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlMetaDataFactory.cs Filters catchable exceptions before wrapping query execution failure.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlInternalTransaction.cs Filters catchable exceptions around zombie/commit/rollback/save error paths.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDependencyUtils.cs Filters catchable exceptions while invalidating dependencies (user callbacks).
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDependencyListener.cs Filters catchable exceptions in listener setup/restart/teardown resiliency paths.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDependency.cs Filters catchable exceptions in Start/Stop flows (including duplicate-start cleanup).
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDataReader.cs Filters catchable exceptions for sync buffer ops and async Task.FromException paths.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlConnection.cs Filters catchable/security exceptions in InfoMessage event invocation.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommand.cs Filters catchable/security exceptions in StatementCompleted event invocation.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommand.Xml.cs Filters catchable exceptions for XML reader completion and async state cleanup.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommand.Reader.cs Filters catchable exceptions during reader finish/metadata initialization cleanup.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommand.NonQuery.cs Filters catchable/security exceptions for async state object cleanup in BeginExecuteNonQuery.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlBulkCopy.cs Filters catchable exceptions during dispose cleanup and value conversion.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Server/ValueUtilsSmi.cs Filters catchable exceptions around reader string/chars transfer fallback.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ManagedSni/SniTcpHandle.netcore.cs Refactors connection retry exception handling to use filters for retry eligibility.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ConnectionPool/WaitHandleDbConnectionPool.cs Filters catchable exceptions in pooled connection creation and background create requests.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Connection/SqlConnectionInternal.cs Uses exception filters for transient retry and connection deactivate/login failure handling.

…gedSni/SniTcpHandle.netcore.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 28, 2026 12:35
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 20 out of 20 changed files in this pull request and generated no new comments.

@mdaigle mdaigle moved this from To triage to In review in SqlClient Board Apr 1, 2026
@mdaigle mdaigle added this to the 7.1.0-preview1 milestone Apr 1, 2026
@paulmedynski paulmedynski self-assigned this Apr 21, 2026
@paulmedynski
Copy link
Copy Markdown
Contributor

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 2 pipeline(s).

Comment thread src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommand.NonQuery.cs Outdated
@github-project-automation github-project-automation Bot moved this from In review to In progress in SqlClient Board Apr 22, 2026
@paulmedynski
Copy link
Copy Markdown
Contributor

@SimonCropp - Some conflicts to resolve here.

@cheenamalhotra cheenamalhotra moved this from In progress to Waiting for customer in SqlClient Board Apr 24, 2026
@cheenamalhotra
Copy link
Copy Markdown
Member

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 2 pipeline(s).

@cheenamalhotra cheenamalhotra added Author attention needed PRs that require author to respond or make updates to PR. labels May 7, 2026
@cheenamalhotra cheenamalhotra moved this from Waiting for customer to In progress in SqlClient Board May 8, 2026
@cheenamalhotra cheenamalhotra self-assigned this May 8, 2026
@paulmedynski paulmedynski removed the Author attention needed PRs that require author to respond or make updates to PR. label May 11, 2026
@paulmedynski paulmedynski added the Code Health 💊 Issues/PRs that are targeted to source code quality improvements. label May 11, 2026
@paulmedynski paulmedynski moved this from In progress to In review in SqlClient Board May 11, 2026
@cheenamalhotra cheenamalhotra self-requested a review May 11, 2026 17:20
@codecov
Copy link
Copy Markdown

codecov Bot commented May 11, 2026

Codecov Report

❌ Patch coverage is 33.33333% with 102 lines in your changes missing coverage. Please review.
✅ Project coverage is 65.87%. Comparing base (b700269) to head (e522871).
⚠️ Report is 18 commits behind head on main.

Files with missing lines Patch % Lines
.../Data/SqlClient/ManagedSni/SniTcpHandle.netcore.cs 27.90% 31 Missing ⚠️
.../Microsoft/Data/SqlClient/SqlDependencyListener.cs 0.00% 16 Missing ⚠️
...Microsoft/Data/SqlClient/SqlInternalTransaction.cs 0.00% 10 Missing ⚠️
.../Microsoft/Data/SqlClient/SqlQueryMetadataCache.cs 0.00% 9 Missing ⚠️
.../src/Microsoft/Data/SqlClient/SqlCommand.Reader.cs 38.46% 8 Missing ⚠️
...ient/src/Microsoft/Data/SqlClient/SqlDataReader.cs 22.22% 7 Missing ⚠️
...qlClient/src/Microsoft/Data/SqlClient/TdsParser.cs 80.95% 4 Missing ⚠️
...ent/src/Microsoft/Data/SqlClient/SqlCommand.Xml.cs 0.00% 3 Missing ⚠️
...ient/src/Microsoft/Data/SqlClient/SqlDependency.cs 25.00% 3 Missing ⚠️
...src/Microsoft/Data/SqlClient/SqlDependencyUtils.cs 0.00% 3 Missing ⚠️
... and 7 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4099      +/-   ##
==========================================
- Coverage   66.04%   65.87%   -0.18%     
==========================================
  Files         275      279       +4     
  Lines       42976    66029   +23053     
==========================================
+ Hits        28383    43495   +15112     
- Misses      14593    22534    +7941     
Flag Coverage Δ
CI-SqlClient ?
PR-SqlClient-Project 65.87% <33.33%> (?)

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 previously approved these changes May 12, 2026
@paulmedynski paulmedynski added the Author attention needed PRs that require author to respond or make updates to PR. label May 12, 2026
@paulmedynski
Copy link
Copy Markdown
Contributor

Hi @SimonCropp - Are you able to make the changes I suggested?

Copilot AI review requested due to automatic review settings May 20, 2026 18:36
@paulmedynski
Copy link
Copy Markdown
Contributor

/azp run

@paulmedynski paulmedynski removed the Author attention needed PRs that require author to respond or make updates to PR. label May 20, 2026
@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 2 pipeline(s).

paulmedynski
paulmedynski previously approved these changes May 20, 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

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

Comments suppressed due to low confidence (1)

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParser.cs:9528

  • Same readability issue here: the comment sits between the catch (Exception e) header and the when (...) filter. Please move the comment inside the catch body (or above the catch) and keep catch (Exception e) when (...) together to improve clarity/consistency.
            catch (Exception e)
                // UNDONE - should not be catching all exceptions!!!
                when (ADP.IsCatchableExceptionType(e))

Comment thread src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParser.cs Outdated
Comment thread src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDependency.cs Outdated
@paulmedynski
Copy link
Copy Markdown
Contributor

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 2 pipeline(s).

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

Labels

Code Health 💊 Issues/PRs that are targeted to source code quality improvements.

Projects

Status: In review

Development

Successfully merging this pull request may close these issues.

6 participants