Skip to content

Commit

Permalink
server.py: Fail gracefully when serializing/deserializing a tree.
Browse files Browse the repository at this point in the history
Currently, t.explore() will dump the tree to /tmp/*tid*.pickle, and use it
if to "undo" an ultrametric visualization (by reloading the original tree
from disk). Sometimes the trees cannot be pickled, and this was not considered.

Now at least it fails gracefully. But I think we would be better by not dumping
the tree at all, and not allowing the visualization of trees to modify the
tree itself and pretend that it is not. Editing the tree should be explicit.
  • Loading branch information
jordibc committed Mar 25, 2024
1 parent 5b5a54f commit 08d45f9
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions ete4/smartview/gui/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,11 +732,20 @@ def retrieve_tree_data(tid):
It retrieves all that from a previously saved pickle file in /tmp."""
# Called when tree has been deleted from memory.
tree_data = pickle.load(open(f'/tmp/{tid}.pickle', 'rb'))
try:
tree_data = pickle.load(open(f'/tmp/{tid}.pickle', 'rb'))
except (FileNotFoundError, EOFError, pickle.UnpicklingError) as e:
print(f'Tree {tid} cannot be recovered from disk. Loading placeholder.')
tree_data = TreeData()
tree_data.name = 'Placeholder tree'
tree_data.tree = Tree('(could,not,load,tree);')
ops.update_sizes_all(tree_data.tree)

tree_data.style = copy_style(TreeStyle())
tree_data.layouts = retrieve_layouts(tree_data.layouts)
tree_data.active = drawer_module.get_empty_active()
tree_data.timer = time() # to track if it is active

return tree_data


Expand Down Expand Up @@ -1445,7 +1454,12 @@ def write_tree_data():
data.style = None # since it can't be pickled
data.layouts = layouts # same
data.active = None # same
pickle.dump(data, open(f'/tmp/{tid}.pickle', 'wb'))
try:
pickle.dump(data, open(f'/tmp/{tid}.pickle', 'wb'))
except (pickle.PicklingError, PermissionError) as e:
print(f'Tree {tid} not saved to file.')
# So changing to ultrametric and back will not work,
# because it is done by re-reading from the dumped file.
thr_write = Thread(daemon=True, target=write_tree_data) # so we are not delayed
thr_write.start() # by big trees

Expand Down

0 comments on commit 08d45f9

Please sign in to comment.