avoid stack trace cost via exception when#4099
Conversation
There was a problem hiding this comment.
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 withcatch (...) when (ADP.IsCatchableExceptionType(...))(orIsCatchableOrSecurityExceptionType) 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
SqlQueryMetadataCachehandling 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>
|
/azp run |
|
Azure Pipelines successfully started running 2 pipeline(s). |
|
@SimonCropp - Some conflicts to resolve here. |
…ost-via-exception-when
|
/azp run |
|
Azure Pipelines successfully started running 2 pipeline(s). |
Codecov Report❌ Patch coverage is 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
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:
|
|
Hi @SimonCropp - Are you able to make the changes I suggested? |
…stack-trace-cost-via-exception-when
|
/azp run |
|
Azure Pipelines successfully started running 2 pipeline(s). |
There was a problem hiding this comment.
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 thewhen (...)filter. Please move the comment inside the catch body (or above the catch) and keepcatch (Exception e) when (...)together to improve clarity/consistency.
catch (Exception e)
// UNDONE - should not be catching all exceptions!!!
when (ADP.IsCatchableExceptionType(e))
…g, clean up formatting
|
/azp run |
|
Azure Pipelines successfully started running 2 pipeline(s). |
No description provided.