vscode icon indicating copy to clipboard operation
vscode copied to clipboard

VSCode never remembers my previous session and doesn't reopen the tabs and project after restart

Open hasen6 opened this issue 1 year ago • 58 comments

  • VS Code Version: 1.71.0
  • OS Version: MacOS Monterey 12.5
  • Computer: Mac M1 Ultra

Steps to Reproduce:

  1. Open VSCode and open at least one project
  2. Restart computer
  3. Upon restart VSCode opens but the previously opened project does not

Annoying problem to solve since it doesn't happen literally every time, more like 60-70% of the time. I realise you have a previous thread with exactly the same title from a few years back but it's locked.

hasen6 avatar Sep 05 '22 10:09 hasen6

Hi @hasen6

Try this setting

{
  "window.restoreFullscreen": true,
  "window.restoreWindows": "all",
  "window.newWindowDimensions": "offset",
  "workbench.experimental.editSessions.enabled": true,
  "workbench.editor.restoreViewState": true,
  "terminal.integrated.enablePersistentSessions": true,
  "terminal.integrated.persistentSessionReviveProcess": "onExit",
}

And let me if this helps as on macOS there has catchs but the issue you describe does not exist for me

dalisoft avatar Sep 05 '22 10:09 dalisoft

I checked my settings and everything was already set except "workbench.experimental.editSessions.enabled"

Why is it I need different settings to what I used to to do the same thing? Also why does it only sometimes not remember?

hasen6 avatar Sep 05 '22 20:09 hasen6

workbench.experimental.editSessions.enabled should not be required to reopen the previous session after restart.

Does it reproduce if you close VSCode and then restart?

bpasero avatar Sep 06 '22 04:09 bpasero

workbench.experimental.editSessions.enabled should not be required to reopen the previous session after restart.

Ok, no I didn't think changing any settings would solve anything unless recent updates had changed something more than normal.

Does it reproduce if you close VSCode and then restart?

No, it only ever happens after a computer restart and it doesn't happen every time. I was reluctant to report it since so often there's no problem, it's already been happening for a week or more. VSCode is closed automatically when I restart my Mac and is supposed to re-open when my Mac restarts. VSCode always opens successfully but sometimes it's just one single blank window. It may have something to do with multiple projects being open since normally I have two open at once? Really not sure what it is.

hasen6 avatar Sep 06 '22 10:09 hasen6

We require the OS to orderly shutdown VSCode to persist state on shutdown. We have added some tricks in some cases to periodically flush state so that it would even survive a crash or unorderly shutdown by the OS and it is possible that the list of opened windows and workspaces is not proper with crashes or unorderly shutdowns:

https://github.com/microsoft/vscode/blob/95f469a0dffa93487d6df2a7f01228b32519d8de/src/vs/platform/windows/electron-main/windowsStateHandler.ts#L140-L144

As such, turning into a feature request as we simply do not behave different here to accommodate.

bpasero avatar Sep 06 '22 13:09 bpasero

We have added some tricks in some cases to periodically flush state so that it would even survive a crash or unorderly shutdown by the OS and it is possible that the list of opened windows and workspaces is not proper with crashes or unorderly shutdowns:

They were not unorderly shutdowns. Everything shut down smoothly and quickly. Also all the other programs opened again and remembered all windows etc, vscode is the only one not remembering anything.

hasen6 avatar Sep 06 '22 14:09 hasen6

They were not unorderly shutdowns

How do you know? Given the fact this is somewhat random, here is my speculation:

  • you restart OS with opened apps
  • OS asks each app to shutdown
  • for whatever reason, VSCode takes 5 seconds to shutdown
  • OS only grants 3 seconds and thus terminates VSCode after that time
  • VSCode did not persist some state because it was terminated

bpasero avatar Sep 06 '22 14:09 bpasero

How do you know?

Because I know what it's like when it has trouble shutting down, it takes abnormally long to do so or some other program cancels the shut down due to it not being able to close.

Given the fact this is somewhat random, here is my speculation:

you restart OS with opened apps OS asks each app to shutdown for whatever reason, VSCode takes 5 seconds to shutdown OS only grants 3 seconds and thus terminates VSCode after that time VSCode did not persist some state because it was terminated

Definitely not, VSCode always closes extremely quickly, it's the fastest to close of all the programs in fact, I've literally never seen it get stuck when it's its turn to shut down. It never even takes a full second. The only kind of programs I've ever seen getting stuck are either ones that ask to save before quitting or something that has to save a lot before it exits, like a windows emulator.

Also if you were right, then it wouldn't be a problem that only started recently.

hasen6 avatar Sep 06 '22 21:09 hasen6

Only because the window closes, does not mean VSCode was able to shutdown orderly. We do a lot of work on the hidden main process to persist states from all DBs and that can take a longer time, this also includes terminating the processes responsible for running extensions.

Next time this happens:

Please see if you can find any logged output that could help us find the source of the issue:

  • run code --verbose from the command line and attach the output
  • in Code select Help | Toggle Developer Tools and select the Console
  • attach any output you see in the Console you can find

Thanks!

bpasero avatar Sep 07 '22 03:09 bpasero

% code --verbose
zsh: command not found: code

hasen6 avatar Sep 07 '22 07:09 hasen6

@bpasero The problem still persists, today it did not remember last opened projects. I've been using VSCode for at least 6 years and am only recently seeing this problem, definitely not a 'feature' unless it's a feature you USED to have...

The code command not working seems to be a separate issue a lot of people have. Using CMD + SFT + P and shell command shows that it is successfully installed for me in path which is the usual way to fix this issue.

hasen6 avatar Sep 10 '22 12:09 hasen6

We use a file /Users/<username>/Library/Application Support/Code/User/globalStorage/storage.json and within a key called windowsState which lists all the windows that were opened at the time the application closed. We use that information to restore the last active window workspace but also other windows if you had more than one open.

If after restart VSCode does not restore your last opened workspace, it could mean that the contents of the file are either corrupt or missing.

Before you start VSCode after a restart, can you check the contents?

bpasero avatar Sep 12 '22 05:09 bpasero

Before you start VSCode after a restart, can you check the contents?

Before I start it? But all my programs automatically open after a restart.

Btw my whole system in vscode is now set to French whereas previously it was in English, not sure if that could possibly be a factor. I noticed the label names in the storage.json file are in French. Some of the labels look really odd in there actually, like:

"Ou&&vrir le dossier..."

and

"Ouvrir les éléments &&récents"

These are the commands to re-open the previous projects, although today the previous projects re-opened just fine.

hasen6 avatar Sep 12 '22 09:09 hasen6

I would not think it makes a difference but you can change VSCode language from the command palette: "Configure Display Language"

But all my programs automatically open after a restart.

Without knowing and understanding what is written to the file, I am not sure how I can help unless we find the condition for me or other users to reproduce. I fear so far no one else has reported this, nor did I encounter this (I am also on macOS).

bpasero avatar Sep 12 '22 12:09 bpasero

Without knowing and understanding what is written to the file

This is what's written to the file, I posted it above:

"Ou&&vrir le dossier..."

Does that look normal to you? It doesn't look nor&&mal (sic) to me. May not be causing the issue but it certainly looks weird af.

I fear so far no one else has reported this,

It would seem they'd have to have it in French language. In my experience a programmer often uses English for programming regardless of what their native language is. Also if you notice in my original post even I stated I was reluctant to report it since it's intermittent. In my experience with github issues I've seen over the years, the vast majority are only reported by one person and if anyone else does have the same problem it's normally a few months or even years later that they add to the thread stating they have the same issue. It's exactly the same on stackoverflow.

nor did I encounter this (I am also on macOS).

Well what does it say in your storage.json? Is it normal or seriously messed up like it is in mine. If you're on MacOS you'll know that everything opens up automatically. The steps to reproduce the error are:

  1. restart while vscode is open
  2. vscode automatically opens upon restart and does not remember the previously opened projects

Obviously you can't change one of these steps.

hasen6 avatar Sep 12 '22 13:09 hasen6

I changed language to French, had 2 windows with workspaces opened and restarted and it restored:

image

bpasero avatar Sep 13 '22 04:09 bpasero

I changed language to French, had 2 windows with workspaces opened and restarted and it restored:

?? Not sure why you're making that statement about an intermittent error. You also seemed to have ignored my statements about all the weird garbage in the storage.json file which since you suspected it may be corrupt would seem to be very relevant. What does your storage.json file look like now it's in French? Also what language do you normally have VSCode in?

hasen6 avatar Sep 13 '22 08:09 hasen6

I do not think the storage.json file is corrupt, but to verify, can you send it over? As long as we can JSON.parse it, the file should be fine to be interpreted on startup.

bpasero avatar Sep 13 '22 08:09 bpasero

I do not think the storage.json file is corrupt, but to verify, can you send it over? As long as we can JSON.parse it, the file should be fine to be interpreted on startup.

Of course it can be parsed but what's the explanation for the weird & signs all over the place? When you set your storage.json file to French, what does it say here:

"id": "workbench.action.files.openFolder",
                        "label": "Ou&&vrir le dossier..."

Is it the same or what?

hasen6 avatar Sep 13 '22 10:09 hasen6

Yes:

image

The && is something special for the main menu to draw "mnemonics": https://en.wikipedia.org/wiki/Mnemonics_(keyboard)

bpasero avatar Sep 13 '22 12:09 bpasero

The && is something special for the main menu to draw "mnemonics":

It doesn't look normal that it's in the middle of the word. I also don't know why you didn't say that before. Well we're no closer to solve the problem, there must be something else happening entirely. Although I do find vscode is generally quite buggy, I often have to quit it and reopen it due to glitches that seem to occur after prolonged use.

hasen6 avatar Sep 14 '22 08:09 hasen6

It doesn't look normal that it's in the middle of the word

We try to have 1 mnemonic of a kind per menu group so that you can quickly access the entry with a single keybinding and not 2. In most cases it should be the capital letter of a word, but sometimes that is not used to prevent conflicts.

bpasero avatar Sep 14 '22 08:09 bpasero

We try to have 1 mnemonic of a kind per menu group so that you can quickly access the entry with a single keybinding and not 2. In most cases it should be the capital letter of a word, but sometimes that is not used to prevent conflicts.

Surely if it's because the storage.json is corrupt on vscode re-opening then it would still be corrupt AFTER failing to open, I'm not sure why you particularly wanted me to check the file before VSCode restarts? It's not like it would be somehow fixed after failing to open. I think the issue must lie somewhere else since the storage.json file seems to be ok, despite appearances.

hasen6 avatar Sep 14 '22 10:09 hasen6

No, we do update the file every time data is written to it, so you would not be able to see that after the window has opened, however we do log errors when reading the file:

https://github.com/microsoft/vscode/blob/a68f9da5fe7019f52f52c4ad3d2e613470e08d68/src/vs/platform/state/node/stateService.ts#L48

So if there is an entry in the "Log (Main)" output channel, that could maybe indicate as such:

image

bpasero avatar Sep 14 '22 11:09 bpasero

No, we do update the file every time data is written to it, so you would not be able to see that after the window has opened, however we do log errors when reading the file:

So if it was corrupt and could not read it correctly it would still be able to write it again no problem? I'm pretty sure there wasn't such an error anyway, but is there any way to check that anywhere else to make sure? Is it saved to any log file anywhere?

hasen6 avatar Sep 14 '22 11:09 hasen6

Yes, it would just rewrite the file with the latest state, dropping anything that was there before. If you run this command, it will show the folder where the main.log file is in:

image

bpasero avatar Sep 14 '22 14:09 bpasero

@bpasero It just failed re-open previous projects today again. This is what's in the main.log file before doing anything else with vscode:

[2022-09-15 12:07:50.670] [main] [info] update#setState idle
[2022-09-15 12:07:53.753] [main] [info] Starting extension host with pid 732 (fork() took 1 ms).
[2022-09-15 12:08:20.677] [main] [info] update#setState checking for updates
[2022-09-15 12:08:21.044] [main] [info] update#setState downloading
[2022-09-15 12:08:31.573] [main] [info] update#setState ready

There's nothing in the log output channel though.

hasen6 avatar Sep 15 '22 10:09 hasen6

Yeah, so this means it is not a corrupt file that is the cause of this issue. I still think its an issue where VSCode fails to write the last opened workspace to disk on shutdown.

To clarify: when this happens, how does VSCode actually open? Does it open with a single empty window or does it restore some session, just not the last one?

bpasero avatar Sep 15 '22 10:09 bpasero

I still think its an issue where VSCode fails to write the last opened workspace to disk on shutdown.

There were three projects open last night when shutting down.

To clarify: when this happens, how does VSCode actually open? Does it open with a single empty window or does it restore some session, just not the last one?

It opens with a single empty window.

hasen6 avatar Sep 15 '22 12:09 hasen6

One other idea would be to trace what gets written to the file on shutdown. Steps:

  • from now on run code --log trace from the command line
  • as soon as this happens after a restart, run the command "Developer: Open Logs Folder"
  • go one level up of that folder which is the logs folder where the logs from all sessions are kept (because we want the session from the shutdown)
  • find the session from shutdown and open main.log
  • find an entry [main] [trace] [WindowsStateHandler] onBeforeShutdown

I just tried this having 2 windows open and triggering a restart and my data is:

{
   "lastActiveWindow":{
      "workspaceIdentifier":{
         "id":"e3cdb405dff44ddaeb3d6c68dd167e33",
         "configURIPath":"file:///Users/bpasero/Development/Microsoft/vscode.code-workspace"
      },
      "backupPath":"/Users/bpasero/Library/Application Support/Code - Insiders/Backups/e3cdb405dff44ddaeb3d6c68dd167e33",
      "uiState":{
         "mode":1,
         "x":0,
         "y":25,
         "width":2560,
         "height":1287
      }
   },
   "lastPluginDevelopmentHostWindow":{
      "uiState":{
         "mode":1,
         "x":0,
         "y":25,
         "width":1680,
         "height":910
      }
   },
   "openedWindows":[
      {
         "workspaceIdentifier":{
            "id":"e3cdb405dff44ddaeb3d6c68dd167e33",
            "configURIPath":"file:///Users/bpasero/Development/Microsoft/vscode.code-workspace"
         },
         "backupPath":"/Users/bpasero/Library/Application Support/Code - Insiders/Backups/e3cdb405dff44ddaeb3d6c68dd167e33",
         "uiState":{
            "mode":1,
            "x":0,
            "y":25,
            "width":2560,
            "height":1287
         }
      },
      {
         "folder":"file:///Users/bpasero/Downloads",
         "backupPath":"/Users/bpasero/Library/Application Support/Code - Insiders/Backups/5e85cd5be9a3c3653c636e7a8c3d4107",
         "uiState":{
            "mode":1,
            "x":919,
            "y":434,
            "width":1641,
            "height":878
         }
      }
   ]
}

Important is to see one entry per window in openedWindows.

bpasero avatar Sep 15 '22 12:09 bpasero