Skip to content

Document DeleteObjectReference to prevent memory leaks in Orleans observers#51986

Open
Copilot wants to merge 4 commits intomainfrom
copilot/fix-observer-reference-memory-leak
Open

Document DeleteObjectReference to prevent memory leaks in Orleans observers#51986
Copilot wants to merge 4 commits intomainfrom
copilot/fix-observer-reference-memory-leak

Conversation

Copy link
Contributor

Copilot AI commented Mar 3, 2026

Calling CreateObjectReference without a matching DeleteObjectReference leaves a reference in InvokableObjectManager.localObjects, causing a memory leak that can eventually crash the client process. The observers article never mentioned this requirement.

Changes

  • Intro paragraph: Added that DeleteObjectReference must be called when the observer is no longer needed, cross-referenced via <xref:Orleans.IGrainFactory.DeleteObjectReference*>.
  • New cleanup example: Added code showing proper teardown after the existing subscription example:
    // Unsubscribe the observer when it's no longer needed.
    await friend.UnSubscribe(obj);
    
    // Delete the object reference to free resources and avoid a memory leak.
    _grainFactory.DeleteObjectReference<IChat>(obj);
  • [!IMPORTANT] callout: Warns that omitting DeleteObjectReference leaks a reference in the client's internal object manager and can eventually crash the process.
Original prompt

This section details on the original issue you should resolve

<issue_title>Mention DeleteObjectReference when talking about Orleans Observers</issue_title>
<issue_description>### Describe the issue or suggestion

https://github.com/dotnet/docs/blob/main/docs/orleans/grains/observers.md?target=https://github.com

The observers page teaches calling CreateObjectReference to enable an Orleans client to register an IGrainObserver with the library. It does not mention that this call needs to be paired with DeleteObjectReference.

If the latter is not called when done—at least for the OutsideRuntimeClient—a small reference remains in the InvokableObjectManager.localObjects dictionary, effectively becoming a memory leak, that will kill the (client) process eventually.


Associated WorkItem - 558079</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.


Internal previews

📄 File 🔗 Preview link
docs/orleans/grains/observers.md Observers

Co-authored-by: meaghanlewis <10103121+meaghanlewis@users.noreply.github.com>
Copilot AI changed the title [WIP] Mention DeleteObjectReference for Orleans observers Document DeleteObjectReference to prevent memory leaks in Orleans observers Mar 3, 2026
@meaghanlewis meaghanlewis marked this pull request as ready for review March 3, 2026 22:23
@meaghanlewis meaghanlewis self-requested a review as a code owner March 3, 2026 22:23
Copilot AI review requested due to automatic review settings March 3, 2026 22:23
@meaghanlewis meaghanlewis requested a review from ReubenBond as a code owner March 3, 2026 22:23
Copy link
Contributor

Copilot AI left a comment

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 updates the Orleans observers documentation to explicitly call out that CreateObjectReference must be paired with DeleteObjectReference to avoid leaking registrations in the client and potentially crashing long-running client processes.

Changes:

  • Adds DeleteObjectReference guidance to the introductory explanation of client observers.
  • Adds a cleanup snippet showing unsubscribe + object reference deletion.
  • Adds an [!IMPORTANT] warning emphasizing the memory-leak/crash risk when DeleteObjectReference is omitted.

You can also share your feedback on Copilot code review. Take the survey.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
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.

Mention DeleteObjectReference when talking about Orleans Observers

3 participants