]> Cypherpunks.ru repositories - nncp.git/commitdiff
Lock map during read, to prevent concurrent access
authorSergey Matveev <stargrave@stargrave.org>
Thu, 19 Jan 2017 06:29:13 +0000 (09:29 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Thu, 19 Jan 2017 06:29:13 +0000 (09:29 +0300)
src/cypherpunks.ru/nncp/sp.go

index 17f62c5aa5ddbba4faf533daec6a7bb6166e2b43..8b64be1a68e24505513fa4d6476a895a525df180 100644 (file)
@@ -826,13 +826,16 @@ func (state *SPState) ProcessSP(payload []byte) ([][]byte, error) {
                                return nil, err
                        }
                        ourSize := uint64(file.Offset) + uint64(len(file.Payload))
+                       state.RLock()
                        sdsp["fullsize"] = strconv.FormatInt(int64(state.infosTheir[*file.Hash].Size), 10)
                        sdsp["size"] = strconv.FormatInt(int64(ourSize), 10)
                        state.ctx.LogP("sp-file", sdsp, "")
                        if state.infosTheir[*file.Hash].Size != ourSize {
+                               state.RUnlock()
                                fd.Close()
                                continue
                        }
+                       state.RUnlock()
                        go func() {
                                if err := fd.Sync(); err != nil {
                                        state.ctx.LogE("sp-file", SdsAdd(sdsp, SDS{"err": err}), "sync")
@@ -918,10 +921,12 @@ func (state *SPState) ProcessSP(payload []byte) ([][]byte, error) {
        if infosGot {
                var pkts int
                var size uint64
+               state.RLock()
                for _, info := range state.infosTheir {
                        pkts++
                        size += info.Size
                }
+               state.RUnlock()
                state.ctx.LogI("sp-infos", SDS{
                        "xx":   string(TRx),
                        "node": state.Node.Id,