-
Notifications
You must be signed in to change notification settings - Fork 5.1k
FileStream.Unix: improve DeleteOnClose when FileShare.None. #55327
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
191b675
FileStream.Unix: improve DeleteOnClose when FileShare.None.
tmds 3f904c7
Rephrase comment
tmds b0c10d5
Merge branch 'main' into deleteonclose_none
tmds fb6ad3b
FileStream_DeleteOnClose: handle a few special cases.
tmds 1bf6b0d
Try avoiding test timeout on arm
tmds bc2fc02
Limit check to filemodes that open an existing file, or create it whe…
tmds 88c9640
PR feedback
tmds be7fbc3
Limit functional change to FileMode.OpenOrCreate, and increase test t…
tmds 54baa38
Fix comment.
tmds File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
src/libraries/System.IO.FileSystem/tests/FileStream/DeleteOnClose.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace System.IO.Tests | ||
{ | ||
public class FileStream_DeleteOnClose : FileSystemTest | ||
{ | ||
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsFileLockingEnabled), nameof(PlatformDetection.IsThreadingSupported))] | ||
public async Task OpenOrCreate_DeleteOnClose_UsableAsMutex() | ||
{ | ||
var cts = new CancellationTokenSource(); | ||
int enterCount = 0; | ||
int locksRemaining = int.MaxValue; | ||
bool exclusive = true; | ||
|
||
string path = GetTestFilePath(); | ||
Assert.False(File.Exists(path)); | ||
|
||
Func<Task> lockFile = async () => | ||
{ | ||
while (!cts.IsCancellationRequested) | ||
{ | ||
try | ||
{ | ||
using (var fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None, 4096, FileOptions.DeleteOnClose)) | ||
{ | ||
int counter = Interlocked.Increment(ref enterCount); | ||
if (counter != 1) | ||
{ | ||
// Test failed. | ||
exclusive = false; | ||
cts.Cancel(); // Stop other Tasks. | ||
return; | ||
} | ||
|
||
// Hold the lock for a little bit. | ||
await Task.Delay(TimeSpan.FromMilliseconds(5)); | ||
|
||
Interlocked.Decrement(ref enterCount); | ||
|
||
if (Interlocked.Decrement(ref locksRemaining) <= 0) | ||
{ | ||
return; | ||
} | ||
} | ||
await Task.Delay(TimeSpan.FromMilliseconds(1)); | ||
} | ||
catch (UnauthorizedAccessException) | ||
{ | ||
// This can occur when the file is being deleted on Windows. | ||
await Task.Delay(TimeSpan.FromMilliseconds(1)); | ||
} | ||
catch (IOException) | ||
{ | ||
// The file is opened by another Task. | ||
await Task.Delay(TimeSpan.FromMilliseconds(1)); | ||
} | ||
} | ||
}; | ||
|
||
var tasks = new Task[50]; | ||
for (int i = 0; i < tasks.Length; i++) | ||
{ | ||
tasks[i] = Task.Run(lockFile); | ||
} | ||
|
||
// Wait for 1000 locks. | ||
cts.CancelAfter(TimeSpan.FromSeconds(60)); // Test timeout. | ||
Volatile.Write(ref locksRemaining, 500); | ||
await Task.WhenAll(tasks); | ||
|
||
Assert.True(exclusive, "Exclusive"); | ||
Assert.False(cts.IsCancellationRequested, "Test cancelled"); | ||
tmds marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Assert.False(File.Exists(path), "File exists"); | ||
} | ||
} | ||
} |
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.