# Buggy code
def append_graph(self, graph_key, op_info):
    super().append_graph(graph_key, op_info)

    if not self._is_terminal:
        self._is_terminal = op_info.get("is_terminal")

    if self.state not in OperandState.TERMINATED_STATES:
        for in_key in self._pred_keys:
            self._get_operand_actor(in_key).remove_finished_successor(
                self._op_key, _tell=True, _wait=False
            )
        self.start_operand()
    elif self.state in OperandState.STORED_STATES:
        for out_key in self._succ_keys:
            self._get_operand_actor(out_key).add_finished_predecessor(
                self._op_key,
                self.worker,
                output_sizes=self._data_sizes,
                output_shapes=self._data_shapes,
                _tell=True,
                _wait=False,
            )
        # require more chunks to execute if the completion caused no successors to run
        if self._is_terminal:
            # update records in GraphActor to help decide if the whole graph finished execution
            self._add_finished_terminal()


# Traceback (KeyError)
2020-12-02 11:19:40,309 mars.scheduler.operands.common 87 ERROR    Attempt 1: Unexpected error KeyError occurred in executing operand 5c7a3b06d448300987640036d2f5a34e in 11.238.145.234:49708
Traceback (most recent call last):
File "/home/admin/work/_public-mars-0.5.5.zip/mars/promise.py", line 378, in _wrapped
return func(*args, **kwargs)
File "/home/admin/work/_public-mars-0.5.5.zip/mars/utils.py", line 377, in _wrapped
return func(*args, **kwargs)
File "/home/admin/work/_public-mars-0.5.5.zip/mars/worker/execution.py", line 564, in execute_graph
quota_request = self._prepare_quota_request(session_id, graph_key)
File "/home/admin/work/_public-mars-0.5.5.zip/mars/worker/execution.py", line 249, in _prepare_quota_request
memory_estimations = self._estimate_calc_memory(session_id, graph_key)
File "/home/admin/work/_public-mars-0.5.5.zip/mars/worker/execution.py", line 213, in _estimate_calc_memory
res = executor.execute_graph(graph_record.graph, graph_record.chunk_targets, mock=True)
File "/home/admin/work/_public-mars-0.5.5.zip/mars/executor.py", line 690, in execute_graph
res = graph_execution.execute(retval)
File "/home/admin/work/_public-mars-0.5.5.zip/mars/executor.py", line 574, in execute
return [self._chunk_results[key] for key in self._keys]
File "/home/admin/work/_public-mars-0.5.5.zip/mars/executor.py", line 574, in <listcomp>
return [self._chunk_results[key] for key in self._keys]
KeyError: '3990ec90331559138b6ecbc6d76fbd0d'

# Fixed code
def append_graph(self, graph_key, op_info):
    super().append_graph(graph_key, op_info)

    if not self._is_terminal:
        self._is_terminal = op_info.get("is_terminal")

    if self.state in OperandState.STORED_STATES:
        metas = self.chunk_meta.batch_get_chunk_meta(
            self._session_id, self._io_meta.get("chunks")
        )
        if any(meta is None for meta in metas):
            self.state = OperandState.UNSCHEDULED

    if self.state not in OperandState.TERMINATED_STATES:
        for in_key in self._pred_keys:
            self._get_operand_actor(in_key).remove_finished_successor(
                self._op_key, _tell=True, _wait=False
            )
        self.start_operand()
    elif self.state in OperandState.STORED_STATES:
        for out_key in self._succ_keys:
            self._get_operand_actor(out_key).add_finished_predecessor(
                self._op_key,
                self.worker,
                output_sizes=self._data_sizes,
                output_shapes=self._data_shapes,
                _tell=True,
                _wait=False,
            )
        # require more chunks to execute if the completion caused no successors to run
        if self._is_terminal:
            # update records in GraphActor to help decide if the whole graph finished execution
            self._add_finished_terminal()