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

Identify places where file handles are closed and very soon reopened again #64288

Open
Tracked by #64596
adamsitnik opened this issue Jan 25, 2022 · 1 comment
Open
Tracked by #64596
Labels
area-System.IO help wanted [up-for-grabs] Good issue for external contributors
Milestone

Comments

@adamsitnik
Copy link
Member

As #63912 has shown, there are places in BCL where files are being closed and very soon reopened (it's typically not obvious).

using (FileStream fs = new FileStream(destinationFileName, fMode, FileAccess.Write, FileShare.None, bufferSize: 0x1000, useAsync: false))
{
    using (Stream es = source.Open())
        es.CopyTo(fs);
} // the file is closed

File.SetLastWriteTime(destinationFileName, source.LastWriteTime.DateTime); // it's reopened

It causes two kinds of problems:

  • AV and other software can open the file in the meantime, lock the access and hence make the re-opening code fail (ERROR_SHARING_VIOLATION on Windows)
  • the perf is not ideal, as opening the files is expensive (especially on Unix where we perform more than 1 sys-call to open a file).

One of the potential solutions would be to use APIs that are accepting SafeFileHandle like #20234

@adamsitnik adamsitnik added this to the 7.0.0 milestone Jan 25, 2022
@ghost
Copy link

ghost commented Jan 25, 2022

Tagging subscribers to this area: @dotnet/area-system-io
See info in area-owners.md if you want to be subscribed.

Issue Details

As #63912 has shown, there are places in BCL where files are being closed and very soon reopened (it's typically not obvious).

using (FileStream fs = new FileStream(destinationFileName, fMode, FileAccess.Write, FileShare.None, bufferSize: 0x1000, useAsync: false))
{
    using (Stream es = source.Open())
        es.CopyTo(fs);
} // the file is closed

File.SetLastWriteTime(destinationFileName, source.LastWriteTime.DateTime); // it's reopened

It causes two kinds of problems:

  • AV and other software can open the file in the meantime, lock the access and hence make the re-opening code fail (ERROR_SHARING_VIOLATION on Windows)
  • the perf is not ideal, as opening the files is expensive (especially on Unix where we perform more than 1 sys-call to open a file).

One of the potential solutions would be to use APIs that are accepting SafeFileHandle like #20234

Author: adamsitnik
Assignees: -
Labels:

area-System.IO

Milestone: 7.0.0

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged New issue has not been triaged by the area owner label Jan 25, 2022
@adamsitnik adamsitnik removed the untriaged New issue has not been triaged by the area owner label Jan 25, 2022
@danmoseley danmoseley added the help wanted [up-for-grabs] Good issue for external contributors label Jan 26, 2022
@jeffhandley jeffhandley modified the milestones: 7.0.0, Future Jul 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.IO help wanted [up-for-grabs] Good issue for external contributors
Projects
None yet
Development

No branches or pull requests

3 participants