Repository navigation
Description
Discussion of how to implement hot exit for custom editors (#77131)
Background
With hot exit users can quit VS Code with unsaved file changes and know that those files (in their edited state) will be restored when they open VS Code again.
VS Code can properly restore the user's previous file state even in cases such as the following:
- User edits a file and then exits VS Code without saving
- In a different program, the user then edits the same file and saves it to disk
- Then the user reopens VS Code
In that case, VS Code correctly restores the original file with edits. VS Code implements hot exit by continuously persisting edited files to a backup folder, and then checking that backup folder when restoring VS Code.
If there is an error that would prevent VS Code from hot exiting, such as if the backup fails, VS Code will alert the user and require the the non-backed up files be either saved to disk or have their changes lost.
Hot exit + Custom editors
We would like to support hot exit for custom editors as well. From the user's point of view, hot exit of a custom editor should behave the same way it does for normal text editors.
Goals
- Allow extensions to support hot exit for both text and binary based custom editors
- Don't require transferring binary file data from extensions to VS Code
- Make it possible to implement hot exit for larger files
- Support hot exit for custom editors that are not on disk or that are backed by multiple resources
- Fail gracefully (no data loss)
Big questions
- What part of the file state does VS Code back up? What part of the state do extension back up?
- Edits?
- File contents?
- Where is the backup stored?
- Who cleans up old backups?
- How do extensions know that a backup exists and restore the editors from this?
- How does VS Code know that a given editor is backed up? (i.e. we can hot exit)
- When/how are extension notified that they need to back up? For example, if auto save is enabled, we may not need to backup since the file will be saved shortly anyways
How we implement backup also influences the rest of the custom editor API. For example, does it still make sense to have the concept of edits in the API, or could this be delegated to extensions?
Activity
mjbvz commentedon Jan 15, 2020
Review of some offline discussions
Can't VS Code just backup everything?
Yes, so long as edits are json serializable and backing up a custom editors only requires backing up a single file. To implement this, VS Code would simply write the edits + the current disk file to a backup location
However, this falls apart for some interesting cases:
So in practice, not really.
Do we need to tell extensions where to write backup data?
No. In fact, giving extensions a path to write backups is problematic because we then have to tell extensions where to restore their backups from as well
Extensions can use the workspace specific storage folder instead.
Can we reasonably expect extensions to implement hot exit on their own if we provide only documentation or a very small set of api hooks?
No. We can't assume that extensions will properly implement hot exit as there are lots of fun edge cases (such as the file on disk changing between sessions) that they will likely not consider.
Making hot exit an opt-in feature that advanced custom editors can implement may be a good idea.
Do we need any API at all for hot exit?
Strictly speaking, no. Extensions could mostly implement hot exit using the existing custom editor api. However we would likely need some kind of api for extensions to tell us what state backup is in (ongoing, failed, successful)
There are a also some points where minimal api could help reduce code duplication:
What is actually saved during a backup? Do we save off the edits + original file? The file with the edits applied?
Maybe it's best to leave this to extensions as well.
During restore, all VS Code really needs to do is tell an extension to open a custom editor. That extension can then figure out if it has a backup or not and decide how to restore itself.
Some extensions may restore by simply loading the edited file contents, other by loading the original file and then re-applying the edits to it.
Do edits need to be json serializable?
Not if VS Code never needs to write them to disk or restore them across sessions.
We could keep edit objects on the extension host side and just give the VS Code process simple handles to them.
Is there any advantage then to having the concept of edits in the API?
Perhaps.
Edits give VS Code knowledge of the the edit stack. This enable features such as:
reverthookDisadvantages:
bpasero commentedon Jan 16, 2020
On backup & auto save: if auto save is enabled and configured to save after short delay (anything with or below 1s - which is our default) we do not backup at all for content changes. Upon shutdown, we save any dirty editor automatically if not yet saved (e.g. the user closing VSCode before auto save had a chance to kick in).
mjbvz commentedon Jan 16, 2020
A few sketches of what a minimal api could look like:
Optional
prepareForHotExitmethodAdd a new, optional method on the
WebviewCustomEditorEditingDelegateclass that backedOptional event that signals if an editor can hot exit or not
Add a
onHotExitChangeevent that lets extension signal to VS Code that the editor can hot exit. We would leave it up to extension to fire this whenever the editor state changes.mjbvz commentedon Jan 17, 2020
Benjamin Pasero (@bpasero) For web, you mentioned that we have to be able to synchronously check if we can hot exit. In order to avoid data loss, I'm currently thinking that internally VS Code will need to track three hot exit states for each editor:
CanHotExit— We can safely hot exit.CannotHotExit— We cannot hot exit.Pending— We are determining if we can hot exit or not.On desktop, we would delay exiting while all
Pendinghot exit states are resolved. On web, we would assume thatPendingmeans that we cannot hot exit.In the api sketches,
CanHotExitis signaled by returningtrue,CannotHotExitby returningfalseor by not implementingprepareForHotExit, while an unresolved promise would be treated asPendingDo you think that this approach would work for web? Anything important that I am overlooking?
bpasero commentedon Jan 17, 2020
I think the requirements are different depending on the platform:
mjbvz commentedon Jan 17, 2020
My thinking was we would tell extensions to back up the resource after every edit (possibly with a denounce). That means that we don't have to perform extra work on shutdown (besides possibly waiting for any long running backups to complete)
For example, here's how the
prepareForHotExitmethod could work:prepareForHotExit. At this point, VS Code would also consider the resource as not being backed up (it would be in thePendingstate I outlined above)prepareForHotExitmethod is invoked. The extension starts backing up the resource.prepareForHotExitcompletes, VS Code cancels the originalprepareForHotExitcall and invokesprepareForHotExitagainprepareForHotExit, it stores the current hot exit state (CanHotExitorCannotHotExit) until the next edit comes in.On desktop, when quitting VS Code with hot exit enabled we would try to resolve all pending calls to
prepareForHotExit. We would not need to invokeprepareForHotExitat this point (unless we have debounced it). IfprepareForHotExitreturns false for any known resources (or if we hit some timeout), we do not hot exitOn web, when quitting VS Code with hot exit enabled we would first synchronously check all known hot exit states. If there are any pending calls to
prepareForHotExitor any previous calls toprepareForHotExithave returned false, we do not hot exitThe other approach we discussed is having an event that extensions use to tell VS Code that they can hot exit. However we need to be careful to avoid potential data loss here. For example, consider the following scenario
canHotExitEvent(true)event.canHotExitEvent(false)event since it needs to back up the resource againbpasero commentedon Jan 19, 2020
Matt Bierner (@mjbvz) giving some progress of my current work, didn't have time yet to comment on your proposal.
I am thinking to make the working copy service capable of a long running backup into the extension. Here is what I am sketching up:
The reason why I am going away from a
backup():Promise<void>andhasBackup(): booleanmodel is that I really want to make sure theIBackupFileServiceis used in every case. The reason are the following advantages:In other words, making everyone use
IBackupFileServicegives the most consistent experience.Now, I kept
contentinIWorkingCopyBackupstrictly optional because we do not want to move any textual data from extension to workbench. Still, you might want to put somemetainto the backup to figure out how to restore the editor later, but even that is optional.IWorkingCopy#backup()will be called automatically every1safter the content has changed. You are free to communicate this to the extension as appropriate. Here is the flow I have in mind:Backup Flow:
IWorkingCopy#backup()is called after1sIWorkingCopy#backup()callRestore Flow:
As you can see, this model makes backups mandatory and not optional, I think we should try for this goal for the best possible experience
Working Copy Resource Identity
We will have revisit how resources are used for custom editors and their working copies. Since the
IBackupFileServiceis all resource based, we cannot simply use the same file resource for a custom editor that might be used for a normal text editor already - the backups would overwrite each other.Instead, I am thinking this: the custom editor could add some identifier to the file resource to identity the
viewTypeit is associated with from the override logic. This would also allow the custom editor to properly restore the rightviewTypewhen asked to open the resource again. The flow would be:file:///User/bpasero/cat.abc)viewType: myExtensionCustomEditorfile:///User/bpasero/cat.abc?viewType=myExtensionCustomEditor)openEditor({ resource: file:///User/bpasero/cat.abc?viewType=myExtensionCustomEditor}))viewTypequery param to figure out the extension to activateThoughts?
PS: not yet pushed, will do Tuesday (out tomorrow)
mjbvz commentedon Jan 20, 2020
That sounds like an interesting idea. However I wonder if custom editors really need to use VS Code's backup service at all. The only information that a custom editor would backup on the VS Code side would be the file path and its editor type. Both of those can be serialized and restored on the editor input.
I need to think through your proposal more. Let's discuss it further next week!
Also, I have a draft PR that implements
prepareForHotExit: #88948 This PR will help me test how well can extension can actually implement hot exit with a simple API and understand if we've overlooked any major issuesbpasero commentedon Jan 20, 2020
Matt Bierner (@mjbvz) There is one case which is tricky to get right where the backup file service would help you. Imagine this scenario:
code <some other workspace>"electron-mainside that knows that there are backups for a certain workspace)I know the happy path is easy to get right by relying on the editors being restored from the UI state, but there are cases where this will not work. This is why I think having everyone use the same service is a good idea.
bpasero commentedon Jan 21, 2020
I have pushed
IWorkingCopy#backup()as described. We can discuss if it can be optional or not, currently it is mandatory.bpasero commentedon Jan 21, 2020
Matt Bierner (@mjbvz) I have read through the things in this issue and here is some feedback
I would actually add here that every custom editor implementation needs to support hot exit to begin with. Imho it only asks for frustration if users cannot quit VSCode with dirty custom editors and us being on the mercy of the extension to implement this. We are designing a new API here that is expert level, so we can define the rules.
Agreement. Backup should happen in the extension. The only thing the workbench is concerned with is to know if a backup was made and to restore backups. For that,
IBackupFileServiceis still very useful.I think this is the best API to have and it would not require any other API nor any tracking of backup state in the workbench at all due to the way backup are made on shutdown:
web: workbench asks the backup file service if every dirty file has an associated backup and if not blocks the closingdesktop: workbench will trigger a backup for each dirty working copy and wait until that has finished and then ask again if all backups have made. if not the user can choose to still close or cancel.A backup end-to-end would then simply be:
1safter this event only if auto save is offIWorkingCopy#backup()is calledextension#prepareForHotExitIWorkingCopy#backup()mjbvz commentedon Feb 24, 2020
Closing this for now as we've shipped this:
TextDocumentsbackupmethod. This method is no longer optionalbpasero commentedon Feb 25, 2020
Matt Bierner (@mjbvz) to clarify, the API proposal is to require backups from all extensions or is it optional?
mjbvz commentedon Feb 25, 2020
We now require that extensions implement the
backupmethod. However there is nothing to stop an extension from not actually backing anything up in itsbackupmethod