Skip to content
This repository was archived by the owner on Mar 3, 2023. It is now read-only.

Atom erasing the content of files when there is not enough disk space #3158

Closed

Description

@xaviervia

I have a hard time replicating this issue since it will require me filling up the disk again, but in a nutshell the problem is that when I edit a file in Atom, if the disk space is not enough to save it, I find that the file content is erased.

This happened to me more than once (yeah I need more Gbs of disk space) so I can certify that it is Atom the one causing the issue. The issue appears whenever I edit the file, not when I just open it. The problem is not that Atom is not being able to save the file (that would make sense if there is no disk space available), the problem is that Atom removes all the content, and of course I'm afterwards not able to recover it.

I have no clue of why may this be happening

Activity

  1. lee-dohm commented on Aug 2, 2014

    @lee-dohm
    Contributor

    It sounds like when Atom saves the file that it just saves directly on top of the original, so perhaps the timeline is something like this:

    1. Atom opens the file for writing, but not append ... thereby truncating the file to zero
    2. Atom attempts to write the file to disk, but there isn't enough disk space so it fails

    This leaves you in a state with the file erased. I understand that some editors write the new contents to a temporary file and then just move the temp file on top of the original file, clobbering it. This way if the write of the temp file fails, no harm no foul.

  2. xaviervia commented on Aug 2, 2014

    @xaviervia
    Author

    Makes sense. Thanks @lee-dohm .

    The behavior seems pretty unsafe besides my concrete issue, I guess using a temporary file would be better.

  3. batjko commented on Aug 2, 2014

    @batjko
    Contributor
    Atom opens the file for writing, but not append ... thereby truncating the file to zero
    

    I don't get the "thereby" part. How does opening a file ever truncate it?

    Granted I don't know how OSX does this, but certainly on Windows and Linux, opening a file creates a temporary working copy, until you save it down again, as @xaviervia suggests.

    I'm confused.

  4. lee-dohm commented on Aug 2, 2014

    @lee-dohm
    Contributor

    http://msdn.microsoft.com/en-us/library/windows/desktop/aa365430(v=vs.85).aspx

    Right in the first line it states "or deletes a file". Take note of the OF_CREATE flag that can be passed. This is a convention that exists across DOS, Windows, Unix ... Heck, if I remember correctly even CP/M.

    I can find more references, but yes, opening a file can truncate it to zero length.

    On Aug 2, 2014, at 12:18 PM, batjko [email protected] wrote:

    Atom opens the file for writing, but not append ... thereby truncating the file to zero
    I don't get the "thereby" part. How does opening a file ever truncate it?

    Granted I don't know how OSX does this, but certainly on Windows and Linux, opening a file creates a temporary working copy, until you save it down again, as @xaviervia suggests.

    I'm confused.

    —
    Reply to this email directly or view it on GitHub.

  5. xaviervia commented on Aug 2, 2014

    @xaviervia
    Author

    Since it is not every day that I open a file in write mode and write nothing into it, I tested it just now.

    First I ran:

    File.open "lele.txt", "w" do |file|
      file.write "lala"
    end

    Then

    File.open "lele.txt", "w" do |file|
    end

    At least in OS X, the result is that the lele.txt file contents are erased.

    Back to the issue, since the operation being done is sync (atomic as far as the application is concerned) it is clear that the operation fails when some writing is already being attempted. I guess the way to know exactly what it is happening would be to read the Node.js implementation for writeFileSync, but I don't think is necessary since the behavior is still unsafe if the writing is being performed directly on the target file.

    Since @lee-dohm located the line, I may pull request a fix–but I'm afraid I'm not aware of any design decisions in Atom regarding where the temporary files should be located and I'm not sure where to start looking for them.

  6. xaviervia commented on Aug 2, 2014

    @xaviervia
    Author

    The file is not deleted, but it is definitely truncated. OS X is POSIX after all so I guess this applies to Linuxes as well.

  7. batjko commented on Aug 2, 2014

    @batjko
    Contributor

    @lee-dohm I suppose my confusion comes from never ever having observed that behavior in any Windows application, and I've been a Windows guy since v3.11 and DOS before that (my Linux exposure is limited and zero on Mac).

    File contents usually always exist after opening, always readable, just not writable.
    The only cases where a file may lose its contents after opening is if the file data itself gets corrupted for whatever reasons, but even then is it never actually empty.

    So, this may just be a huge whole in my tech education, but it is still very weird.

    Edit:
    The behavior of the "w" operation seems to me like what should explicitly not be used if just opening an existing file.
    Are you saying this is a bug in Atom? Where actually the "a" / append mode should have been used but for some reason it used the "w" mode?

  8. xaviervia commented on Aug 2, 2014

    @xaviervia
    Author
  9. lee-dohm commented on Aug 2, 2014

    @lee-dohm
    Contributor

    The behavior of the "w" operation seems to me like what should explicitly not be used if just opening an existing file. Are you saying this is a bug in Atom? Where actually the "a" / append mode should have been used but for some reason it used the "w" mode?

    Well, this is where it gets complicated. No, I'm not seriously suggesting that the append mode should be used because that puts the file position at the end of the file and you would have to seek to the beginning to overwrite ... and then if the file was being shortened you would have to signal EOF and ... blah blah blah. Seriously, opening the file for truncate and write and just blasting the new contents into the file is 99% of the time exactly the right thing to do. Except when it isn't 😆 It just so happens that low or zero disk space (on a Unix machine ... which is notoriously intolerant for being low on disk space) is one of those times.

    To be honest, I don't know where to truly point the finger and say "there's the bug" (whether it's the file system or the OS or what) ... I just know that I've seen Unix editors have the kind of workaround I mentioned above specifically for this kind of issue. It has seemed to be less of an issue on Windows ... but then again, I don't make a habit of running out of disk space on any machine, so I don't know.

    So ... now that we've hashed this thing to death 😀

    @xaviervia Sorry for getting side tracked ... yeah, I'm not sure I'm completely cognizant of the issues either. @kevinsawicki, @zcbenz ... do you have suggestions on how you would like this handled?

  10. batjko commented on Aug 2, 2014

    @batjko
    Contributor

    Ok.... now it makes sense to me too.
    We may move on.

  11. lee-dohm commented on Aug 2, 2014

    @lee-dohm
    Contributor

    @batjko Thanks 😀

    And also this might get handled as part of the large file support that is planned eventually.

  12. victorhooi commented on Dec 2, 2014

    @victorhooi

    I got hit by this issue as well =(. Lost a file that I'd been working with over a couple of days.

    It's good to read about the background, learnt something new =).

    I've noticed that vim seems particularly robust with handling basically anything you throw at it - basically, I've never lost work with vim, it's always somewhere. I guess that gels with what was said in guard/guard#98 (comment).

    Would we be looking at adding the temp-file workaround that the other editors have into Atom at some point? Or did you mean the temp-file workaround would be added as part of the large file support (#307)?

  13. 18 remaining items

  14. xaviervia commented on Jul 23, 2015

    @xaviervia
    Author

    Good point. If it messed with watchers and whatnot, I would rather not have atomic saves.

    Saving a backup on the other hand sounds like a good idea.

  15. bronson commented on Jul 23, 2015

    @bronson

    Interesting! Good discussion on stack overflow and the sublime forums (overview, encoding, and breaking links).

    As for your first suggestion, checking for room before writing is racy and notoriously incorrect. For example, if you're out of inodes, you'll see 20 GB free but get ENOSPC when you try to write. And forget getting any meaningful number from a network share! I'm not sure it buys anything.

    Your second suggestion, writing a backup file before writing to the original file, definitely seems prudent. I don't like the idea of needing to enable safety, though. Seems like Atom should be safe by default, no?

    If the user experience is an issue, what about putting a 500ms timeout on writing the backup file? If it expires, put up an alert saying, "Writing the backup file is taking longer than expected. You can disable them in the Settings Panel"? But, given Atom's filesize limitations, even network-attached users will probably rarely see it.

    Remember to fsync before writing the main file... Otherwise the user could end up with an empty backup file and an empty main file.

    Overall, yes, I agree with your direction.

  16. nathansobo commented on Jul 28, 2015

    @nathansobo
    Contributor

    If anyone would like to take a look at atom/text-buffer#87, I'd appreciate it. I'm going to start with this as an option that's off by default so we can have some folks test it out, then I'll flip the default. Let me know if there are oversights in the current implementation.

  17. bronson commented on Jul 28, 2015

    @bronson

    (ooops, meant to leave this on atom/text-buffer#87)

    I left some pseudocode to fsync the files in an inline comment. Tell me if you want me to expand that into a clean PR. Should be pretty straightforward.

    Syncing the data is required on posix and windows, syncing the directory is only needed on posix. I assume fsyncing a dir is a no-op on windows but I've never tried.

  18. phord commented on Sep 21, 2015

    @phord
    Contributor

    More scary details on the things that can go wrong with atomic-saves (even though they are the right way to do it) and ways to get around them, here: http://stupidpythonideas.blogspot.com/2014/07/getting-atomic-writes-right.html

  19. Zireael07 commented on Jan 29, 2016

    @Zireael07

    As pointed out in #10609, Atom should check if the backup is not empty.

    Also, what are the advantages of atomic saves if so much can go wrong with them?

  20. jiangyy commented on Jan 29, 2016

    @jiangyy

    @Zireael07 Atomic saves work perfectly on POSIX environments. Cross-platform seems to bring much trouble.

    Still, I am considering overwriting a file without backup is not to be a very good choice (though problems occur only in very rare cases). This problem not only happens when the disk is full (disk full is one extreme case), it may lose the entire file at system crash, even if there are plenty of spaces, just because the file is opened with O_TRUNC. File contents are truncated, then, system crashes, and then, everything is lost.

    I am not a Node developer, just found such issues by an automated tool (I am even not expecting to find such issue in Atom!). Is there any more reliable library that can safely handle this situation?

  21. eriklarson33 commented on Jan 15, 2018

    @eriklarson33

    This issue doesn't appear to be closed despite it being labeled as such. I'm fairly new to programming but I've used numerous editors and 3 times now over the past 6 months while using Atom I've had it completely delete all contents in my file. This always happens after I click SaveAll. Until, this issue is actually fixed I'm considering not using Atom. I like the editability of Atom, but it's not worth the headache or insecurity of possibly losing all of my work even with using Github and duplicating my saves for backup. Using an editor shouldn't involve risking your entire project. This problem is specific to Atom and how it saves files.

  22. pkhateri commented on Feb 7, 2018

    @pkhateri

    This problem happened to me last night while I was using atom. Although I save the file every few second the content of the file disappeared after no disk space alarm. I lost the result of half day of my work. Lucky that I had the old version in git.. won't use atom any more unless this issue is solved.

  23. jemilsson commented on Jun 15, 2018

    @jemilsson

    This just happened to me. Lost a couple of hours work on a new project. This is really unfortunate behavior.

  24. yazdavar commented on Jul 11, 2018

    @yazdavar

    This problem just happened to me. So frustrated by this bug. Lost my 3 days efforts.

  25. hjohns12 commented on Aug 6, 2018

    @hjohns12

    this issue happened to me just now. would love to see it fixed

  26. lock commented on Feb 2, 2019

    @lock

    This issue has been automatically locked since there has not been any recent activity after it was closed. If you can still reproduce this issue in Safe Mode then please open a new issue and fill out the entire issue template to ensure that we have enough information to address your issue. Thanks!

  27. locked as resolved and limited conversation to collaborators on Feb 2, 2019
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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions