From 841a6272c661fae506cd656ea884655f3c313743 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Wed, 24 Oct 2018 12:41:12 -0700 Subject: [PATCH] fix a data-race in the mock net This was causing https://github.com/ipfs/go-ipfs/pull/5637#issuecomment-432792969 --- p2p/net/mock/mock_stream.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/p2p/net/mock/mock_stream.go b/p2p/net/mock/mock_stream.go index 4f0c395fe..c680ead26 100644 --- a/p2p/net/mock/mock_stream.go +++ b/p2p/net/mock/mock_stream.go @@ -56,6 +56,11 @@ func (s *stream) Write(p []byte) (n int, err error) { l := s.conn.link delay := l.GetLatency() + l.RateLimit(len(p)) t := time.Now().Add(delay) + + // Copy it. + cpy := make([]byte, len(p)) + copy(cpy, p) + select { case <-s.closed: // bail out if we're closing. return 0, s.writeErr