Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Promote CA2000 to warning and resolve existing disposal diagnostics

Aperta
#4,937 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
25/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
csharp
Ambito
database, testing

Direzione di ricerca

Start with the root .editorconfig entry for CA2000 and reproduce the reported baseline with dotnet build --no-restore -v:minimal on the specified revision. Work through the listed source locations by ownership category, beginning with the seven straightforward cleanup candidates; use focused regression tests for each behavior sensitive change. Done means the diagnostics are fixed or narrowly justified, ownership lifetimes and async paths remain correct, and the applicable build matrix passes.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Code Health :pill: Up-for-Grabs :raised_hands:

Goal

Promote dotnet_diagnostic.CA2000.severity from suggestion to warning in the root .editorconfig, resolving or narrowly documenting existing diagnostics first.

CA2000 detects disposable objects whose cleanup or ownership transfer is not established by analysis. Normal builds treat warnings as errors, so this promotion will enforce the rule during builds rather than leaving it as an informational IDE diagnostic.

Do not resolve diagnostics by adding using indiscriminately: several resources escape to callers, belong to pools, or remain in use by asynchronous continuations.

Reproduction and scope

  • Baseline inspected: 5d9001ea2423e850d3fc6d07f822e84bd0004352 (Handle authentication configuration failures independently (#4821)), on development branch dev/paul/aot/phase-1.3.
  • Local change: set CA2000 severity to warning.
  • Command: dotnet build --no-restore -v:minimal.
  • Environment: Linux, .NET SDK 10.0.402, default Debug configuration.
  • Result: failed with 32 distinct CA2000 source locations. The build summary reported 65 errors, comprising repeated build-variant diagnostics and the orchestration failure.
  • This is a branch-specific initial inventory, not proof of the complete repository backlog. The build stopped at the driver failure; subsequent projects, other configurations, and platform-specific paths can reveal additional diagnostics. Line numbers below refer to the inspected revision.
  • Analysis was read-only; none of the proposed fixes has been implemented or regression-tested.

All source paths below are relative to src/Microsoft.Data.SqlClient/src/Microsoft/Data/.

Initial triage

1. Straightforward local cleanup: 7 locations

Good candidates for a first behavior-preserving cleanup, subject to focused validation.

File and line Proposed treatment
SqlClient/VirtualSecureModeEnclaveProviderBase.cs:435 Add scoped disposal of the local X509Chain. No chain object escapes certificate verification.
SqlClient/ManagedSni/SniCommon.netcore.cs:138 Add scoped disposal of the local X509Chain after its status has been inspected.
SqlClient/SqlDependencyListener.cs:359 Dispose the temporary SqlCommand with a using declaration. It does not own the connection or transaction.
SqlClient/Server/ValueUtilsSmi.cs:448, 2132, 2521, 3016 Dispose temporary SmiGettersStream/SmiSettersStream wrappers. Their current implementations inherit base stream disposal without custom cleanup. Keep copied streams alive when they are returned through SqlBytes or SqlXml; distinguish the temporary source from its returned copy.
2. Small but not mechanical: 7 locations
File and line Required care
SqlClient/Server/SqlSer.cs:185, 196 BinaryWriter and BinaryReader wrap borrowed streams. Plain using would close the underlying stream. Consider leaveOpen: true and scoped wrapper disposal; verify writer flushing, encoding, and UDT serialization behavior.
SqlClient/SqlMetaDataFactory.cs:765, 768, 771, 775 Tables are loaded and then added to a returned dataset. Dispose on loading/addition failures without prematurely disposing successfully transferred tables.
SqlClient/ManagedSni/SniTcpHandle.netcore.cs:386 Existing finally cleanup handles most paths, but Blocking is set in an object initializer before the local receives the socket. Split allocation from property initialization so a throwing setter cannot bypass local cleanup. Preserve ownership of the successfully returned socket and inspect the success-transfer boundary.
3. Existing cleanup or intentional ownership: 9 locations

These are candidates for narrowly justified suppressions or analyzer-friendly ownership expression, not automatic additional disposal. Confirm each contract before suppressing.

File and line Existing ownership/cleanup
SqlClient/SqlAeadAes256CbcHmac256Algorithm.cs:193, 195 AES providers are deliberately returned to a reuse pool in finally. Disposing them per encryption operation would break reuse. Pool-lifetime cleanup is a separate question.
SqlClient/SqlCommand.Scalar.cs:58 CompleteExecuteScalar already closes the reader in finally.
SqlClient/SqlMetaDataFactory.cs:220 The resulting DataTable is returned to the caller.
SqlClient/SqlConnection.cs:2914 The temporary internal connection already has finally cleanup.
SqlTypes/SqlFileStream.cs:866 Handle ownership passes to FileStream; the catch path disposes an acquired valid handle. A local using would close the successful stream's handle.
SqlClient/SqlInternalTransaction.cs:300 The command runs with returnStream: false; the analyzed path does not create a returned reader.
SqlClient/Connection/SqlConnectionInternal.cs:2957 Inspect the actual transaction request values. The shared request method creates a reader only for GetDTCAddress; ordinary transaction-control requests do not return one.
SqlClient/Parser/TdsParser.cs:9731 The Propagate transaction request does not return a reader, despite the shared method's disposable return type.
4. Ownership-sensitive work: 9 locations

Require exception-path/lifetime analysis and targeted regression coverage.

File and line Work needed
SqlClient/SqlConnection.cs:1388 Establish ownership of the transaction discarded when reconnection forces another begin attempt. Disposal can trigger rollback; preserve the returned transaction and reconnection semantics.
SqlClient/SqlConnectionFactory.cs:423 The non-pooled async-open cancellation source is disposed by a completion continuation. Check failures before that continuation is registered and avoid disposal while scheduled work still uses it.
SqlClient/SqlSequentialStream.cs:134 Normal synchronous/asynchronous completion disposes the linked cancellation source, but synchronous exceptions can bypass that cleanup. Distinguish the newly linked source from the stream-owned disposal source.
SqlClient/SqlCommand.NonQuery.cs:907 Reconnect callbacks already cancel/dispose the timeout source. Verify setup exceptions and timeout/reconnect races before changing ownership.
SqlClient/SqlCommand.Reader.cs:1679 Same reconnect timeout-source ownership concerns as the nonquery path; keep both execution paths consistent.
SqlClient/SqlCommand.Encryption.cs:628 Encryption metadata reader ownership spans synchronous cleanup and asynchronous continuations, plus connection async-count bookkeeping. Avoid premature close or duplicate cleanup.
SqlClient/SqlCommand.Xml.cs:127 Successful ownership transfers through SqlStream to the returned XmlReader. Check failures before transfer: metadata inspection happens outside the completion helper's cleanup try. A method-scoped using would break the returned XML reader.
SqlClient/Parser/TdsParserStateObject.cs:2943, 3120 DEBUG-only delay timers must stay rooted until firing and be disposed afterward. A method-scoped using would cancel the intended delayed operation. Preserve delayed completion, exception propagation, and write-count bookkeeping.

Suggested implementation sequence

  1. Land straightforward local cleanup with focused tests.
  2. Address borrowed-stream wrappers, table construction failures, and socket initialization.
  3. Document verified ownership false positives using the narrowest appropriate suppression or analyzer-friendly code shape. Do not blanket-disable CA2000.
  4. Fix and test async, transaction, XML-reader, and debug-timer lifetimes separately.
  5. Promote CA2000 to warning and run the complete applicable build matrix to discover and resolve remaining diagnostics.

Acceptance criteria

  • Root .editorconfig sets dotnet_diagnostic.CA2000.severity = warning.
  • Existing diagnostics are fixed or narrowly suppressed with an explicit, verified ownership rationale.
  • Caller-owned streams, returned readers/tables, pooled crypto providers, and transferred file/socket handles remain usable for their intended lifetimes.
  • Exception, cancellation, timeout, reconnection, and ownership-transfer paths clean up correctly without double disposal or premature disposal.
  • Regression tests cover behavior-sensitive changes, including sync/async counterparts where applicable.
  • Debug-only delayed-write behavior remains correct.
  • Full applicable builds pass, including downstream tests/samples/tools, supported target frameworks, and Windows-specific paths; record exact commands and results.
  • No global CA2000 suppression or relaxation of normal warnings-as-errors policy is introduced to make the promotion pass.
Lingua principale
C#
Stelle
1k
Fork
352
Merge medio
4g 17h
PR unite (30g)
82

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di dotnet/SqlClient

Tutte le issue di dotnet/SqlClient

Issue simili

Altre issue su C#

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.