Skip to content

Deleting an open file is handled inconsistently  #756

Open

Description

@drorlevywork

Describe the bug
When performing a File.OpenWrite(), followed by File.Delete() the current implementation executes this without giving any indication anything is wrong.
even if attampting to perform a filestream.Flush() operation on the file that is deleted.

To Reproduce
Steps to reproduce the behavior:

fileSystem = new MockFileSystem();
var openfile = fileSystem.File.OpenWrite("somefile.txt");
openfile.Write(new byte[] { 0  }, 0, 1);
openfile.Flush();
fileSystem.File.Delete("somefile.txt");
openfile.Write(new byte[] { 0  }, 0, 1);
openfile.Flush();

Expected behavior
From quick investigation, using the normal filesystem the results of the above would be different between windows and linux, but in any case will not match with the result that occurs here, which is that it all executes with no issue.
In windows the fileSystem.File.Delete("somefile.txt"); call would fail with an exception noting that the file cannot be deleted as it is open.
meanwhile on Linux it would appear to fail on the final openfile.Flush(); call, noting that there is no longer any stream.
The outcome of the operation should match one of these, but ideally should support both.

Activity

  1. added
    state: needs discussion
    type: bug
    on Sep 25, 2021
  2. added
    state: ready to pick
    and removed
    state: needs discussion
    on Oct 29, 2021
  3. fgreinacher commented on Oct 29, 2021

    @fgreinacher
    Contributor

    Sorry for the very late response and thanks for reporting!

    While it's not our goal to mirror real file system behavior 100% I agree that your code snippet should not succeed.

    TIL that on Linux files can be deleted while they are opened (https://stackoverflow.com/q/16712638).

    I'd try to mirror this behavior in the mock file system, or if that's hard, make it fail during the delete operation on all OSs.

  4. added
    area: testinghelpers
    on Oct 29, 2021
  5. CaringDev commented on Nov 24, 2026

    @CaringDev
    Contributor

    @vbreuss Can this be closed, given your fix?
    If not, what is still missing?

  6. vbreuss commented on Nov 24, 2026

    @vbreuss
    Member

    @CaringDev : It fixes this issue in Testably.Abstractions, not here in System.IO.Abstractions (for more background on these two libraries, see here).

  7. CaringDev commented on Nov 24, 2026

    @CaringDev
    Contributor

    Related: #894

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: testinghelpersstate: ready to picktype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions