Skip to content

[ImageIO] Keep metadata alive while enumerating tags. Fixes #26545 - #26676

Merged
rolfbjarne merged 4 commits into
mainfrom
dev/rolf/fix-metadata-enumeration
Sep 24, 2026
Merged

rolfbjarne merged 4 commits into
mainfrom
dev/rolf/fix-metadata-enumeration

Conversation

@rolfbjarne

Copy link
Copy Markdown
Member

EnumerateTags passes only the native handle to ImageIO. Once that handle has been read, an optimizing runtime can collect the managed metadata owner while native enumeration is still in progress. Its finalizer can then release metadata needed by subsequent callbacks, which is consistent with the intermittent NativeAOT tag-retain crash.

Keep the owner alive until native enumeration returns. Add forced-GC coverage for full enumeration and early termination, checking the managed owner's weak reference. Retain the native metadata independently in the tests so a lifetime regression produces an assertion failure rather than a native crash.

Runtime confirmation is deferred to CI: Xcode 27 is unavailable locally, and the clean build was stopped during dependency restore.

Fixes #26545

🤖 Pull request created by Copilot

EnumerateTags passes only the native handle to ImageIO. Once that handle has been read, an optimizing runtime can collect the managed metadata owner while native enumeration is still in progress. Its finalizer can then release metadata needed by subsequent callbacks, which is consistent with the intermittent NativeAOT tag-retain crash.

Keep the owner alive until native enumeration returns. Add forced-GC coverage for full enumeration and early termination, checking the managed owner's weak reference. Retain the native metadata independently in the tests so a lifetime regression produces an assertion failure rather than a native crash.

Fixes #26545

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI lite review requested due to automatic review settings September 22, 2026 11:22

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.

Copilot review overview

🟢 Approval recommended

Only a minor test naming nit remains; no blocking issues were identified.

Review effort: Lite
Findings: 1 Low severity

Open (1)
What changed in this PR

Fixes a CGImageMetadata lifetime issue during native tag enumeration and adds forced-GC regression coverage.

Changes:

  • Keeps the managed metadata owner alive through enumeration.
  • Tests complete and early-terminated enumeration under forced GC.
  • Retains native metadata independently in tests.
  • Minor nit: clarify the ambiguous test name EnumerateMetadataWithCollection.
File Description
tests/​monotouch-test/​CoreGraphics/​CGImageMetadataTests.cs Adds lifetime regression tests.
src/​ImageIO/​CGImageMetadata.cs Preserves metadata owner lifetime during enumeration.

Rename EnumerateMetadataWithCollection to EnumerateMetadataWithForcedGC so the test name describes its garbage-collection lifetime scenario rather than suggesting collection traversal. Both existing test cases and their assertions remain unchanged.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings September 22, 2026 11:25

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.

Copilot review overview

🔵 Needs a closer look

Runtime confirmation is deferred to CI.

Review effort: Lite
Findings: None

Resolved since last review (1)

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@rolfbjarne rolfbjarne added the backport-to-net11.0 Add this to issues or pull requests that should be backported to .NET 11 label Sep 22, 2026
Copilot AI review requested due to automatic review settings September 23, 2026 11:17

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.

Copilot review overview

🔵 Needs a closer look

Stop full enumeration immediately when the weak-reference check fails to prevent later native callbacks.

Review effort: Lite
Findings: None

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

⚠️ AppSizeTest expected files changed ⚠️

The AppSizeTest detected changes in the expected app size files.

To update the expected files, add a comment with the following command:

/apply-gist https://gist.github.com/vs-mobiletools-engineering-service2/366fc22c906fe79dbe5d441be99815b5
Updated files
  • MacOSX-CoreCLR-R2R-size.txt

Pipeline on Agent
Hash: a813b6ea761c243bc558edd0daee3d93d0f021f6 [PR build]

Copilot AI review requested due to automatic review settings September 23, 2026 14:21

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.

Copilot review overview

🔵 Needs a closer look

An unresolved moderate testing concern remains around collection during initial tag proxy construction.

Review effort: Lite
Findings: None

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

✅ API diff for current PR / commit

NET (empty diffs)

✅ API diff vs stable

NET (empty diffs)

ℹ️ Generator diff

Generator Diff: vsdrops (html) vsdrops (raw diff) gist (raw diff) - Please review changes)

Pipeline on Agent
Hash: 967512dd451da1a2df9140c6d786ecdd26359db6 [PR build]

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

🚀 [CI Build #967512d] Test results 🚀

Test results

✅ All tests passed on VSTS: test results.

🎉 All 269 tests passed 🎉

Tests counts

✅ assembly-processing: All 1 tests passed. Html Report (VSDrops) Download
✅ cecil: All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (iOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (MacCatalyst): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (macOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (Multiple platforms): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (tvOS): All 1 tests passed. Html Report (VSDrops) Download
✅ framework: All 2 tests passed. Html Report (VSDrops) Download
✅ fsharp: All 4 tests passed. Html Report (VSDrops) Download
✅ generator: All 5 tests passed. Html Report (VSDrops) Download
✅ interdependent-binding-projects: All 4 tests passed. Html Report (VSDrops) Download
✅ introspection: All 7 tests passed. Html Report (VSDrops) Download
✅ linker (iOS): All 31 tests passed. Html Report (VSDrops) Download
✅ linker (MacCatalyst): All 31 tests passed. Html Report (VSDrops) Download
✅ linker (macOS): All 21 tests passed. Html Report (VSDrops) Download
✅ linker (tvOS): All 31 tests passed. Html Report (VSDrops) Download
✅ monotouch (iOS): All 25 tests passed. Html Report (VSDrops) Download
✅ monotouch (MacCatalyst): All 25 tests passed. Html Report (VSDrops) Download
✅ monotouch (macOS): All 20 tests passed. Html Report (VSDrops) Download
✅ monotouch (tvOS): All 25 tests passed. Html Report (VSDrops) Download
✅ msbuild: All 2 tests passed. Html Report (VSDrops) Download
✅ sharpie: All 1 tests passed. Html Report (VSDrops) Download
✅ windows: All 3 tests passed. [attempt 3] Html Report (VSDrops) Download
✅ xcframework: All 4 tests passed. Html Report (VSDrops) Download
✅ xtro: All 1 tests passed. Html Report (VSDrops) Download

macOS tests

✅ Tests on macOS Sonoma (14): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Sequoia (15): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Tahoe (26): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Golden Gate (27): All 5 tests passed. Html Report (VSDrops) Download

Linux Build Verification

✅ Linux build succeeded

Pipeline on Agent
Hash: 967512dd451da1a2df9140c6d786ecdd26359db6 [PR build]

@rolfbjarne
rolfbjarne enabled auto-merge (squash) September 24, 2026 07:04
@rolfbjarne rolfbjarne added the ready-to-review This PR is ready to review/merge. label Sep 24, 2026
@rolfbjarne
rolfbjarne merged commit 6f1d2e5 into main Sep 24, 2026
56 checks passed
@rolfbjarne
rolfbjarne deleted the dev/rolf/fix-metadata-enumeration branch September 24, 2026 10:31
rolfbjarne added a commit that referenced this pull request Sep 26, 2026
…ng tags. Fixes #26545 (#26713)

EnumerateTags passes only the native handle to ImageIO. Once that handle has been read, an optimizing runtime can collect the managed metadata owner while native enumeration is still in progress. Its finalizer can then release metadata needed by subsequent callbacks, which is consistent with the intermittent NativeAOT tag-retain crash.

Keep the owner alive until native enumeration returns. Add forced-GC coverage for full enumeration and early termination, checking the managed owner's weak reference. Retain the native metadata independently in the tests so a lifetime regression produces an assertion failure rather than a native crash.

Fixes #26545

🤖 Pull request created by Copilot

Backport of #26676.

---------

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
rolfbjarne added a commit that referenced this pull request Oct 28, 2026
#26704)

## Summary

Follow up on the lifetime audit prompted by #26545 and #26676. Native enumeration receives only the owner's handle; rooting the callback does not necessarily keep its managed owner alive while callbacks allocate or trigger GC.

Add 37 `GC.KeepAlive (this)` guards after synchronous native calls: the 29 additional enumerators identified by the audit, font sorting, pre-shared-key access, and six adjacent callback operations. Preserve native return values and keep owners alive before callback state is released. The ImageIO fix in #26676 is not duplicated.

Fix `DispatchQueue.Submit` by retaining its callback/owner tuple until all iterations finish, freeing the handle once in `finally`, and using the matching `Action<int>` delegate type.

Correct related defects encountered while exercising these APIs: native boolean callback return types, borrowed metadata ownership, peer certificate conversion, the signature-algorithm success check, pre-shared-key delegate/ownership handling, and recursive WebSocket response header forwarding.

## Coverage

Add forced-GC regressions for path application, caret enumeration (full traversal and early termination), all four tag collection operations, and serial/concurrent dispatch iterations. The path, line, and tag tests independently retain native owners so regressions fail assertions instead of crashing.

Add or extend functional coverage for browse-result interfaces, prohibited interface objects, content-context metadata, establishment-report protocols, WebSocket response headers, Security metadata accessors, and connection batching.

WebSocket request enumeration and framer parsing tests remain deferred: their existing public callback APIs require separate native return-contract corrections. Optional TLS collections are checked when available, and the existing fixture does not provide a TLS-PSK server to exercise actual PSK callbacks.

🤖 Pull request created by Copilot

---------

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-to-net11.0 Add this to issues or pull requests that should be backported to .NET 11 copilot ready-to-review This PR is ready to review/merge.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[CI] NativeAOT CGImageMetadataTest crashes retaining CGImageMetadataTag proxy

4 participants