BelaPlatform / Bela

Bela: core code, IDE and lots of fun!
Other
491 stars 139 forks source link

After sudden power shutdown, file that was being edited is empty #372

Open giuliomoro opened 6 years ago

giuliomoro commented 6 years ago

Maybe we should keep a backup somewhere*, instead of hot-overwriting the file?

adanlbenito commented 6 years ago

I 100% agree with this.

LBDonovan commented 6 years ago

Ok I'm gonna get on and fix this soon. There are actually two issues here; one is preventing files being corrupted when the power is removed while they are being edited, and the other is keeping regular backups of the projects directory.

For the first I propose the following:

- before writing a file, create a lock file containing the name and contents of the old file
- after writing the file, delete the lock file
- on boot, check if a lock file exists and ask the user if they want to restore that file

I think this will work fine, though it will be fiddly. It may add a significant delay to uploading and running projects, particularly when editing large files. It is also impossible to reproduce this problem, so there's no way to test that its working.

The second problem, keeping regular backups of the projects dir, is tricky. I propose the following:

- rsync whole projects dir to 'hot' backup every 5-10 mins when IDE is running
    - perhaps only backup 'code' files by extension to save disk space / CPU?
- on boot archive the previous hot backup to a 'cold' backup
    - perhaps use compression

The questions are, how many cold backups do we keep, how do we restore them, and how do we let users know that they exist so they can make use of them? Is it actually worth doing this, or is it too much cost for too little reward?

Comments / suggestions welcome.

giuliomoro commented 6 years ago

Note that what happened to @adanlbenito last night did not involve a power shutdown, it was simply the IDE dying and coming back to life*. So in the process, maybe something is screwed up, perhaps when refreshing a page or start editing the file before the IDE has fully come back to life?

Without much thinking, I would say just keep old versions of each file you edit (every so often**), allow the user to retrieve older versions by right-clicking on the file. Also, always keep a lock file for each file being edited. The lock is deleted when the IDE closes the file safely. If the file is there when the IDE comes back, hint the user (in case there is a diff between the latest backup and the current version of the file) that they can access an old version of the file.

*: fixing the suspected systemctl issue, i.e.: unexpected crashing is not going to be enough, we want to have backups in place anyhow **: how often? Maybe every (5 min && diff is larger than 10 lines)?

giuliomoro commented 5 years ago

The corruption on shutdown was addressed in 1c8f7538b2fa1f2833bf5d9c72ddb788b36a22c7 . Still, users can somewhat end up with an empty file after the IDE gets somehow unresponsive.