Fix memory leak with IOptionsMonitor.OnChange and non-singleton registered components (closes #20709 for 13)#20722
Merged
AndyButland merged 6 commits intov13/devfrom Nov 6, 2025
Conversation
…tered components.
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR fixes a memory leak issue by properly disposing of IOptionsMonitor.OnChange subscriptions. The changes ensure that change tracking callbacks registered with IOptionsMonitor.OnChange are properly cleaned up when the classes are disposed.
- Converts mutable fields to
readonlywhere subscriptions are now properly tracked - Captures the
IDisposablereturned byOnChangeto enable proper cleanup - Implements
IDisposableon classes that subscribe to option changes to dispose of subscriptions
Reviewed Changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Umbraco.Web.BackOffice/Controllers/ImagesController.cs | Made _contentSettings field readonly and removed dynamic change tracking |
| src/Umbraco.Web.BackOffice/Controllers/HelpController.cs | Made _helpPageSettings field readonly and removed dynamic change tracking |
| src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/RteMacroRenderingValueConverter.cs | Implemented IDisposable and captured OnChange subscription for proper disposal |
| src/Umbraco.Infrastructure/PropertyEditors/UploadFileTypeValidator.cs | Implemented IDisposable and captured OnChange subscription for proper disposal |
| src/Umbraco.Infrastructure/PropertyEditors/ImageCropperPropertyValueEditor.cs | Implemented IDisposable and captured OnChange subscription for proper disposal |
| src/Umbraco.Infrastructure/PropertyEditors/ImageCropperPropertyEditor.cs | Implemented IDisposable and captured OnChange subscription for proper disposal |
| src/Umbraco.Infrastructure/PropertyEditors/FileUploadPropertyValueEditor.cs | Implemented IDisposable and captured OnChange subscription for proper disposal |
src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/RteMacroRenderingValueConverter.cs
Show resolved
Hide resolved
src/Umbraco.Infrastructure/PropertyEditors/ImageCropperPropertyValueEditor.cs
Show resolved
Hide resolved
src/Umbraco.Infrastructure/PropertyEditors/ImageCropperPropertyEditor.cs
Outdated
Show resolved
Hide resolved
src/Umbraco.Infrastructure/PropertyEditors/FileUploadPropertyValueEditor.cs
Show resolved
Hide resolved
IOptionsMonitor.OnChange and non-singleton registered components.IOptionsMonitor.OnChange and non-singleton registered components (closes #20709 for 13)
Migaroez
approved these changes
Nov 6, 2025
AndyButland
added a commit
that referenced
this pull request
Nov 6, 2025
…istered components (closes #20709 for 13) (#20722) * Fix memory leak with IOptionsMonitor.OnChange and non-singleton registered components. * Added XML docs. * Apply fix to DeliveryApiContentIndexingNotificationHandler. * Dispose disposable data editors in ValueEditorCache. * Removed unnecessary refactoring and clarified code comments.
This was referenced Dec 9, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Prerequisites
Addresses #20709
Description
The linked issue points out a potential problem with how we are handling
IOptionsMonitor.OnChangein components that aren't registered as singletons.Why the current pattern is a concern:
I've checked all instances of this and found only the classes amended in this PR to not be registered as singletons. So other instances are safe. I've resolved by either:
private readonly IDisposable? _optionsChangeSubscription;, assigning it in the constructor, implementingIDisposableand disposing of the reference.OnChange- as we won't need it given controllers are transient.