Skip to content

Commit

Permalink
Fall back to loading default session if autosave fails
Browse files Browse the repository at this point in the history
If loading the autosave session fails you can be left with a browser window
with just your start pages in it, instead of the default session that is
normally loaded on startup (assuming you have `auto_save.session = true`). It
is then quite easy to overwrite your default session if you aren't careful.

This is particularly helpful when an early crash caused an invalid autosave
session to be saved, which we later fail to load. See qutebrowser#4672 (comment)

If the default session then fails to load the user will be back in the current
state of having a single browser window with their start pages open.

After this there will be a `_autosave.yml.bak` file saved to disk. Although
there is nothing to make that obvious to the user.
  • Loading branch information
toofar committed Jan 6, 2024
1 parent 1d6bea7 commit 1917148
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions qutebrowser/misc/sessions.py
Expand Up @@ -723,12 +723,17 @@ def load_default(name):
Args:
name: The name of the session to load, or None to read state file.
"""
if name is None and session_manager.exists('_autosave'):
name = '_autosave'
elif name is None:
try:
name = configfiles.state['general']['session']
except KeyError:
try:
last_session = configfiles.state['general']['session']
except KeyError:
last_session = None

if name is None:
if session_manager.exists('_autosave'):
name = '_autosave'
elif last_session is not None:
name = last_session
else:
# No session given as argument and none in the session file ->
# start without loading a session
return
Expand All @@ -739,6 +744,22 @@ def load_default(name):
message.error("Session {} not found!".format(name))
except SessionError as e:
message.error("Failed to load session {}: {}".format(name, e))

if name == "_autosave":
# TODO: I think if loading autosave fails we are left with an
# invisible window with an invalid tab in it which breaks saving
# sessions going forward.
message.error(
"Attempting to save backup of autosave and trying main session"
)
path = session_manager._get_session_path("_autosave")
try:
shutil.copyfile(path, path + '.bak')
except OSError as err:
message.error(f"backing up autosave failed: {err}")
pass
session_manager.delete_autosave()
return load_default(None)
try:
del configfiles.state['general']['session']
except KeyError:
Expand Down

0 comments on commit 1917148

Please sign in to comment.