# Buggy code
def __init__(
    self,
    session_id,
    graph_key,
    serialized_tensor_graph,
    target_tensors=None,
    serialized_chunk_graph=None,
    state=GraphState.UNSCHEDULED,
    final_state=None,
):
    super(GraphActor, self).__init__()
    self._graph_key = graph_key
    self._session_id = session_id
    self._serialized_tensor_graph = serialized_tensor_graph
    self._serialized_chunk_graph = serialized_chunk_graph
    self._state = state
    self._final_state = final_state

    self._start_time = None
    self._end_time = None
    self._nodes_num = None

    self._cluster_info_ref = None
    self._assigner_actor_ref = None
    self._resource_actor_ref = None
    self._kv_store_ref = None
    self._chunk_meta_ref = None
    self._graph_meta_ref = None

    self._tensor_graph_cache = None
    self._chunk_graph_cache = None

    self._op_key_to_chunk = defaultdict(list)

    self._resource_actor = None
    self._tensor_to_tiled = defaultdict(list)
    self._terminal_chunk_op_tensor = defaultdict(set)
    self._terminated_tensors = set()
    self._operand_infos = dict()
    if target_tensors:
        self._target_tensor_chunk_ops = dict((k, set()) for k in target_tensors)
        self._target_tensor_finished = dict(
            (k, set()) for k in self._target_tensor_chunk_ops
        )
    else:
        self._target_tensor_chunk_ops = dict()
        self._target_tensor_finished = dict()


# Traceback (IndexError)
Traceback (most recent call last):
File "src/gevent/greenlet.py", line 716, in gevent._greenlet.Greenlet.run
File "mars/actors/pool/gevent_pool.pyx", line 88, in mars.actors.pool.gevent_pool.ActorExecutionContext.fire_run
File "mars/actors/pool/gevent_pool.pyx", line 91, in mars.actors.pool.gevent_pool.ActorExecutionContext.fire_run
File "mars/actors/pool/gevent_pool.pyx", line 102, in mars.actors.pool.gevent_pool.ActorExecutionContext.fire_run
File "mars/actors/pool/gevent_pool.pyx", line 96, in mars.actors.pool.gevent_pool.ActorExecutionContext.fire_run
File "mars/actors/core.pyx", line 108, in mars.actors.core.FunctionActor.on_receive
File "mars/actors/core.pyx", line 110, in mars.actors.core.FunctionActor.on_receive
File "/Users/wenjun.swj/Code/mars/mars/scheduler/graph.py", line 421, in prepare_graph
for n in tensor_to_tiled[tk][-1].chunks:
IndexError: list index out of range

# Fixed code
def __init__(
    self,
    session_id,
    graph_key,
    serialized_tensor_graph,
    target_tensors=None,
    serialized_chunk_graph=None,
    state=GraphState.UNSCHEDULED,
    final_state=None,
):
    super(GraphActor, self).__init__()
    self._graph_key = graph_key
    self._session_id = session_id
    self._serialized_tensor_graph = serialized_tensor_graph
    self._serialized_chunk_graph = serialized_chunk_graph
    self._state = state
    self._final_state = final_state

    self._start_time = None
    self._end_time = None
    self._nodes_num = None

    self._cluster_info_ref = None
    self._assigner_actor_ref = None
    self._resource_actor_ref = None
    self._kv_store_ref = None
    self._chunk_meta_ref = None
    self._graph_meta_ref = None

    self._tensor_graph_cache = None
    self._chunk_graph_cache = None

    self._op_key_to_chunk = defaultdict(list)

    self._resource_actor = None
    self._tensor_key_opid_to_tiled = defaultdict(list)
    self._tensor_key_to_opid = dict()
    self._terminal_chunk_op_tensor = defaultdict(set)
    self._terminated_tensors = set()
    self._operand_infos = dict()
    if target_tensors:
        self._target_tensor_chunk_ops = dict((k, set()) for k in target_tensors)
        self._target_tensor_finished = dict(
            (k, set()) for k in self._target_tensor_chunk_ops
        )
    else:
        self._target_tensor_chunk_ops = dict()
        self._target_tensor_finished = dict()