-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Make polling use the symbolic link target's LastWriteTime #55664
Conversation
Tagging subscribers to this area: @dotnet/area-system-io Issue DetailsFixes #36091 Adding
|
@@ -38,7 +38,7 @@ public class PollingFileChangeToken : IPollingChangeToken | |||
/// <param name="fileInfo">The <see cref="System.IO.FileInfo"/> to poll</param> | |||
public PollingFileChangeToken(FileInfo fileInfo) | |||
{ | |||
_fileInfo = fileInfo; | |||
_fileInfo = FileSystemInfoHelper.ResolveFileLinkTarget(fileInfo) ?? fileInfo; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this raise new exceptions if the target of the file is not reachable? Perhaps we can add a test case for that: watching a normal file, watching a non-existent file, watching a symlinked file, watching a symlink with non-existent target.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It shouldn't, Exists
and LinkTarget != null
should guard for that.
Lines 42 to 49 in 08233ba
if (fileInfo.Exists && fileInfo.LinkTarget != null) | |
{ | |
FileSystemInfo targetInfo = fileInfo.ResolveLinkTarget(returnFinalTarget: true); | |
if (targetInfo.Exists) | |
{ | |
return (FileInfo)targetInfo; | |
} | |
} |
However since these APIs are not atomic, it could be that the link gets deleted between LinkTarget and ResolveLinkTarget. I don't know if there's an atomic way to do this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's worse than just not being atomic, since you capture the FileInfo
for the lifetime of the token, which might be long.
Here's another scenario. Suppose that the symlink initially points at one target, but then is changed to point at a new target and that new target has a new modified time? I think that's actually the scenario in this issue. If you captured the FileInfo
from the initial target you'd miss that change since you'd be checking the original target.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if the symlinks chain goes through an area that you can't read?
touch bar
mkdir foo
cd foo
ln -s ../bar .
cd ..
ln -s baz foo/bar
sha256sum bar baz
chmod a-x foo
sha256sum bar baz
In our world, baz.LinkTarget should resolve (to foo/bar). Does foo/bar then report Exists is false since it can't be read (the directory is no longer allowed to be traversed into)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In our world, baz.LinkTarget should resolve (to foo/bar). Does foo/bar then report Exists is false since it can't be read (the directory is no longer allowed to be traversed into)?
@bartonjs Yes, that's what I would expect. Therefore this should print false, unless that the FileInfo .ctor does some validation on inaccessible paths:
var targetInfo = File.ResolveLinkTarget("baz", returnFinaltarget: true);
Console.Write(targetInfo.Exists);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for addressing the link target feedback. Please consider adding test cases for the deleted link target cases, as they seem unique to this addition as it has to handle non-existent link targets, and link targets that get deleted / added.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the link target does not exists, we are returning the LastWriteTimeUtc
from the link itself but I think that doesn't tell us anything, I will change it to return DateTime.MinValue
instead (same thing we do when the file does not exists).
Let me know if you think that could be problematic.
I also added the requested tests: 98b737a.
src/libraries/Microsoft.Extensions.FileProviders.Physical/src/PollingWildCardChangeToken.cs
Outdated
Show resolved
Hide resolved
...nsions.FileProviders.Abstractions/ref/Microsoft.Extensions.FileProviders.Abstractions.csproj
Outdated
Show resolved
Hide resolved
@@ -2,5 +2,6 @@ | |||
<Import Project="..\Directory.Build.props" /> | |||
<PropertyGroup> | |||
<IsAspNetCoreApp>true</IsAspNetCoreApp> | |||
<UnsupportedOSPlatforms>browser</UnsupportedOSPlatforms> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How is this unsupported on browser? Is the whole assembly not supported on browser?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this project references System.IO.FileSystem.Watcher and that is not supported in browser:
<UnsupportedOSPlatforms>browser</UnsupportedOSPlatforms> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, so then should we add a runtime assembly for Browser that throws PNSE?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/cc @lewing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, so then should we add a runtime assembly for Browser that throws PNSE?
Yes which is what this setting does
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This setting doesn't generate a PNSE automatically, this setting is just to add an assembly attribute for the platform analyzer. Unless something changed since last time I did it, in order to generate PNSE we need to add a -Browser
tfm and then set the flag to generate a pnse when TargetsBrowser == true. So then it seems like we should do that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...s/Microsoft.Extensions.FileSystemGlobbing/ref/Microsoft.Extensions.FileSystemGlobbing.csproj
Outdated
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.FileProviders.Physical/tests/Directory.Build.props
Outdated
Show resolved
Hide resolved
// Act 2 - Change link target to file 2. | ||
token = provider.Watch(filter); // Once HasChanged is true, the value will always be true. Get a new change token. | ||
Assert.False(token.HasChanged); | ||
File.Delete(linkPath); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to reset the target without deleting the link?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Through File.CreateSymbolicLink
, it is not. It may be possible to modify the target of an existing link by using FSCTL_SET_REPARSE_POINT
, see https://docs.microsoft.com/en-us/windows/win32/fileio/reparse-point-operations.
For Unix, I know that you can use the -f switch in ln -sf bar foo
to force the link to be created even if it already exists, but I guess that's just deleting the old link.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/ln.html says that -f
in ln
is functionally equivalent to "call unlink", which we call File.Delete
.
...es/Microsoft.Extensions.FileProviders.Physical/tests/PhysicalFileProviderTests.netcoreapp.cs
Outdated
Show resolved
Hide resolved
...es/Microsoft.Extensions.FileProviders.Physical/tests/PhysicalFileProviderTests.netcoreapp.cs
Outdated
Show resolved
Hide resolved
@@ -38,7 +38,7 @@ public class PollingFileChangeToken : IPollingChangeToken | |||
/// <param name="fileInfo">The <see cref="System.IO.FileInfo"/> to poll</param> | |||
public PollingFileChangeToken(FileInfo fileInfo) | |||
{ | |||
_fileInfo = fileInfo; | |||
_fileInfo = FileSystemInfoHelper.ResolveFileLinkTarget(fileInfo) ?? fileInfo; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for addressing the link target feedback. Please consider adding test cases for the deleted link target cases, as they seem unique to this addition as it has to handle non-existent link targets, and link targets that get deleted / added.
…o debug CI failures and increase delay between writes
Debug.Assert(fileInfo.Exists); | ||
if (fileInfo.LinkTarget != null) | ||
{ | ||
FileSystemInfo targetInfo = fileInfo.ResolveLinkTarget(returnFinalTarget: true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jozkee have you considered keeping all these libs .NET Standard 2.0 and referencing the code that defines ResolveLinkTarget
(and other things if needed)? We are already doing something like this for Microsoft.IO.Redist
which targets net471
. I am just wondering how much effort it would require to keep it NS2.0.
@jeffhandley @ericstj how long do we plan to keep targeting netstandard2.0
and net461
for the Microsoft.Extensions*
libs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jeffhandley Jeff Handley FTE @ericstj Eric St. John FTE how long do we plan to keep targeting netstandard2.0 and net461 for the Microsoft.Extensions* libs?
At the moment we have no plans for dropping netstandard2.0
(and therefor net461
) from the extensions. Perhaps it's something that could be considered in the future if this limitation hinders development considerably. They were intentionally omitted from aspnet/Announcements#324. cc @davidfowl @DamianEdwards @maryamariyan
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we're keeping ns2.0 and net461 until further notice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can do a different (and more efficient) approach to get the target's Last write time, see @ericstj's suggestion #55664 (comment).
But (not) saving a couple of allocations is not a big issue considering that the performance penalty is relatively low compared to other pieces of the code that allocate much more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can do a different (and more efficient) approach to get the target's Last write time
@jozkee I wanted to know whether it would be possible to make this fix work for all TFMs (6 + NS2 + net461), because currently, it's going to work only for .NET 6.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is, but we can defer fixing this on other TFMs if no one is asking for it.
Do you think we can take a dependency on MS.IO.Redist here to utilize the same set of Symbolic Link APIs in order to fix this in ns2.0?
cc @jeffhandley.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think to fix on older TFMs it needs more than just MS.IO.Redist. I believe the implementation of the IO API you are using here (or would add) would depend on the system-native PALs which we don't consider part of the public API. Having a package depend on those is fragile at the least, and might not even work if the versions of those don't have the exports you need on older frameworks. Adding private copies of the system-native PALs (similar to what System.IO.Ports does) is expensive too. Something to consider around OOB'ing our IO API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh, good info @ericstj. So far, we don't have any requests to have this fix apply to previous versions; I'm OK with this being net6.0+ until such requests come in.
src/libraries/Microsoft.Extensions.FileProviders.Physical/tests/PhysicalFileProviderTests.cs
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.FileProviders.Physical/tests/PhysicalFileProviderTests.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thank you for the fix @jozkee!
fwiw, I think we failed to acknowledge the root cause of #36091 is |
After working on fixing this I agree on that, we should definitely give it a try. |
Fixes #36091
Builds on top of #55668
Adding
NetCoreAppCurrent
to Microsoft.Extensions.FileProviders.Physical and to all the projects it depends on, in order to use the Symbolic Link APIs added in .NET 6.cc @ericstj @jeffhandley
The fix consists on avoid using the link's
LastWriteTimeUtc
with the one of its target.