Skip to content

Save: Flush to disk after writing to file #9589

Closed

Description

@garfieldbanks
  • VSCode Version: 1.3.1
  • OS Version: W10

Steps to Reproduce:

I don't have exact steps. It was working fine. I was writing node js scripts to split old family videos into individual clips. I was using the built in terminal. Midway through a script I canceled the process. After I fixed and restarted the script I noticed ffmpeg wasn't running. It was frozen or something so I rebooted my computer. It wouldn't restart so I held the power button until it turned off. On reboot I reopened vscode and I can no longer see javascript files:

The file cannot be displayed in the editor because it is either binary or uses an unsupported text encoding.

Activity

  1. garfieldbanks commented on Jul 21, 2016

    @garfieldbanks
    Author

    Opened file with notepad and it is blank.

    Looks like vscode deleted the contents of my file.

    I was really starting to like vscode. But this sucks.

  2. garfieldbanks commented on Jul 21, 2016

    @garfieldbanks
    Author

    I can open javascript files from old projects but all the *.js files in the folder I had open in vscode are blank and give the "file cannot be displayed..." message.

  3. added
    info-needed
    on Jul 22, 2016
  4. kieferrm commented on Jul 22, 2016

    @kieferrm
    Member

    garfieldbanks I'm sorry you lost your data. That's bad. However, I'm not yet ready to accept the blame... All your files where saved when you rebooted your box? Did you have autosave enabled or not?

  5. kieferrm commented on Jul 22, 2016

    @kieferrm
    Member

    See also my comment in #9552. The issue describes a similar experience also on W10.

  6. garfieldbanks commented on Jul 22, 2016

    @garfieldbanks
    Author

    No I don't have autosave on, but I don't think autosave is relevant as this happened to many of the files in the project, most of which were not being edited anywhere near the time of the freeze. There is only one file that I edited before restarting the script and that is the main script itself. But I saved it before restarting the script. So none of my files should have been in an unsaved state.

    I'm sorry that I was a little angry when I made this issue. I've since rewritten the code (it wasn't really all that much). I understand that these things happen and I still like vscode.

    If there's anything else I can do to help figure out the bug, please let me know.

  7. benlowry commented on Jul 22, 2016

    @benlowry

    I don't have auto-save enabled either and lost my pre-existing file when the power cut and I had unsaved changes. Windows 10 Pro, current VSCode release (1.3). The only setting I have altered is using \n as line endings.

  8. added
    important
    freeze-slow-crash-leak
    on Jul 27, 2016
  9. joaomoreno commented on Jul 27, 2016

    @joaomoreno
    Contributor
  10. joaomoreno commented on Jul 27, 2016

    @joaomoreno
    Contributor

    I can easily reproduce this with VS Code. But does it reproduce with another applications...?

    An interesting experience would be to open VS Code in a workspace (which watches over the files within), use Notepad to edit a file inside that workspace and crash after the save operation. The theory is that the file watching would have some factor in this. After a few attemps, I could reproduce it, but in this case instead of the file being completely zero filled, only the newly added content would be zero filled (I typed 7 bytes before the crash):

    image

    Hmmm... I didn't really believe file watching had anything to do with it so I tried reproducing it with Nodepad only. After many attempts, I did it:

    image

    While Notepad still has the issue, it does seem to behave a bit better, by limiting the damage to the newly added bytes. So this lead me to compare what Notepad does versus what Code does.

    Notepad

    notepad

    Code

    code

    Well, both processes seem to write the full file to disk, but Notepad seems to cause two more events (SetEndOfFileInformationFile and SetAllocationInformationFile) to make sure the file is truncated at 1144 bytes... is that really making a difference here? The seem to come from calling SetEndOfFile from KERNELBASE.dll:

    screen shot 2016-07-27 at 12 41 07

    To be continued...

  11. joaomoreno commented on Jul 27, 2016

    @joaomoreno
    Contributor

    Let's boil it down to a snippet:

    const fs = require('fs');
    
    fs.readFile('LICENSE.txt', 'utf8', (err, contents) => {
        if (err) contents = '';
    
        contents += 'hello world\r\n';
    
        const buffer = new Buffer(contents, 'utf8');
        fs.writeFile('LICENSE.txt', buffer, err => {
            if (err) return console.error(err);
            console.log('done');
        });
    });

    It wasn't hard to reproduce the issue with this code: after a few attempts, the whole file was filled with zeros.

    So.. let's try to replicate Notepad's behaviour and truncate the file before closing it:

    const fs = require('fs');
    
    fs.readFile('LICENSE.txt', 'utf8', (err, contents) => {
        if (err) contents = '';
    
        contents += 'hello world\r\n';
    
        const buffer = new Buffer(contents, 'utf8');
    
        fs.open('LICENSE.txt', 'w', 0o666, (err, fd) => {
            if (err) return console.error(err);
    
            fs.write(fd, buffer, 0, buffer.length, err => {
                if (err) return console.error(err);
    
                fs.ftruncate(fd, buffer.length, err => {
                    if (err) return console.error(err);
    
                    fs.close(fd, err => {
                        if (err) return console.error(err);
    
                        console.log('done');
                    });
                });
            });
        });
    });

    Here's the process events. It seems that at least the SetEndOfFileInformationFile event is fired. Not sure how to get the SetAllocationInformationFile.

    screen shot 2016-07-27 at 13 02 32

    But that wasn't enough, it seems, as the whole file was zeros, after reproducing the issue:

    image

    I decided to stop here, since I don't think we'll get close to Notepad's behaviour. Also, even if we did the issue would only be improved, not fixed.

    It's possible that the solution would lie on the SetAllocationInformationFile event, which I have no idea how to call from Node. Another theory is that the CreateFile call is slightly different as well, between Notepad and Node. I couldn't figure out a way to make the same call in Node as its done in Notepad.

    image

    Interesting link: http://stackoverflow.com/questions/12228042/what-does-setfilevaliddata-doing-what-is-the-difference-with-setendoffile

  12. joaomoreno commented on Jul 27, 2016

    @joaomoreno
    Contributor

    I will close this issue as there's not much we can do here.

    Mitigation suggestions

    • use an SCM tool, such as git;
    • use a backup system, such as Windows Backup and Restore.

    Closing arguments

    Above all else, note that a crash is an unexpected error which can hardly be always properly handled. Anything can fail (and often will) during a crash, from user-land apps, through the kernel down to actual transistors.

    There are a few ideas of how we can mitigate this such as saving the file to another location and only then overwrite the original file; this way there would be copies of the edited files somewhere in disk. But even that can fail: the FS could batch the backup and save operations together and the machine could crash while applying those operations, thus losing all state...

    It stands to reason that finding and fixing the source of the crash is the only sane fix; in this case this means charging your battery more often or simply not crash the machine on purpose.

  13. 14 remaining items

  14. emragins commented on Jul 27, 2016

    @emragins

    Awesome. Thanks! :)

  15. benlowry commented on Jul 28, 2016

    @benlowry

    I share all of emragins' concerns, and the additional concern that a bug in Sublime is somehow justification to write this file-destroying issue off, seems far more like "they have to fix it too".

  16. garfieldbanks commented on Jul 28, 2016

    @garfieldbanks
    Author

    I don't think João Moreno (@joaomoreno) is writing it off. I believe his last comment meant that he was going to be enlisting Benjamin Pasero (@bpasero) to help him figure it out and that he expects to have it done sometime in August.

  17. joaomoreno commented on Jul 28, 2016

    @joaomoreno
    Contributor

    Ben (@benlowry) The fact that other applications have the same problem means the issue isn't with the applications, but with the OS, file system, kernel, hardware, or something down below.

    But the fact that Sublime doesn't seem to have it, by using the FlushBuffers operation, means we can at least be as good as them. This doesn't mean the problem won't exist, we're just going to narrow down the time window in which is it possible to occur. A crash is always a crash, and very little can be done about a crash at the right time.

  18. changed the title [-]The file cannot be displayed in the editor because it is either binary or uses an unsupported text encoding[/-] [+]Save: Flush to disk after writing to file[/+] on Aug 8, 2016
  19. bpasero commented on Aug 9, 2016

    @bpasero
    Contributor

    João Moreno (@joaomoreno) I pushed this and would appreciate a verification on Windows with your setup.

  20. added
    verification-needed
    on Aug 9, 2016
  21. removed
    verification-needed
    on Aug 29, 2016
  22. joaomoreno commented on Sep 2, 2016

    @joaomoreno
    Contributor

    This seems to work beautifully.

  23. bpasero commented on Sep 2, 2016

    @bpasero
    Contributor

    João Moreno (@joaomoreno) thanks for verifying <3

  24. locked and limited conversation to collaborators on Nov 18, 2017
  25. added a commit that references this issue on Sep 6, 2026
    51c16fa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugfreeze-slow-crash-leakimportantverified

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions