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 }}
Repository navigation
Save UI state periodically to prevent loss on shutdown #12058
Let Windows install updates and automatically restart the computer
After logging into Windows and launching VS Code, VS Code opens the project that was opened last time, but it doesn't restore editors to the state they were in last time. It restores some much older state instead (probably from last time the "Close folder" menu option was used). Very annoying.
Reacted by MrChriZ, Meai1, teneon, Lajos Rodek, aLexa, Zach Bloomquist, Marco Bonardo, Anton Gilgur, Horatio Caine, Beck and 30 more
Jan Kalfus (@jankalfus) this indicates that Windows is not shutting down VS Code gracefully but rather terminates it. We would need to periodically save UI state to prevent that.
changed the title [-]Editor states are forgotten after Windows restarts due to updates installation[/-][+]UI state lost when VS Code is terminated ungracefully[/+]on Sep 15, 2016
I have also noticed that when using F12 (Go to Definition), VS Code can be confused with certain files (probably those that weren't existing in the restored state, but were created later).
The issue is the following (BTW this relates to TypeScript .tsx and .ts files, I haven't tested any others):
When using F12 with a symbol, VS Code opens a new editor tab with an identical file opened and sets the cursor at the import, instead of going to the symbol import in the currenty opened editor tab. Expected behavior is to just set the cursor in the currently opened tab.
I'm not sure that periodically saving the UI state is the only way to achieve this. With other Windows programs, they appear to be able to execute some code when the computer shuts down. For instance, Paint.NET will prompt you to save any unsaved changes, and will even halt the shutdown until you have given a response.
The real fix is for Electron to provide API to detect the system shutdown case on Windows (e.g. what electron/electron#15620 suggests).
These changes will probably not help when VSCode has focus and windows restarts, though in my testing it works when any non-vscode UI is popping up (e.g. some prompt from Windows to apply updates). I think this is the best we can do for now.
(...) for VSCode to save state any time it changes: when opening and closing windows and tabs. This is the most robust solution. Benjamin Pasero (@bpasero): detecting shutdown is brittle, would only fix half of the problem. Think of a power loss or a hard reset case - you can't "detect" those, so we would still lose state. Please, please keep this in mind if you ever come back to reworking this, because if you later change to relying on Electron, you'll revive this issue.
This fix is a tradeoff between having to compute and save the UI state every time it changes (keep in mind: any cursor move is a change in UI state) and protecting against crashes. I many cases we are already storing the UI state when it changes but there are a few exceptions as the example above.
Btw: if Windows decides to just kill VSCode we are at the mercy of how robust SQLite is in those cases (the DB we use for all state). The DB only closes on a proper shutdown, but not on a crash or Windows restart. Having the Electron API might allow us to properly close the DB.
The real fix is for Electron to provide API to detect the system shutdown case on Windows
Benjamin Pasero (@bpasero) Please remember: bug exists on all platforms: Linux and Mac as well, not only Windows.
Btw: if Windows decides to just kill VSCode we are at the mercy of how robust SQLite is in those cases (the DB we use for all state). The DB only closes on a proper shutdown, but not on a crash or Windows restart. Having the Electron API might allow us to properly close the DB.
Though from my testing I think I could never reproduce it on any platform but Windows.
Then close the DB after every writing.
Thanks for the suggestion, but I hope that is not needed, I trust SQLite to be robust specifically in this case because only 1 process is actually writing to the DB at all times.
Aleksey Kontsevich (@akontsevich) and my fix is for all platforms, not just windows. Though from my testing I think I could never reproduce it on any platform but Windows.
On Linux happened many times for me as well in KDE on shutdown for example.
Przemysław Dolata (@Noiredd) yes, I was referring to the state loss on restart while VSCode is running, not a forced kill of the application. I think killing VSCode like that or having a native crash is not the typical scenario we optimize for. The OS restart scenario is more typical to me and should now be fine (pending verification).
There are probably a lot more places in VSCode where a forced process termination leaves state behind. To give an example, all of the extensions are not receiving their deactivate() call which could mean that extension state is not persisted.
I still think that the powerMonitor API from Electron could further improve this, e.g. to gracefully shutdown extensions as well.
Wout Mertens (@wmertens) most of the UI state is persisted when it actually changes but some is not. As I was mentioning earlier, editor state for example changes every time you move the cursor and is not very fine grained currently (it is the entire editor UI state for all editors persisted under 1 key). I can make another pass to see if the layout state can be persisted when it changes.
Activity
bpasero commentedon Sep 15, 2016
Jan Kalfus (@jankalfus) this indicates that Windows is not shutting down VS Code gracefully but rather terminates it. We would need to periodically save UI state to prevent that.
[-]Editor states are forgotten after Windows restarts due to updates installation[/-][+]UI state lost when VS Code is terminated ungracefully[/+]jankalfus commentedon Sep 18, 2016
I have also noticed that when using F12 (Go to Definition), VS Code can be confused with certain files (probably those that weren't existing in the restored state, but were created later).
The issue is the following (BTW this relates to TypeScript .tsx and .ts files, I haven't tested any others):
When using F12 with a symbol, VS Code opens a new editor tab with an identical file opened and sets the cursor at the import, instead of going to the symbol import in the currenty opened editor tab. Expected behavior is to just set the cursor in the currently opened tab.
YoshiWalsh commentedon Jun 14, 2017
I'm not sure that periodically saving the UI state is the only way to achieve this. With other Windows programs, they appear to be able to execute some code when the computer shuts down. For instance, Paint.NET will prompt you to save any unsaved changes, and will even halt the shutdown until you have given a response.
I'm having trouble finding up-to-date documentation about this, but it seems in Vista Windows was changed to allow programs 2 seconds to exit once a shutdown is initiated (https://blogs.msdn.microsoft.com/oldnewthing/20070416-00/?p=27243/), and I can't see any sign that this has been changed since. Would it be possible to use this time to store the UI state?
71 remaining items
bpasero commentedon Dec 17, 2019
Pushed a change (#87158) that will:
The real fix is for Electron to provide API to detect the system shutdown case on Windows (e.g. what electron/electron#15620 suggests).
These changes will probably not help when VSCode has focus and windows restarts, though in my testing it works when any non-vscode UI is popping up (e.g. some prompt from Windows to apply updates). I think this is the best we can do for now.
Noiredd commentedon Dec 17, 2019
Thank you!
(...) for VSCode to save state any time it changes: when opening and closing windows and tabs. This is the most robust solution.
Benjamin Pasero (@bpasero): detecting shutdown is brittle, would only fix half of the problem. Think of a power loss or a hard reset case - you can't "detect" those, so we would still lose state. Please, please keep this in mind if you ever come back to reworking this, because if you later change to relying on Electron, you'll revive this issue.
bpasero commentedon Dec 17, 2019
This fix is a tradeoff between having to compute and save the UI state every time it changes (keep in mind: any cursor move is a change in UI state) and protecting against crashes. I many cases we are already storing the UI state when it changes but there are a few exceptions as the example above.
Btw: if Windows decides to just kill VSCode we are at the mercy of how robust SQLite is in those cases (the DB we use for all state). The DB only closes on a proper shutdown, but not on a crash or Windows restart. Having the Electron API might allow us to properly close the DB.
akontsevich commentedon Dec 17, 2019
Benjamin Pasero (@bpasero) Please remember: bug exists on all platforms: Linux and Mac as well, not only Windows.
Then close the DB after every writing.
bpasero commentedon Dec 17, 2019
Aleksey Kontsevich (@akontsevich) and my fix is for all platforms, not just windows.
Though from my testing I think I could never reproduce it on any platform but Windows.Thanks for the suggestion, but I hope that is not needed, I trust SQLite to be robust specifically in this case because only 1 process is actually writing to the DB at all times.
akontsevich commentedon Dec 17, 2019
On Linux happened many times for me as well in KDE on shutdown for example.
Noiredd commentedon Dec 17, 2019
Here's how to reproduce on Linux. Do
killall -9 codeat step 5.bpasero commentedon Dec 17, 2019
Aleksey Kontsevich (@akontsevich) you are right, I see it on Linux as well
bpasero commentedon Dec 17, 2019
Przemysław Dolata (@Noiredd) yes, I was referring to the state loss on restart while VSCode is running, not a forced kill of the application. I think killing VSCode like that or having a native crash is not the typical scenario we optimize for. The OS restart scenario is more typical to me and should now be fine (pending verification).
There are probably a lot more places in VSCode where a forced process termination leaves state behind. To give an example, all of the extensions are not receiving their
deactivate()call which could mean that extension state is not persisted.I still think that the
powerMonitorAPI from Electron could further improve this, e.g. to gracefully shutdown extensions as well.wmertens commentedon Dec 17, 2019
bpasero commentedon Dec 18, 2019
Wout Mertens (@wmertens) most of the UI state is persisted when it actually changes but some is not. As I was mentioning earlier, editor state for example changes every time you move the cursor and is not very fine grained currently (it is the entire editor UI state for all editors persisted under 1 key). I can make another pass to see if the layout state can be persisted when it changes.