Skip to content

Conversation

@ramonsmits
Copy link
Member

@ramonsmits ramonsmits commented Jun 6, 2024

It frequently happens that for unknown reasons files are still in use which result in errors like the following:

The process cannot access the file 'H:\ServiceControl\servicecontrol-test-monitoring\bin\System.Transactions.Local.dll' because it is being used by another process.

This change improves the installer logic when upgrading instances by first preparing the new version in a new folder and when that completes swap the folder of the current instance for the folder of the new instance. This resolves most if not all issues around files that are locked or in use.

It also makes the folder delete operation more resilient against very short lock that can be caused because the instance although reporting stopped in Windows Service Control Manager to still be running for a very little time or because a virusscanner locks a file or any other reason for a file to be in use.

Summarized:

  1. Installer upgrade first prepares folder, then swaps current/new folder. Previously immediately updated files in existing folder.
  2. Retries file operations when failing with an IOException 10x with a 100ms sleep to allow for short file locks
  3. Renames (move to the same parent folder with different name) the folder which only passes when there are no files in use/locked

This is tested with all 3 monitor types by upgrading 5.2.4 to 5.3.0-alpha.0.30 from the build server.

@ramonsmits ramonsmits self-assigned this Jun 6, 2024
@ramonsmits ramonsmits changed the title Move the folder first to ensure no files are in use and locked Folder deletion - Move the folder first to ensure no files are in use and locked Jun 6, 2024
Copy link
Member

@johnsimons johnsimons left a comment

Choose a reason for hiding this comment

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

I have added a few comments, but I was thinking that adding a retry with a delay would make things better.
From past experience Antivirus software can cause this kind of locking issues.
Something like https://stackoverflow.com/a/44324346 may be a good start, thoughts?

Copy link
Member

@johnsimons johnsimons left a comment

Choose a reason for hiding this comment

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

@ramonsmits LGTM, but I wonder now that you have added retries for the deletion, whether we even need to bother doing the move before hand, something to consider?

@ramonsmits ramonsmits requested a review from johnsimons June 11, 2024 15:21
@ramonsmits
Copy link
Member Author

@ramonsmits ramonsmits changed the title Folder deletion - Move the folder first to ensure no files are in use and locked Made installer upgrade more resilient to files in use and locked Jun 12, 2024
@ramonsmits ramonsmits changed the title Made installer upgrade more resilient to files in use and locked Installer upgrade behavior more resilient when files are in use and locked Jun 12, 2024
ramonsmits and others added 7 commits June 12, 2024 12:54
Co-authored-by: Andreas Bednarz <110360248+abparticular@users.noreply.github.com>
Co-authored-by: Andreas Bednarz <110360248+abparticular@users.noreply.github.com>
Co-authored-by: Jo Palac <jpalac@users.noreply.github.com>
@ramonsmits ramonsmits force-pushed the move-before-delete branch from 061593a to 7682ca0 Compare June 12, 2024 10:54
@ramonsmits ramonsmits added this to the 5.3.0 milestone Jun 12, 2024
Copy link
Member

@johnsimons johnsimons left a comment

Choose a reason for hiding this comment

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

I have added a few more comments for consideration

}
catch (Exception ex)
{
Trace.WriteLine($"ServiceControlInstaller.Engine.Instances.BaseService::PurgeOld Unable to cleanup {oldPath}. Reason: {ex.Message} ({ex.GetType().FullName})");
Copy link
Member

Choose a reason for hiding this comment

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

Why aren't we using the Logging class?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants