Skip to content

Cleanup some C# warnings and dispose behavior. Make formatting more consistent.#823

Open
skottmckay wants to merge 2 commits into
mainfrom
skottmckay/CSharpCleanups
Open

Cleanup some C# warnings and dispose behavior. Make formatting more consistent.#823
skottmckay wants to merge 2 commits into
mainfrom
skottmckay/CSharpCleanups

Conversation

@skottmckay

@skottmckay skottmckay commented Jun 19, 2026

Copy link
Copy Markdown
Collaborator
  • Disable CA2000. IDisposableAnalyzers is the modern way to check for IDisposable issues and handles some transfer scenarios like the ones we have.
  • Fix usage of PinContext so we don't get an IDISP001
  • Fix ordering in ItemQueue.Push so we don't leak on push failure.
  • Make native logging more deterministic when running in debug mode. Should make the log outputs match up better so CI logs are easier to debug.
  • Update C# instructions to reflect change to use RelWithDebInfo build which is now the default.
  • Run formatter to make header/namespace/using statement setup consistent

Suggest turning off whitespace diffs when reviewing.

Make native logging more deterministic when running in debug mode. Should make the log outputs match up better so CI logs are easier to debug.
Copilot AI review requested due to automatic review settings June 19, 2026 05:49
@vercel

vercel Bot commented Jun 19, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
foundry-local Ready Ready Preview, Comment Jun 19, 2026 8:36am

Request Review

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 PR is a focused cleanup of the C# SDK's IDisposable/warning hygiene plus a small native-logging tweak. It centralizes the pin→GCHandle ownership-transfer logic for "owned" items into a single helper, makes ItemQueue.Push leak-safe on failure, disables the noisy CA2000 analyzer (deferring to IDisposableAnalyzers), makes native log flushing more deterministic in debug, and refreshes the C# debug-binding instructions to match the existing RelWithDebInfo auto-detect.

Changes:

  • Add PinContext.PinForNativeDeleter(...) as the single ownership-transfer chokepoint (makes AllocForNativeDeleter a private static helper) and route BytesItem/ImageItem/AudioItem owned factories through it; reorder ItemQueue.Push so C#-side ownership is released only after a successful native push.
  • Disable CA2000 project-wide (NoWarn in both csprojs + .editorconfig none), documenting that IDisposableAnalyzers is the disposal-correctness authority.
  • Flush native logs on every message in debug builds or when Debug/Verbose is requested; add a memories/repo doc and update the C#-debug-binding instructions (Debug → RelWithDebInfo).

Reviewed changes

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

Show a summary per file
File Description
sdk_v2/cs/src/Items/PinContext.cs New PinForNativeDeleter chokepoint; AllocForNativeDeleter becomes private static; single IDISP001 suppression.
sdk_v2/cs/src/Items/ItemQueue.cs Push now calls native first and releases ownership only on success, avoiding handle leak on failure.
sdk_v2/cs/src/Items/BytesItem.cs Owned factory routes through PinForNativeDeleter; pragma suppressions removed; namespace/using reordered.
sdk_v2/cs/src/Items/ImageItem.cs Same owned-factory refactor and header reorder as BytesItem.
sdk_v2/cs/src/Items/AudioItem.cs Both owned factories route through PinForNativeDeleter; header reorder.
sdk_v2/cs/src/Microsoft.AI.Foundry.Local.csproj Adds NoWarn;CA2000 with rationale.
sdk_v2/cs/test/FoundryLocal.Tests/...Tests.csproj Adds NoWarn;CA2000 for the test project.
sdk_v2/cs/.editorconfig CA2000 severity downgraded from warning to none with rationale.
sdk_v2/cpp/src/spdlog_logger.cc Flush-every-message in debug builds or Debug/Verbose level; otherwise flush at warning+.
memories/repo/cs-item-ownership-transfer.md New doc capturing the C# item ownership/CA2000 model.
.github/instructions/cs-debug-binding.instructions.md Updates guidance from Debug to RelWithDebInfo to match csproj auto-detect.

Comment thread sdk_v2/cs/src/Items/PinContext.cs
@skottmckay skottmckay changed the title Cleanup some C# warnings and dispose behavior Cleanup some C# warnings and dispose behavior. Make formatting more consistent. Jun 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants