Repository navigation
Description
Windows CoreCLR built-in COM connection points can delete a ConnectionCookie while it is still linked. The strongest source-derived manifestation needs no race: UnadviseWorker takes deleting ownership of a linked cookie, invokes managed remove_X code, and unlinks only afterward. If the managed accessor or another throwing operation fails, unwinding deletes the cookie and its strong handle while the list and insertion bookkeeping can retain the freed address.
The same ownership gap allows concurrent or callback-reentrant Unadvise operations to construct multiple deleting holders for one cookie. ConnectionEnum also retains an unpinned cookie pointer between calls, and AdviseWorker continues using its constructing pointer after publication. These findings apply to supported Windows Release configurations with built-in COM interop, including x64 and Arm64. They were validated from source; no build, test, or dynamic crash reproduction was performed. No matching duplicate search hit was confirmed.
Evidence
src\coreclr\vm\comconnectionpoints.cpp:378-398-UnadviseWorkerconstructs a deleting holder from a still-linked raw lookup result, invokes managed provider removal, and only then unlinks the node.src\coreclr\System.Private.CoreLib\src\System\StubHelpers.cs:2288-2311andsrc\coreclr\vm\callhelpers.h:410-455- managed provider exceptions are captured at the reverse-P/Invoke boundary and explicitly rethrown intoUnadviseWorker, so line 398 can be skipped.src\coreclr\inc\holder.h:222-285,753-838andsrc\coreclr\vm\comconnectionpoints.h:36-74- constructingConnectionCookieHolderfrom the raw pointer acquires deleting ownership; the cookie has no refcount or removal state.src\coreclr\vm\comconnectionpoints.cpp:714-751andsrc\coreclr\inc\slist.h:330-369- lookup releasesCrstInteropbefore returning a borrowed pointer; a missing pointer-based unlink returnsfalse, and its caller ignores the result.src\coreclr\vm\comconnectionpoints.cpp:986-1000,1115-1190-ConnectionEnumreads the head without the connection-point lock and retainsm_CurrCookieacross calls, allowing a later locked dereference of a reclaimed node.src\coreclr\vm\comconnectionpoints.cpp:346-355,580-631-AdviseWorkerpublishes the node under the list lock, then reads its ID and relinquishes constructing ownership after unlocking.
Feasible interleaving
- A single caller invokes
Unadvise; the worker finds active cookie C and constructs a deleting holder while C remains linked. - A managed
remove_Xaccessor throws, andInvokeThrowingpropagates the exception into the worker. - Unwinding skips the final unlink but destroys the holder, deleting C and its strong GC handle while the list can still name C.
- A later
Advise,Unadvise, or enumeration traverses or writes through the freed node in Release.
Expected behavior
A published cookie should have one well-defined owner. Removal must establish exclusive lifetime before user callbacks, and no linked list or enumerator may retain a pointer after the cookie can be reclaimed. A failed removal must leave either a valid retryable connection or a fully detached cookie according to an explicit failure policy.
Actual behavior
A locked lookup returns a linked, unpinned raw pointer that each caller independently converts into deleting ownership. Unlinking happens only after arbitrary managed callbacks. Exception unwind can therefore delete a linked node; concurrent or reentrant callers can both delete one allocation; and an enumerator can dereference a node removed between calls. The ignored SList removal result provides no Release signal when a second remover loses.
The duplicate-holder execution guarantees undefined behavior from a second delete, but a second DestroyHandle of the identical value is not deterministic: the first LifetimeHolder::Free clears its stored value before the allocation is freed, after which reuse controls what the stale destructor observes.
Suggested fix
Use the existing CrstInterop to define list ownership and a two-phase removal claim:
- Publish a fully initialized cookie, capture its ID, and transfer constructing ownership to the list while holding the lock. Do not dereference the cookie from
AdviseWorkerafter that handoff. - Find by ID and transition
ActivetoRemovingunder the lock. Missing or already-removing IDs returnCONNECT_E_NOCONNECTION; never give a linked node to a deleting holder. - Leave the cookie list-owned and alive while invoking managed accessors outside the lock. On success, unlink and update
m_pLastInsertedunder the lock, transfer to exactly one deleting holder, then destroy outside the lock. - On exception, a nothrow claim guard restores
Activeunder the lock without allocation, callbacks, COM calls, or handle destruction. Document that arbitrary accessor side effects cannot be rolled back. - Replace
ConnectionEnum's persistent raw pointer with a lock-protected ID cursor or an independently rooted snapshot. Copy/root an item under the short lock and perform COM conversion afterward.
A CAS, volatile access, or fence on a cookie field is insufficient because lookup does not protect the allocation while the CAS or refcount is attempted. A lock-free alternative would require a complete reclamation protocol across removal, enumeration, publication, and teardown.
Safety considerations
Keep all managed callbacks, type loading, delegate allocation, COM interface conversion, waits, and cookie/handle destruction outside CrstInterop; the lock is not intended for recursive managed reentry, and handle destruction can take other locks or notify diagnostics. Lock only lookup, claim transitions, publication ownership transfer, list mutation, bookkeeping, and short cursor/object capture.
The claim keeps the native node alive independently of cooperative GC mode. Managed objects must remain GC-protected across callback- and allocation-capable work. Lock release/acquisition supplies the required ordering on x64 and Arm64; this is not a weak-memory bug, and fences without reclamation do not help. The list is already locked, so short claim transitions add little expected contention. An ID cursor may rescan a normally short list, while a snapshot adds allocation and retention; choose and test the intended enumeration policy.
Suggested validation
Add a deterministic COM event test whose managed remove_X accessor throws once, then verify the list remains valid and a retry follows the selected rollback policy. Add gated MTA callers racing Unadvise for one real cookie and a guarded callback-reentrant removal; exactly one claim should invoke removal, and the other should receive CONNECT_E_NOCONNECTION without deadlock. Count cookie and handle creation/destruction in test instrumentation.
Exercise Next, Skip, Reset, and Clone while removing current and next entries, including insertion, ID reuse, end-of-list, and maximum-ID cases. Add a publication scheduling hook around Advise. Run the focused suite on Windows x64 and Arm64 Release, with Checked lock/GC diagnostics, GC stress, and PageHeap or equivalent native memory instrumentation. No such validation was run while preparing this draft.
Reviewer synthesis
Both reviewers validated the Release ownership defect, COM classification, architecture independence, enumeration coupling, and absence of a confirmed duplicate. The draft leads with reviewer B's stronger deterministic exception manifestation and uses its corrected Unadvise range (src\coreclr\vm\comconnectionpoints.cpp:227-254).
For the repair, the selected synthesis follows reviewer A's removal-claim direction rather than reviewer B's unconditional early unlink. Early unlink is smaller and memory-safe, but it permanently consumes the cookie and permits ID reuse when managed removal throws. A list-owned Removing state preserves the apparent retry and ID-reservation behavior without holding a VM lock across user code. Publication ownership and enumerator lifetime must be repaired under the same contract.
Note
This issue draft was generated with GitHub Copilot and requires maintainer review before filing.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsNo status
Activity
dotnet-policy-service commentedon Sep 19, 2026
Tagging subscribers to this area: @dotnet/interop-contrib
See info in area-owners.md if you want to be subscribed.