From 958f74340b4cbb3883a63d7bcad3e498b11755af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Fri, 5 Jun 2020 18:43:47 +0200 Subject: [PATCH] mock: Fix concurrent map writes in AddPiece --- mock/mock.go | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/mock/mock.go b/mock/mock.go index 26aacce13..2e80bde36 100644 --- a/mock/mock.go +++ b/mock/mock.go @@ -70,17 +70,6 @@ func (mgr *SectorMgr) NewSector(ctx context.Context, sector abi.SectorID) error func (mgr *SectorMgr) AddPiece(ctx context.Context, sectorId abi.SectorID, existingPieces []abi.UnpaddedPieceSize, size abi.UnpaddedPieceSize, r io.Reader) (abi.PieceInfo, error) { log.Warn("Add piece: ", sectorId, size, mgr.proofType) - mgr.lk.Lock() - ss, ok := mgr.sectors[sectorId] - if !ok { - ss = §orState{ - state: statePacking, - } - mgr.sectors[sectorId] = ss - } - mgr.lk.Unlock() - ss.lk.Lock() - defer ss.lk.Unlock() var b bytes.Buffer tr := io.TeeReader(r, &b) @@ -92,9 +81,24 @@ func (mgr *SectorMgr) AddPiece(ctx context.Context, sectorId abi.SectorID, exist log.Warn("Generated Piece CID: ", c) + mgr.lk.Lock() mgr.pieces[c] = b.Bytes() + + ss, ok := mgr.sectors[sectorId] + if !ok { + ss = §orState{ + state: statePacking, + } + mgr.sectors[sectorId] = ss + } + mgr.lk.Unlock() + + ss.lk.Lock() ss.pieces = append(ss.pieces, c) + ss.lk.Unlock() + return abi.PieceInfo{ + Size: size.Padded(), PieceCID: c, }, nil