# Buggy code
def build_graph(
    self, graph=None, cls=DAG, tiled=False, compose=True, executed_keys=None
):
    from .tensor.expressions.utils import convert_to_fetch

    executed_keys = executed_keys or []
    if tiled and self.is_coarse():
        self.tiles()

    graph = graph if graph is not None else cls()
    keys = None

    if tiled:
        nodes = list(c.data for c in self.chunks)
        keys = list(c.key for c in self.chunks)
    else:
        nodes = list(self.op.outputs)
    visited = set()
    while len(nodes) > 0:
        node = nodes.pop()

        # replace executed tensor/chunk by tensor/chunk with fetch op
        if node.key in executed_keys:
            node = convert_to_fetch(node).data

        visited.add(node)
        if not graph.contains(node):
            graph.add_node(node)
        children = node.inputs or []
        for c in children:
            if c.key in executed_keys:
                continue
            if not graph.contains(c):
                graph.add_node(c)
            if not graph.has_successor(c, node):
                graph.add_edge(c, node)
        nodes.extend(
            [
                c
                for c in itertools.chain(*[inp.op.outputs for inp in node.inputs or []])
                if c not in visited
            ]
        )
    if tiled and compose:
        graph.compose(keys=keys)
    return graph


# Traceback (IndexError)
In [1]: from mars.deploy.local import new_cluster

In [2]: cluster = new_cluster(scheduler_n_process=2, worker_n_process=3, shared_memory='100M')
/Users/travis/build/wesm/crossbow/arrow/cpp/src/plasma/store.cc:971: Allowing the Plasma store to use up to 0.104858GB of memory.
/Users/travis/build/wesm/crossbow/arrow/cpp/src/plasma/store.cc:1001: Starting object store with directory /tmp and huge page support disabled

In [3]: import mars.tensor as mt

In [4]: a = mt.random.rand(10, 10, chunk_size=3)

In [5]: a.tiles()
Out[5]: Tensor <op=TensorRand, shape=(10, 10), key=2d1101e0bd65a1ca3134ca872c6072b5>

In [6]: cluster.session.run(a)
Unexpected exception occurred in GraphActor.build_tensor_merge_graph. tensor_key='2d1101e0bd65a1ca3134ca872c6072b5'
Traceback (most recent call last):
File "/Users/hekaisheng/Documents/mars_dev/mars/mars/utils.py", line 298, in _wrapped
return func(*args, **kwargs)
File "/Users/hekaisheng/Documents/mars_dev/mars/mars/scheduler/graph.py", line 819, in build_tensor_merge_graph
tiled_tensor = self._get_tensor_by_key(tensor_key)
File "/Users/hekaisheng/Documents/mars_dev/mars/mars/scheduler/graph.py", line 804, in _get_tensor_by_key
return self._tensor_key_opid_to_tiled[(key, tid)][-1]
IndexError: list index out of range
Traceback (most recent call last):
File "src/gevent/greenlet.py", line 766, 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/hekaisheng/Documents/mars_dev/mars/mars/utils.py", line 298, in _wrapped
return func(*args, **kwargs)
File "/Users/hekaisheng/Documents/mars_dev/mars/mars/scheduler/graph.py", line 819, in build_tensor_merge_graph
tiled_tensor = self._get_tensor_by_key(tensor_key)
File "/Users/hekaisheng/Documents/mars_dev/mars/mars/scheduler/graph.py", line 804, in _get_tensor_by_key
return self._tensor_key_opid_to_tiled[(key, tid)][-1]
IndexError: list index out of range
2019-03-18T03:29:21Z <Greenlet at 0xc17966268: <built-in method fire_run of mars.actors.pool.gevent_pool.ActorExecutionContext object at 0xc1796f818>> failed with IndexError

Traceback (most recent call last):
File "src/gevent/greenlet.py", line 766, 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/hekaisheng/Documents/mars_dev/mars/mars/scheduler/graph.py", line 66, in fetch_tensor
fetch_graph = deserialize_graph(graph_actor.build_tensor_merge_graph(tensor_key))
File "mars/actors/core.pyx", line 64, in mars.actors.core.ActorRef.__getattr__._mt_call
File "mars/actors/core.pyx", line 36, in mars.actors.core.ActorRef.send
File "mars/actors/pool/gevent_pool.pyx", line 179, in mars.actors.pool.gevent_pool.ActorContext.send
File "mars/actors/pool/gevent_pool.pyx", line 754, in mars.actors.pool.gevent_pool.Communicator.send
File "mars/actors/pool/gevent_pool.pyx", line 755, in mars.actors.pool.gevent_pool.Communicator.send
File "mars/actors/pool/gevent_pool.pyx", line 750, in mars.actors.pool.gevent_pool.Communicator._send
File "mars/actors/pool/gevent_pool.pyx", line 659, in mars.actors.pool.gevent_pool.Communicator._dispatch
File "mars/actors/pool/gevent_pool.pyx", line 730, in mars.actors.pool.gevent_pool.Communicator._send_process
File "mars/actors/pool/gevent_pool.pyx", line 278, in mars.actors.pool.gevent_pool.AsyncHandler.submit
File "src/gevent/event.py", line 344, in gevent._event.AsyncResult.result
File "src/gevent/event.py", line 268, in gevent._event.AsyncResult.get
File "src/gevent/event.py", line 286, in gevent._event.AsyncResult.get
File "src/gevent/event.py", line 266, in gevent._event.AsyncResult._raise_exception
File "/Users/hekaisheng/miniconda3/lib/python3.7/site-packages/gevent/_compat.py", line 47, in reraise
raise value.with_traceback(tb)
File "mars/actors/pool/gevent_pool.pyx", line 942, in mars.actors.pool.gevent_pool.Communicator._on_receive_send
File "mars/actors/pool/gevent_pool.pyx", line 73, in mars.actors.pool.gevent_pool.MessageContext.result
File "src/gevent/event.py", line 344, in gevent._event.AsyncResult.result
File "src/gevent/event.py", line 268, in gevent._event.AsyncResult.get
File "src/gevent/event.py", line 296, in gevent._event.AsyncResult.get
File "src/gevent/event.py", line 286, in gevent._event.AsyncResult.get
File "src/gevent/event.py", line 266, in gevent._event.AsyncResult._raise_exception
File "/Users/hekaisheng/miniconda3/lib/python3.7/site-packages/gevent/_compat.py", line 47, in reraise
raise value.with_traceback(tb)
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/hekaisheng/Documents/mars_dev/mars/mars/utils.py", line 298, in _wrapped
return func(*args, **kwargs)
File "/Users/hekaisheng/Documents/mars_dev/mars/mars/scheduler/graph.py", line 819, in build_tensor_merge_graph
tiled_tensor = self._get_tensor_by_key(tensor_key)
File "/Users/hekaisheng/Documents/mars_dev/mars/mars/scheduler/graph.py", line 804, in _get_tensor_by_key
return self._tensor_key_opid_to_tiled[(key, tid)][-1]
IndexError: list index out of range
2019-03-18T03:29:21Z <Greenlet at 0xc179668c8: <built-in method fire_run of mars.actors.pool.gevent_pool.ActorExecutionContext object at 0xc17968408>> failed with IndexError

Traceback (most recent call last):
File "src/gevent/greenlet.py", line 766, in gevent._greenlet.Greenlet.run
File "mars/actors/pool/gevent_pool.pyx", line 446, in mars.actors.pool.gevent_pool.ActorRemoteHelper._send_remote
File "mars/actors/pool/gevent_pool.pyx", line 450, in mars.actors.pool.gevent_pool.ActorRemoteHelper._send_remote
File "mars/actors/pool/gevent_pool.pyx", line 457, in mars.actors.pool.gevent_pool.ActorRemoteHelper._send_remote
File "/Users/hekaisheng/Documents/mars_dev/mars/mars/lib/six.py", line 702, in reraise
raise value.with_traceback(tb)
File "mars/actors/pool/gevent_pool.pyx", line 942, in mars.actors.pool.gevent_pool.Communicator._on_receive_send
File "mars/actors/pool/gevent_pool.pyx", line 73, in mars.actors.pool.gevent_pool.MessageContext.result
File "src/gevent/event.py", line 344, in gevent._event.AsyncResult.result
File "src/gevent/event.py", line 268, in gevent._event.AsyncResult.get
File "src/gevent/event.py", line 296, in gevent._event.AsyncResult.get
File "src/gevent/event.py", line 286, in gevent._event.AsyncResult.get
File "src/gevent/event.py", line 266, in gevent._event.AsyncResult._raise_exception
File "/Users/hekaisheng/miniconda3/lib/python3.7/site-packages/gevent/_compat.py", line 47, in reraise
raise value.with_traceback(tb)
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
cpdef on_receive(self, message):
File "mars/actors/core.pyx", line 110, in mars.actors.core.FunctionActor.on_receive
return getattr(self, method)(*args, **kwargs)
File "/Users/hekaisheng/Documents/mars_dev/mars/mars/scheduler/graph.py", line 66, in fetch_tensor
fetch_graph = deserialize_graph(graph_actor.build_tensor_merge_graph(tensor_key))
File "mars/actors/core.pyx", line 64, in mars.actors.core.ActorRef.__getattr__._mt_call
return self.send((item,) + args + (kwargs,), wait=wait)
File "mars/actors/core.pyx", line 36, in mars.actors.core.ActorRef.send
return self._ctx.send(self, message, wait=wait, callback=callback)
File "mars/actors/pool/gevent_pool.pyx", line 179, in mars.actors.pool.gevent_pool.ActorContext.send
File "mars/actors/pool/gevent_pool.pyx", line 754, in mars.actors.pool.gevent_pool.Communicator.send
File "mars/actors/pool/gevent_pool.pyx", line 755, in mars.actors.pool.gevent_pool.Communicator.send
File "mars/actors/pool/gevent_pool.pyx", line 750, in mars.actors.pool.gevent_pool.Communicator._send
File "mars/actors/pool/gevent_pool.pyx", line 659, in mars.actors.pool.gevent_pool.Communicator._dispatch
File "mars/actors/pool/gevent_pool.pyx", line 730, in mars.actors.pool.gevent_pool.Communicator._send_process
File "mars/actors/pool/gevent_pool.pyx", line 278, in mars.actors.pool.gevent_pool.AsyncHandler.submit
File "src/gevent/event.py", line 344, in gevent._event.AsyncResult.result
File "src/gevent/event.py", line 268, in gevent._event.AsyncResult.get
File "src/gevent/event.py", line 286, in gevent._event.AsyncResult.get
File "src/gevent/event.py", line 266, in gevent._event.AsyncResult._raise_exception
File "/Users/hekaisheng/miniconda3/lib/python3.7/site-packages/gevent/_compat.py", line 47, in reraise
raise value.with_traceback(tb)
File "mars/actors/pool/gevent_pool.pyx", line 942, in mars.actors.pool.gevent_pool.Communicator._on_receive_send
File "mars/actors/pool/gevent_pool.pyx", line 73, in mars.actors.pool.gevent_pool.MessageContext.result
File "src/gevent/event.py", line 344, in gevent._event.AsyncResult.result
File "src/gevent/event.py", line 268, in gevent._event.AsyncResult.get
File "src/gevent/event.py", line 296, in gevent._event.AsyncResult.get
File "src/gevent/event.py", line 286, in gevent._event.AsyncResult.get
File "src/gevent/event.py", line 266, in gevent._event.AsyncResult._raise_exception
File "/Users/hekaisheng/miniconda3/lib/python3.7/site-packages/gevent/_compat.py", line 47, in reraise
raise value.with_traceback(tb)
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
cpdef on_receive(self, message):
File "mars/actors/core.pyx", line 110, in mars.actors.core.FunctionActor.on_receive
return getattr(self, method)(*args, **kwargs)
File "/Users/hekaisheng/Documents/mars_dev/mars/mars/utils.py", line 298, in _wrapped
return func(*args, **kwargs)
File "/Users/hekaisheng/Documents/mars_dev/mars/mars/scheduler/graph.py", line 819, in build_tensor_merge_graph
tiled_tensor = self._get_tensor_by_key(tensor_key)
File "/Users/hekaisheng/Documents/mars_dev/mars/mars/scheduler/graph.py", line 804, in _get_tensor_by_key
return self._tensor_key_opid_to_tiled[(key, tid)][-1]
IndexError: list index out of range
2019-03-18T03:29:21Z <Greenlet at 0xc17962378: <built-in method _send_remote of mars.actors.pool.gevent_pool.ActorRemoteHelper object at 0xc17860e58>('0.0.0.0:29790', [bytearray(b'\x05\x01 \x00\x00\x00\x00\x00\x00\x00)> failed with IndexError

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-6-06ec9ebba039> in <module>
----> 1 cluster.session.run(a)

~/Documents/mars_dev/mars/mars/session.py in run(self, *tensors, **kw)
107
108         tensors = tuple(mt.tensor(t) for t in tensors)
--> 109         result = self._sess.run(*tensors, **kw)
110
111         for t in tensors:

~/Documents/mars_dev/mars/mars/deploy/local/session.py in run(self, *tensors, **kw)
103             return
104         else:
--> 105             return self.fetch(*tensors)
106
107     def fetch(self, *tensors):

~/Documents/mars_dev/mars/mars/deploy/local/session.py in fetch(self, *tensors)
116             future = self._api.fetch_data(self._session_id, graph_key, key, wait=False)
117             futures.append(future)
--> 118         return [dataserializer.loads(f.result()) for f in futures]
119
120     def decref(self, *keys):

~/Documents/mars_dev/mars/mars/deploy/local/session.py in <listcomp>(.0)
116             future = self._api.fetch_data(self._session_id, graph_key, key, wait=False)
117             futures.append(future)
--> 118         return [dataserializer.loads(f.result()) for f in futures]
119
120     def decref(self, *keys):

~/miniconda3/lib/python3.7/site-packages/gevent/_event.cpython-37m-darwin.so in gevent._event.AsyncResult.result()

~/miniconda3/lib/python3.7/site-packages/gevent/_event.cpython-37m-darwin.so in gevent._event.AsyncResult.get()

~/miniconda3/lib/python3.7/site-packages/gevent/_event.cpython-37m-darwin.so in gevent._event.AsyncResult.get()

~/miniconda3/lib/python3.7/site-packages/gevent/_event.cpython-37m-darwin.so in gevent._event.AsyncResult.get()

~/miniconda3/lib/python3.7/site-packages/gevent/_event.cpython-37m-darwin.so in gevent._event.AsyncResult._raise_exception()

~/miniconda3/lib/python3.7/site-packages/gevent/_compat.py in reraise(t, value, tb)
45     def reraise(t, value, tb=None): # pylint:disable=unused-argument
46         if value.__traceback__ is not tb and tb is not None:
---> 47             raise value.with_traceback(tb)
48         raise value
49     def exc_clear():

~/Documents/mars_dev/mars/mars/actors/pool/gevent_pool.pyx in mars.actors.pool.gevent_pool.ActorRemoteHelper._async_run.on_failure()

~/miniconda3/lib/python3.7/site-packages/gevent/_greenlet.cpython-37m-darwin.so in gevent._greenlet.Greenlet.get()

~/miniconda3/lib/python3.7/site-packages/gevent/_greenlet.cpython-37m-darwin.so in gevent._greenlet.Greenlet._raise_exception()

~/miniconda3/lib/python3.7/site-packages/gevent/_compat.py in reraise()
45     def reraise(t, value, tb=None): # pylint:disable=unused-argument
46         if value.__traceback__ is not tb and tb is not None:
---> 47             raise value.with_traceback(tb)
48         raise value
49     def exc_clear():

~/miniconda3/lib/python3.7/site-packages/gevent/_greenlet.cpython-37m-darwin.so in gevent._greenlet.Greenlet.run()

~/Documents/mars_dev/mars/mars/actors/pool/gevent_pool.pyx in mars.actors.pool.gevent_pool.ActorRemoteHelper._send_remote()

~/Documents/mars_dev/mars/mars/actors/pool/gevent_pool.pyx in mars.actors.pool.gevent_pool.ActorRemoteHelper._send_remote()

~/Documents/mars_dev/mars/mars/actors/pool/gevent_pool.pyx in mars.actors.pool.gevent_pool.ActorRemoteHelper._send_remote()

~/Documents/mars_dev/mars/mars/lib/six.py in reraise()
700                 value = tp()
701             if value.__traceback__ is not tb:
--> 702                 raise value.with_traceback(tb)
703             raise value
704         finally:

~/Documents/mars_dev/mars/mars/actors/pool/gevent_pool.pyx in mars.actors.pool.gevent_pool.Communicator._on_receive_send()

~/Documents/mars_dev/mars/mars/actors/pool/gevent_pool.pyx in mars.actors.pool.gevent_pool.MessageContext.result()

~/miniconda3/lib/python3.7/site-packages/gevent/_event.cpython-37m-darwin.so in gevent._event.AsyncResult.result()

~/miniconda3/lib/python3.7/site-packages/gevent/_event.cpython-37m-darwin.so in gevent._event.AsyncResult.get()

~/miniconda3/lib/python3.7/site-packages/gevent/_event.cpython-37m-darwin.so in gevent._event.AsyncResult.get()

~/miniconda3/lib/python3.7/site-packages/gevent/_event.cpython-37m-darwin.so in gevent._event.AsyncResult.get()

~/miniconda3/lib/python3.7/site-packages/gevent/_event.cpython-37m-darwin.so in gevent._event.AsyncResult._raise_exception()

~/miniconda3/lib/python3.7/site-packages/gevent/_compat.py in reraise()
45     def reraise(t, value, tb=None): # pylint:disable=unused-argument
46         if value.__traceback__ is not tb and tb is not None:
---> 47             raise value.with_traceback(tb)
48         raise value
49     def exc_clear():

~/Documents/mars_dev/mars/mars/actors/pool/gevent_pool.pyx in mars.actors.pool.gevent_pool.ActorExecutionContext.fire_run()

~/Documents/mars_dev/mars/mars/actors/core.pyx in mars.actors.core.FunctionActor.on_receive()
106
107 cdef class FunctionActor(Actor):
--> 108     cpdef on_receive(self, message):
109         method, args, kwargs = message[0], message[1:-1], message[-1]
110         return getattr(self, method)(*args, **kwargs)

~/Documents/mars_dev/mars/mars/actors/core.pyx in mars.actors.core.FunctionActor.on_receive()
108     cpdef on_receive(self, message):
109         method, args, kwargs = message[0], message[1:-1], message[-1]
--> 110         return getattr(self, method)(*args, **kwargs)
111
112

~/Documents/mars_dev/mars/mars/scheduler/graph.py in fetch_tensor()
64
65         graph_actor = self.ctx.actor_ref(GraphActor.gen_name(session_id, graph_key))
---> 66         fetch_graph = deserialize_graph(graph_actor.build_tensor_merge_graph(tensor_key))
67
68         if len(fetch_graph) == 1 and isinstance(next(fetch_graph.iter_nodes()).op, TensorFetch):

~/Documents/mars_dev/mars/mars/actors/core.pyx in mars.actors.core.ActorRef.__getattr__._mt_call()
62                 return self.tell((item,) + args + (kwargs,), delay=delay, wait=wait)
63             else:
---> 64                 return self.send((item,) + args + (kwargs,), wait=wait)
65
66         return _mt_call

~/Documents/mars_dev/mars/mars/actors/core.pyx in mars.actors.core.ActorRef.send()
34
35     cpdef object send(self, object message, bint wait=True, object callback=None):
---> 36         return self._ctx.send(self, message, wait=wait, callback=callback)
37
38     cpdef object tell(self, object message, object delay=None, bint wait=True,

~/Documents/mars_dev/mars/mars/actors/pool/gevent_pool.pyx in mars.actors.pool.gevent_pool.ActorContext.send()

~/Documents/mars_dev/mars/mars/actors/pool/gevent_pool.pyx in mars.actors.pool.gevent_pool.Communicator.send()

~/Documents/mars_dev/mars/mars/actors/pool/gevent_pool.pyx in mars.actors.pool.gevent_pool.Communicator.send()

~/Documents/mars_dev/mars/mars/actors/pool/gevent_pool.pyx in mars.actors.pool.gevent_pool.Communicator._send()

~/Documents/mars_dev/mars/mars/actors/pool/gevent_pool.pyx in mars.actors.pool.gevent_pool.Communicator._dispatch()

~/Documents/mars_dev/mars/mars/actors/pool/gevent_pool.pyx in mars.actors.pool.gevent_pool.Communicator._send_process()

~/Documents/mars_dev/mars/mars/actors/pool/gevent_pool.pyx in mars.actors.pool.gevent_pool.AsyncHandler.submit()

~/miniconda3/lib/python3.7/site-packages/gevent/_event.cpython-37m-darwin.so in gevent._event.AsyncResult.result()

~/miniconda3/lib/python3.7/site-packages/gevent/_event.cpython-37m-darwin.so in gevent._event.AsyncResult.get()

~/miniconda3/lib/python3.7/site-packages/gevent/_event.cpython-37m-darwin.so in gevent._event.AsyncResult.get()

~/miniconda3/lib/python3.7/site-packages/gevent/_event.cpython-37m-darwin.so in gevent._event.AsyncResult._raise_exception()

~/miniconda3/lib/python3.7/site-packages/gevent/_compat.py in reraise()
45     def reraise(t, value, tb=None): # pylint:disable=unused-argument
46         if value.__traceback__ is not tb and tb is not None:
---> 47             raise value.with_traceback(tb)
48         raise value
49     def exc_clear():

~/Documents/mars_dev/mars/mars/actors/pool/gevent_pool.pyx in mars.actors.pool.gevent_pool.Communicator._on_receive_send()

~/Documents/mars_dev/mars/mars/actors/pool/gevent_pool.pyx in mars.actors.pool.gevent_pool.MessageContext.result()

~/miniconda3/lib/python3.7/site-packages/gevent/_event.cpython-37m-darwin.so in gevent._event.AsyncResult.result()

~/miniconda3/lib/python3.7/site-packages/gevent/_event.cpython-37m-darwin.so in gevent._event.AsyncResult.get()

~/miniconda3/lib/python3.7/site-packages/gevent/_event.cpython-37m-darwin.so in gevent._event.AsyncResult.get()

~/miniconda3/lib/python3.7/site-packages/gevent/_event.cpython-37m-darwin.so in gevent._event.AsyncResult.get()

~/miniconda3/lib/python3.7/site-packages/gevent/_event.cpython-37m-darwin.so in gevent._event.AsyncResult._raise_exception()

~/miniconda3/lib/python3.7/site-packages/gevent/_compat.py in reraise()
45     def reraise(t, value, tb=None): # pylint:disable=unused-argument
46         if value.__traceback__ is not tb and tb is not None:
---> 47             raise value.with_traceback(tb)
48         raise value
49     def exc_clear():

~/Documents/mars_dev/mars/mars/actors/pool/gevent_pool.pyx in mars.actors.pool.gevent_pool.ActorExecutionContext.fire_run()

~/Documents/mars_dev/mars/mars/actors/core.pyx in mars.actors.core.FunctionActor.on_receive()
106
107 cdef class FunctionActor(Actor):
--> 108     cpdef on_receive(self, message):
109         method, args, kwargs = message[0], message[1:-1], message[-1]
110         return getattr(self, method)(*args, **kwargs)

~/Documents/mars_dev/mars/mars/actors/core.pyx in mars.actors.core.FunctionActor.on_receive()
108     cpdef on_receive(self, message):
109         method, args, kwargs = message[0], message[1:-1], message[-1]
--> 110         return getattr(self, method)(*args, **kwargs)
111
112

~/Documents/mars_dev/mars/mars/utils.py in _wrapped()
296     def _wrapped(*args, **kwargs):
297         try:
--> 298             return func(*args, **kwargs)
299         except:  # noqa: E722
300             kwcopy = kwargs.copy()

~/Documents/mars_dev/mars/mars/scheduler/graph.py in build_tensor_merge_graph()
817         from ..tensor.expressions.merge.concatenate import TensorConcatenate
818
--> 819         tiled_tensor = self._get_tensor_by_key(tensor_key)
820         graph = DAG()
821         if len(tiled_tensor.chunks) == 1:

~/Documents/mars_dev/mars/mars/scheduler/graph.py in _get_tensor_by_key()
802     def _get_tensor_by_key(self, key):
803         tid = self._tensor_key_to_opid[key]
--> 804         return self._tensor_key_opid_to_tiled[(key, tid)][-1]
805
806     @log_unhandled

IndexError: list index out of range

# Fixed code
def build_graph(
    self, graph=None, cls=DAG, tiled=False, compose=True, executed_keys=None
):
    from .tensor.expressions.utils import convert_to_fetch

    executed_keys = executed_keys or []
    if tiled and self.is_coarse():
        self.tiles()

    graph = graph if graph is not None else cls()
    keys = None

    if tiled:
        nodes = list(c.data for c in self.chunks)
        keys = list(c.key for c in self.chunks)
    else:
        nodes = list(self.op.outputs)

    visited = set()
    while len(nodes) > 0:
        node = nodes.pop()

        # replace executed tensor/chunk by tensor/chunk with fetch op
        if node.key in executed_keys:
            node = convert_to_fetch(node).data

        visited.add(node)
        if not graph.contains(node):
            graph.add_node(node)
        children = node.inputs or []
        for c in children:
            if c.key in executed_keys:
                continue
            if not graph.contains(c):
                graph.add_node(c)
            if not graph.has_successor(c, node):
                graph.add_edge(c, node)
        nodes.extend(
            [
                c
                for c in itertools.chain(*[inp.op.outputs for inp in node.inputs or []])
                if c not in visited
            ]
        )
    if tiled and compose:
        graph.compose(keys=keys)

    if not tiled and any(not n.is_coarse() for n in graph):
        return self._to_coarse_graph(graph)

    return graph