Skip to content

Commit

Permalink
Merge pull request #1394 from ersonp/fix/panic-and-datarace
Browse files Browse the repository at this point in the history
Fix/panic and datarace
  • Loading branch information
mrpalide authored Oct 21, 2022
2 parents 1599b75 + 13dc58a commit 65e92c0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
8 changes: 6 additions & 2 deletions pkg/transport/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,12 @@ func (le *LogEntry) GobDecode(b []byte) error {
if err := dec.Decode(&sb); err != nil {
return err
}
atomic.StoreUint64(le.RecvBytes, rb)
atomic.StoreUint64(le.SentBytes, sb)
if le.RecvBytes != nil {
atomic.StoreUint64(le.RecvBytes, rb)
}
if le.SentBytes != nil {
atomic.StoreUint64(le.SentBytes, sb)
}
return nil
}

Expand Down
11 changes: 11 additions & 0 deletions pkg/transport/managed_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type ManagedTransport struct {
rPK cipher.PubKey
Entry Entry
LogEntry *LogEntry
logMx sync.Mutex
logUpdates uint32

dc DiscoveryClient
Expand Down Expand Up @@ -425,11 +426,17 @@ func (mt *ManagedTransport) readPacket() (packet routing.Packet, err error) {
*/

func (mt *ManagedTransport) logSent(b uint64) {
mt.logMx.Lock()
defer mt.logMx.Unlock()

mt.LogEntry.AddSent(b)
atomic.AddUint32(&mt.logUpdates, 1)
}

func (mt *ManagedTransport) logRecv(b uint64) {
mt.logMx.Lock()
defer mt.logMx.Unlock()

mt.LogEntry.AddRecv(b)
atomic.AddUint32(&mt.logUpdates, 1)
}
Expand All @@ -449,6 +456,10 @@ func (mt *ManagedTransport) recordLog() {
if !mt.logMod() {
return
}

mt.logMx.Lock()
defer mt.logMx.Unlock()

if err := mt.ls.Record(mt.Entry.ID, mt.LogEntry); err != nil {
mt.log.WithError(err).Warn("Failed to record log entry.")
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/transport/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ func (tm *Manager) Networks() []network.Type {

// Stcpr returns stcpr client
func (tm *Manager) Stcpr() (network.Client, bool) {
tm.mx.Lock()
defer tm.mx.Unlock()
c, ok := tm.netClients[network.STCPR]
return c, ok
}
Expand Down

0 comments on commit 65e92c0

Please sign in to comment.