# Buggy code
def build_graph(self, graph=None, cls=DAG, tiled=False, compose=True):
    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 = [self]
    visited = set()
    while len(nodes) > 0:
        chunk = nodes.pop()
        visited.add(chunk)
        if not graph.contains(chunk):
            graph.add_node(chunk)
        children = chunk.inputs or []
        for c in children:
            if not graph.contains(c):
                graph.add_node(c)
            if not graph.has_successor(c, chunk):
                graph.add_edge(c, chunk)
        nodes.extend([c for c in children if c not in visited])
    if tiled and compose:
        graph.compose(keys=keys)
    return graph


# Traceback (KeyError)
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
cpdef object fire_run(self):
File "mars/actors/pool/gevent_pool.pyx", line 91, in mars.actors.pool.gevent_pool.ActorExecutionContext.fire_run
with self.lock:
File "mars/actors/pool/gevent_pool.pyx", line 102, in mars.actors.pool.gevent_pool.ActorExecutionContext.fire_run
raise
File "mars/actors/pool/gevent_pool.pyx", line 96, in mars.actors.pool.gevent_pool.ActorExecutionContext.fire_run
res = actor.on_receive(message_ctx.message)
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/qinxuye/Workspace/mars/mars/scheduler/graph.py", line 153, in execute_graph
self.prepare_graph()
File "/Users/qinxuye/Workspace/mars/mars/scheduler/graph.py", line 207, in prepare_graph
tensor_graph = deserialize_graph(self._serialized_tensor_graph)
File "/Users/qinxuye/Workspace/mars/mars/utils.py", line 403, in deserialize_graph
return DirectedGraph.from_json(json_obj)
File "mars/graph.pyx", line 415, in mars.graph.DirectedGraph.from_json
return cls.deserialize(SerialiableGraph.from_json(json_obj))
File "mars/serialize/core.pyx", line 537, in mars.serialize.core.Serializable.from_json
return cls.deserialize(provider, obj)
File "mars/serialize/core.pyx", line 510, in mars.serialize.core.Serializable.deserialize
[cb(key_to_instance) for cb in callbacks]
File "mars/serialize/jsonserializer.pyx", line 176, in mars.serialize.jsonserializer.JsonSerializeProvider._deserialize_list.cb.inner
o = subs[v.key, v.id]
KeyError: ('9e2478de2695b727435601490ebaa999', '5219323976')
2018-12-07T11:14:35Z <Greenlet "Greenlet-0" at 0x138f5a448: <built-in method fire_run of mars.actors.pool.gevent_pool.ActorExecutionContext object at 0x139002cc8>> failed with KeyError

# Fixed code
def build_graph(self, graph=None, cls=DAG, tiled=False, compose=True):
    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:
        chunk = nodes.pop()
        visited.add(chunk)
        if not graph.contains(chunk):
            graph.add_node(chunk)
        children = chunk.inputs or []
        for c in children:
            if not graph.contains(c):
                graph.add_node(c)
            if not graph.has_successor(c, chunk):
                graph.add_edge(c, chunk)
        nodes.extend([c for c in children if c not in visited])
    if tiled and compose:
        graph.compose(keys=keys)
    return graph