Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FileSystem - consider making directory creation invulnerable to transient UnauthorizedAccessException #106

Closed
madelson opened this issue Oct 2, 2021 · 1 comment
Labels
Milestone

Comments

@madelson
Copy link
Owner

madelson commented Oct 2, 2021

For file locks, we have this line of code:

                try { System.IO.Directory.CreateDirectory(this.Directory); }
                catch (Exception ex)
                {
                    throw new InvalidOperationException($"Failed to ensure that lock file directory {this.Directory} exists", ex);
                }

Very rarely when running TestParallelism, I've seen this fail with UnauthorizedAccessException, presumably due to some form of concurrent directory creation. We can make this less vulnerable by being willing to retry directory creation a certain number of times when hitting that particular exception.

@madelson
Copy link
Owner Author

Repro:

var taskCount = 20;
using var barrier = new Barrier(taskCount);

var path = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());

var tasks = Enumerable.Range(0, taskCount)
	.Select(i => Task.Run(() =>
	{
		barrier.SignalAndWait();

		for (var i = 0; i < 1000; ++i)
		{
			try
			{
				Directory.CreateDirectory(path);
				Directory.Delete(path);
			}
			catch (IOException) { }
		}
	}))
	.ToArray();

try
{
	Task.WaitAll(tasks); // fails with UnauthorizedAccessException (wrapped in AggregateException)
}
finally
{
	File.Delete(path);
}

@madelson madelson modified the milestones: 2.2, 2.3 Nov 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant