# Buggy code
def build_fetch_chunk(chunk, input_chunk_keys=None, **kwargs):
    from .operands import ShuffleProxy

    chunk_op = chunk.op
    params = chunk.params.copy()
    params.pop("index", None)

    if isinstance(chunk_op, ShuffleProxy):
        # for shuffle nodes, we build FetchShuffle chunks
        # to replace ShuffleProxy
        to_fetch_keys = [
            pinp.key
            for pinp in chunk.inputs
            if input_chunk_keys is None or pinp.key in input_chunk_keys
        ]
        op = get_fetch_op_cls(chunk_op)(to_fetch_keys=to_fetch_keys)
    else:
        # for non-shuffle nodes, we build Fetch chunks
        # to replace original chunk
        op = get_fetch_op_cls(chunk_op)(sparse=chunk.op.sparse)
    return op.new_chunk(None, kws=[params], _key=chunk.key, _id=chunk.id, **kwargs)


# Traceback (TypeError)
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 112, in mars.actors.core._FunctionActor.on_receive
File "mars/actors/core.pyx", line 114, in mars.actors.core._FunctionActor.on_receive
File "/Users/wenjun.swj/Code/mars/mars/utils.py", line 288, in _wrapped
return func(*args, **kwargs)
File "/Users/wenjun.swj/Code/mars/mars/scheduler/graph.py", line 335, in execute_graph
self.prepare_graph(compose=compose)
File "/Users/wenjun.swj/Code/mars/mars/utils.py", line 288, in _wrapped
return func(*args, **kwargs)
File "/Users/wenjun.swj/Code/mars/mars/scheduler/graph.py", line 475, in prepare_graph
td = self.tile_fetch_tileable(tileable)
File "/Users/wenjun.swj/Code/mars/mars/scheduler/graph.py", line 936, in tile_fetch_tileable
fetch_graph = deserialize_graph(graph_ref.build_fetch_graph(tileable_key))
File "mars/actors/core.pyx", line 65, in mars.actors.core.ActorRef.__getattr__._mt_call
File "mars/actors/core.pyx", line 37, 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 657, in mars.actors.pool.gevent_pool.Communicator._dispatch
File "mars/actors/pool/gevent_pool.pyx", line 682, in mars.actors.pool.gevent_pool.Communicator._send_local
File "src/gevent/greenlet.py", line 709, in gevent._greenlet.Greenlet.get
File "src/gevent/greenlet.py", line 317, in gevent._greenlet.Greenlet._raise_exception
File "/Users/wenjun.swj/miniconda3/lib/python3.7/site-packages/gevent/_compat.py", line 47, in reraise
raise value.with_traceback(tb)
File "src/gevent/greenlet.py", line 766, in gevent._greenlet.Greenlet.run
File "mars/actors/pool/gevent_pool.pyx", line 72, in mars.actors.pool.gevent_pool.MessageContext.result
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 286, in gevent._event.AsyncResult.get
File "src/gevent/event.py", line 266, in gevent._event.AsyncResult._raise_exception
File "/Users/wenjun.swj/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 112, in mars.actors.core._FunctionActor.on_receive
File "mars/actors/core.pyx", line 114, in mars.actors.core._FunctionActor.on_receive
File "/Users/wenjun.swj/Code/mars/mars/scheduler/graph.py", line 928, in build_fetch_graph
return serialize_graph(graph)
File "/Users/wenjun.swj/Code/mars/mars/utils.py", line 217, in serialize_graph
ser_graph = graph.to_pb().SerializeToString()
File "mars/graph.pyx", line 431, in mars.graph.DirectedGraph.to_pb
File "mars/serialize/core.pyx", line 563, in mars.serialize.core.Serializable.to_pb
File "mars/serialize/core.pyx", line 547, in mars.serialize.core.Serializable.serialize
File "mars/serialize/core.pyx", line 665, in mars.serialize.core.Provider.serialize_model
File "mars/serialize/core.pyx", line 141, in mars.serialize.core.Field.serialize
File "mars/serialize/core.pyx", line 142, in mars.serialize.core.Field.serialize
File "mars/serialize/pbserializer.pyx", line 476, in mars.serialize.pbserializer.ProtobufSerializeProvider.serialize_field
File "mars/serialize/core.pyx", line 547, in mars.serialize.core.Serializable.serialize
File "mars/serialize/core.pyx", line 665, in mars.serialize.core.Provider.serialize_model
File "mars/serialize/core.pyx", line 141, in mars.serialize.core.Field.serialize
File "mars/serialize/core.pyx", line 142, in mars.serialize.core.Field.serialize
File "mars/serialize/pbserializer.pyx", line 447, in mars.serialize.pbserializer.ProtobufSerializeProvider.serialize_field
File "mars/serialize/core.pyx", line 142, in mars.serialize.core.Field.serialize
File "mars/serialize/pbserializer.pyx", line 467, in mars.serialize.pbserializer.ProtobufSerializeProvider.serialize_field
File "mars/serialize/core.pyx", line 547, in mars.serialize.core.Serializable.serialize
File "mars/serialize/core.pyx", line 665, in mars.serialize.core.Provider.serialize_model
File "mars/serialize/core.pyx", line 141, in mars.serialize.core.Field.serialize
File "mars/serialize/core.pyx", line 142, in mars.serialize.core.Field.serialize
File "mars/serialize/pbserializer.pyx", line 485, in mars.serialize.pbserializer.ProtobufSerializeProvider.serialize_field
File "mars/serialize/pbserializer.pyx", line 428, in mars.serialize.pbserializer.ProtobufSerializeProvider._set_value
File "mars/serialize/pbserializer.pyx", line 365, in mars.serialize.pbserializer.ProtobufSerializeProvider._set_typed_value
File "mars/serialize/pbserializer.pyx", line 246, in mars.serialize.pbserializer.ProtobufSerializeProvider._set_tuple
File "mars/serialize/pbserializer.pyx", line 428, in mars.serialize.pbserializer.ProtobufSerializeProvider._set_value
File "mars/serialize/pbserializer.pyx", line 365, in mars.serialize.pbserializer.ProtobufSerializeProvider._set_typed_value
File "mars/serialize/pbserializer.pyx", line 246, in mars.serialize.pbserializer.ProtobufSerializeProvider._set_tuple
File "mars/serialize/pbserializer.pyx", line 428, in mars.serialize.pbserializer.ProtobufSerializeProvider._set_value
File "mars/serialize/pbserializer.pyx", line 338, in mars.serialize.pbserializer.ProtobufSerializeProvider._set_typed_value
TypeError: nan has type float, but expected one of: int, long
2019-05-21T10:36:49Z <Greenlet at 0xc20a0d598: <built-in method fire_run of mars.actors.pool.gevent_pool.ActorExecutionContext object at 0xc20d32138>> failed with TypeError

# Fixed code
def build_fetch_chunk(chunk, input_chunk_keys=None, **kwargs):
    from .operands import ShuffleProxy

    chunk_op = chunk.op
    params = chunk.params.copy()

    if isinstance(chunk_op, ShuffleProxy):
        # for shuffle nodes, we build FetchShuffle chunks
        # to replace ShuffleProxy
        to_fetch_keys = [
            pinp.key
            for pinp in chunk.inputs
            if input_chunk_keys is None or pinp.key in input_chunk_keys
        ]
        op = get_fetch_op_cls(chunk_op)(to_fetch_keys=to_fetch_keys)
    else:
        # for non-shuffle nodes, we build Fetch chunks
        # to replace original chunk
        op = get_fetch_op_cls(chunk_op)(sparse=chunk.op.sparse)
    return op.new_chunk(None, kws=[params], _key=chunk.key, _id=chunk.id, **kwargs)