Skip to content

Conversation

TimoTielens
Copy link

@TimoTielens TimoTielens commented Sep 8, 2025

Fixed scoping issues for cleaning up older content versions. When having large datasets (more then 2000 items)

Description

The CleanupDocumentVersions methods tries to clean up older versions of the content. The last step that actually performs the removal of the the contents versions is in an foreach. This foreach makes use of the scopelock. However there is an issue if we iterate multiple times over the logic. The second time we try to write the lock an exceptions occurs. By moving the lock outside the loop we fixed this.

Testing

  1. Make sure to have more then X content versions that need to be cleaned up. In my case X was a lot (13264)
  2. Lower the Constants.Sql.MaxParameterCount to a value lower then X.
  3. Double check it iterates multiple times. The expected result is that it fails the second iteration when we run on the 'old' code and that it doesn't fail on the 'new' code

@Copilot Copilot AI review requested due to automatic review settings September 8, 2025 11:21
Copy link

github-actions bot commented Sep 8, 2025

Hi there @TimoTielens, thank you for this contribution! 👍

While we wait for one of the Core Collaborators team to have a look at your work, we wanted to let you know about that we have a checklist for some of the things we will consider during review:

  • It's clear what problem this is solving, there's a connected issue or a description of what the changes do and how to test them
  • The automated tests all pass (see "Checks" tab on this PR)
  • The level of security for this contribution is the same or improved
  • The level of performance for this contribution is the same or improved
  • Avoids creating breaking changes; note that behavioral changes might also be perceived as breaking
  • If this is a new feature, Umbraco HQ provided guidance on the implementation beforehand
  • 💡 The contribution looks original and the contributor is presumably allowed to share it

Don't worry if you got something wrong. We like to think of a pull request as the start of a conversation, we're happy to provide guidance on improving your contribution.

If you realize that you might want to make some changes then you can do that by adding new commits to the branch you created for this work and pushing new commits. They should then automatically show up as updates to this pull request.

Thanks, from your friendly Umbraco GitHub bot 🤖 🙂

Copy link
Contributor

@Copilot 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 fixes a scoping issue in the ContentVersionService where multiple iterations of document version cleanup would fail due to attempting to acquire a write lock multiple times within nested scopes.

  • Moved the scope creation and write lock outside the foreach loop to prevent lock acquisition conflicts
  • Reorganized the code structure to have a single scope encompassing all batch operations
  • Cleaned up indentation and formatting of the notification publishing call

Comment on lines 272 to 288
using (ICoreScope scope = _scopeProvider.CreateCoreScope())
{
scope.WriteLock(Constants.Locks.ContentTree);
var groupEnumerated = group.ToList();
_documentVersionRepository.DeleteVersions(groupEnumerated.Select(x => x.VersionId));

foreach (ContentVersionMeta version in groupEnumerated)
foreach (IEnumerable<ContentVersionMeta> group in versionsToDelete.InGroupsOf(Constants.Sql.MaxParameterCount))
{
EventMessages messages = _eventMessagesFactory.Get();
var groupEnumerated = group.ToList();
_documentVersionRepository.DeleteVersions(groupEnumerated.Select(x => x.VersionId));

scope.Notifications.Publish(
new ContentDeletedVersionsNotification(version.ContentId, messages, version.VersionId));
}
foreach (ContentVersionMeta version in groupEnumerated)
{
EventMessages messages = _eventMessagesFactory.Get();

scope.Notifications.Publish(new ContentDeletedVersionsNotification(version.ContentId, messages, version.VersionId));
}
}
scope.Complete();
}
Copy link
Preview

Copilot AI Sep 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current implementation processes all version groups within a single scope, which could lead to very long-running transactions. For large datasets, this approach may cause database lock contention and timeout issues. Consider keeping the original per-batch scoping but fixing the lock acquisition issue differently, such as using a higher-level coordination mechanism.

Copilot uses AI. Check for mistakes.

@AndyButland
Copy link
Contributor

I haven't been able to replicate the issue that the PR is proposing to fix I'm afraid @TimoTielens - I took a database with a few hundred versions and looped in groups of 3, just ensure there are multiple passes. Also, I note there's some discussion in the file above about deliberately using multiple scopes and locks to not prevent other activity going on in the CMS.

Given that I'm reluctant to take this further.

Are you able to provide clearer steps for replication that can show there's an issue with the foreach here?

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