You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
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
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.
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:
Atom opens the file for writing, but not append ... thereby truncating the file to zero
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.
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.
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.
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.
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.
@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?
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?
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)?
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.
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.
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.
@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?
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.
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.
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!
Activity
lee-dohm commentedon Aug 2, 2014
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:
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.
lee-dohm commentedon Aug 2, 2014
Editor.save():https://github.com/atom/atom/blob/master/src/editor.coffee#L429
Which leads to
TextBuffer.save():https://github.com/atom/text-buffer/blob/master/src/text-buffer.coffee#L687-L703
Which leads to
File.write()innode-pathwatcher:https://github.com/atom/node-pathwatcher/blob/master/src/file.coffee#L80-L85
Which leads to
File.writeFileWithPrivilegeEscalationSync():https://github.com/atom/node-pathwatcher/blob/master/src/file.coffee#L139-L153
And here it appears that it is a simple,
file.writeSync(), which would appear to bear out my theory.xaviervia commentedon Aug 2, 2014
Makes sense. Thanks @lee-dohm .
The behavior seems pretty unsafe besides my concrete issue, I guess using a temporary file would be better.
batjko commentedon Aug 2, 2014
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.
lee-dohm commentedon Aug 2, 2014
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_CREATEflag 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.
xaviervia commentedon Aug 2, 2014
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:
Then
At least in OS X, the result is that the
lele.txtfile 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.
xaviervia commentedon Aug 2, 2014
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.
lee-dohm commentedon Aug 2, 2014
Another reference: http://www.cplusplus.com/reference/cstdio/fopen/
See the behavior of "w".
batjko commentedon Aug 2, 2014
@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?
xaviervia commentedon Aug 2, 2014
At that:
fs.writeFileSynchttps://github.com/joyent/node/blob/master/lib/fs.js#L980
to
fs.openSynchttps://github.com/joyent/node/blob/master/lib/fs.js#L443
to
fs.open(viabinding.open)https://github.com/joyent/node/blob/master/lib/fs.js#L432
...and then I do not really understand what's going on, at least at a glance.
The important thing is that on line 991 the flag is set to
w, so @lee-dohm's cplusplus link applies.lee-dohm commentedon Aug 2, 2014
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?
batjko commentedon Aug 2, 2014
Ok.... now it makes sense to me too.
We may move on.
lee-dohm commentedon Aug 2, 2014
@batjko Thanks 😀
And also this might get handled as part of the large file support that is planned eventually.
victorhooi commentedon Dec 2, 2014
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
vimseems particularly robust with handling basically anything you throw at it - basically, I've never lost work withvim, 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)?
18 remaining items
xaviervia commentedon Jul 23, 2015
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.
bronson commentedon Jul 23, 2015
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.
nathansobo commentedon Jul 28, 2015
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.
bronson commentedon Jul 28, 2015
(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.
phord commentedon Sep 21, 2015
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
Zireael07 commentedon Jan 29, 2016
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?
jiangyy commentedon Jan 29, 2016
@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?
eriklarson33 commentedon Jan 15, 2018
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.
pkhateri commentedon Feb 7, 2018
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.
jemilsson commentedon Jun 15, 2018
This just happened to me. Lost a couple of hours work on a new project. This is really unfortunate behavior.
yazdavar commentedon Jul 11, 2018
This problem just happened to me. So frustrated by this bug. Lost my 3 days efforts.
hjohns12 commentedon Aug 6, 2018
this issue happened to me just now. would love to see it fixed
lock commentedon Feb 2, 2019
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!