swarm/network: fix data race in stream.(*Peer).handleOfferedHashesMsg()

handleOfferedHashesMsg() contained a data race:
- read => in a goroutine, call to c.batchDone()
- write => in the main thread, write to c.sessionAt

c.batchDone() contained a call to c.AddInterval(). Client was a value
receiver for AddInterval. So on c.AddInterval() call the whole client
struct got copied (read) while one of its field was modified in
handleOfferedHashesMsg() (write).

fixes ethersphere/go-ethereum#1086
This commit is contained in:
Ferenc Szabo 2019-01-17 10:41:02 +01:00
parent bcb2594151
commit aa149ad16b

View file

@ -666,7 +666,7 @@ func peerStreamIntervalsKey(p *Peer, s Stream) string {
return p.ID().String() + s.String() return p.ID().String() + s.String()
} }
func (c client) AddInterval(start, end uint64) (err error) { func (c *client) AddInterval(start, end uint64) (err error) {
i := &intervals.Intervals{} i := &intervals.Intervals{}
err = c.intervalsStore.Get(c.intervalsKey, i) err = c.intervalsStore.Get(c.intervalsKey, i)
if err != nil { if err != nil {
@ -676,7 +676,7 @@ func (c client) AddInterval(start, end uint64) (err error) {
return c.intervalsStore.Put(c.intervalsKey, i) return c.intervalsStore.Put(c.intervalsKey, i)
} }
func (c client) NextInterval() (start, end uint64, err error) { func (c *client) NextInterval() (start, end uint64, err error) {
i := &intervals.Intervals{} i := &intervals.Intervals{}
err = c.intervalsStore.Get(c.intervalsKey, i) err = c.intervalsStore.Get(c.intervalsKey, i)
if err != nil { if err != nil {