Skip to content

Commit cbf672f

Browse files
authored
Engine: Ensure node is sealed when process excepts (aiidateam#6549)
Processes that hit a certain exception were not being sealed. This would cause problems when trying to export them, which only allows sealed nodes. The problem occurs when another exception occurs while handling the original exception. An example is when `Process.update_outputs` would raise a `ValueError` because an unstored node had be attached as an output. Since this method is called in `on_entered`, which is called when the process entered a new state, it would be called again when it entered the excepted state. Since the process was already excepted, the rest of the state changes is cut short by `plumpy`. This would cause the process to never go to the final `TERMINATED` state and so the `on_terminated` method would not be called, which is where the process' node is sealed. The solution is to check the current state in `on_entered` and if it is `EXCEPTED` to simply return and no longer perform any updates on the node. This should prevent any other exceptions from being hit and ensure the process transitions properly to the final terminated state. The only update that is still performed is to update the process state on the process' node, otherwise it would not properly be shown as excepted.
1 parent ad1a431 commit cbf672f

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/aiida/engine/processes/process.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,8 +415,19 @@ def on_create(self) -> None:
415415
@override
416416
def on_entered(self, from_state: Optional[plumpy.process_states.State]) -> None:
417417
"""After entering a new state, save a checkpoint and update the latest process state change timestamp."""
418+
from plumpy import ProcessState
419+
418420
from aiida.engine.utils import set_process_state_change_timestamp
419421

422+
super().on_entered(from_state)
423+
424+
if self._state.LABEL is ProcessState.EXCEPTED:
425+
# The process is already excepted so simply update the process state on the node and let the process
426+
# complete the state transition to the terminal state. If another exception is raised during this exception
427+
# handling, the process transitioning is cut short and never makes it to the terminal state.
428+
self.node.set_process_state(self._state.LABEL)
429+
return
430+
420431
# For reasons unknown, it is important to update the outputs first, before doing anything else, otherwise there
421432
# is the risk that certain outputs do not get attached before the process reaches a terminal state. Nevertheless
422433
# we need to guarantee that the process state gets updated even if the ``update_outputs`` call excepts, for
@@ -432,7 +443,6 @@ def on_entered(self, from_state: Optional[plumpy.process_states.State]) -> None:
432443

433444
self._save_checkpoint()
434445
set_process_state_change_timestamp(self.node)
435-
super().on_entered(from_state)
436446

437447
@override
438448
def on_terminated(self) -> None:

tests/engine/test_work_chain.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ def illegal(self):
422422
orm.QueryBuilder().append(orm.ProcessNode, tag='node').order_by({'node': {'id': 'desc'}}).first(flat=True)
423423
)
424424
assert node.is_excepted
425+
assert node.is_sealed
425426
assert 'ValueError: Workflow<IllegalWorkChain> tried returning an unstored `Data` node.' in node.exception
426427

427428
def test_same_input_node(self):

0 commit comments

Comments
 (0)