# Buggy code
def build_tensor_merge_graph(self, tensor_key):
    from ..tensor.expressions.merge.concatenate import TensorConcatenate

    tiled_tensor = self._get_tensor_by_key(tensor_key)
    graph = DAG()
    if len(tiled_tensor.chunks) == 1:
        # only one chunk, just trigger fetch
        c = tiled_tensor.chunks[0]
        op = TensorFetch(dtype=c.dtype)
        fetch_chunk = op.new_chunk(None, c.shape, index=c.index, _key=c.key).data
        graph.add_node(fetch_chunk)
    else:
        fetch_chunks = []
        for c in tiled_tensor.chunks:
            op = TensorFetch(dtype=c.dtype)
            fetch_chunk = op.new_chunk(None, c.shape, index=c.index, _key=c.key).data
            graph.add_node(fetch_chunk)
            fetch_chunks.append(fetch_chunk)
        chunk = (
            TensorConcatenate(dtype=tiled_tensor.op.dtype)
            .new_chunk(fetch_chunks, tiled_tensor.shape)
            .data
        )
        graph.add_node(chunk)
        [graph.add_edge(fetch_chunk, chunk) for fetch_chunk in fetch_chunks]

    return serialize_graph(graph)


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

In [2]: cluster = new_cluster(scheduler_n_process=2, worker_n_process=3, shared_memory='20M',)

In [3]: import mars.tensor as mt

In [4]: import scipy.sparse as sps

In [5]: a = sps.csr_matrix((10000, 10000))

In [6]: b = sps.csr_matrix((10000, 1))

In [7]: t1 = mt.tensor(a)

In [8]: t2 = mt.tensor(b)


In [9]: from mars.session import new_session

In [10]: cluster.session.run(t1 * t2)

Unexpected exception occurred in ChunkHolderActor.spill_size.
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/worker/chunkholder.py", line 123, in spill_size
raise SpillExhausted
mars.errors.SpillExhausted
Unhandled exception in promise call
Traceback (most recent call last):
File "/Users/hekaisheng/Documents/mars_dev/mars/mars/promise.py", line 304, in _wrapped
return func(*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/worker/chunkholder.py", line 123, in spill_size
raise SpillExhausted
mars.errors.SpillExhausted
....

# Fixed code
def build_tensor_merge_graph(self, tensor_key):
    from ..tensor.expressions.merge.concatenate import TensorConcatenate

    tiled_tensor = self._get_tensor_by_key(tensor_key)
    graph = DAG()
    if len(tiled_tensor.chunks) == 1:
        # only one chunk, just trigger fetch
        c = tiled_tensor.chunks[0]
        op = TensorFetch(dtype=c.dtype, sparse=c.op.sparse)
        fetch_chunk = op.new_chunk(None, c.shape, index=c.index, _key=c.key).data
        graph.add_node(fetch_chunk)
    else:
        fetch_chunks = []
        for c in tiled_tensor.chunks:
            op = TensorFetch(dtype=c.dtype, sparse=c.op.sparse)
            fetch_chunk = op.new_chunk(None, c.shape, index=c.index, _key=c.key).data
            graph.add_node(fetch_chunk)
            fetch_chunks.append(fetch_chunk)
        chunk = (
            TensorConcatenate(dtype=tiled_tensor.op.dtype)
            .new_chunk(fetch_chunks, tiled_tensor.shape)
            .data
        )
        graph.add_node(chunk)
        [graph.add_edge(fetch_chunk, chunk) for fetch_chunk in fetch_chunks]

    return serialize_graph(graph)