Skip to content

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

Open

Description

@adamsitnik

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

Activity

  1. added this to the 7.0.0 milestone on Jan 25, 2022
  2. ghost added
    untriaged
    on Jan 25, 2022
  3. removed
    untriaged
    on Jan 25, 2022
  4. added
    help wanted
    on Jan 26, 2022
  5. modified the milestones: 7.0.0, Future on Jul 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.IOhelp wanted

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions