Skip to content

Commit

Permalink
switch to locks from atomics
Browse files Browse the repository at this point in the history
fixes panics on some systems while trying to atomically load a uint
```
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x4 pc=0x12488]
goroutine 343 [running]:
github.com/mysteriumnetwork/node/session/pingpong.(*InvoiceTracker).getNotReceivedExchangeMessageCount(...)
	/Users/anjmao/go/src/github.com/mysteriumnetwork/node/session/pingpong/invoice_tracker.go:384
github.com/mysteriumnetwork/node/session/pingpong.(*InvoiceTracker).sendInvoice(0x29246e0, 0x28bfed4, 0x4)
	/Users/anjmao/go/src/github.com/mysteriumnetwork/node/session/pingpong/invoice_tracker.go:394 +0x28
github.com/mysteriumnetwork/node/session/pingpong.(*InvoiceTracker).Start(0x29246e0, 0x229c76c, 0x4523b0)
	/Users/anjmao/go/src/github.com/mysteriumnetwork/node/session/pingpong/invoice_tracker.go:356 +0x458
github.com/mysteriumnetwork/node/session.(*Manager).Create.func2(0x14807f8, 0x29246e0, 0x270e700, 0x1328f020, 0x2a, 0x132638b0)
	/Users/anjmao/go/src/github.com/mysteriumnetwork/node/session/manager.go:183 +0x24
created by github.com/mysteriumnetwork/node/session.(*Manager).Create
	/Users/anjmao/go/src/github.com/mysteriumnetwork/node/session/manager.go:182 +0x358
```
  • Loading branch information
vkuznecovas committed Jan 13, 2020
1 parent b93e975 commit a601203
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 21 deletions.
15 changes: 11 additions & 4 deletions session/payment/session_balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ package payment

import (
"math"
"sync/atomic"
"sync"
"time"

"github.com/mysteriumnetwork/node/identity"
Expand Down Expand Up @@ -85,6 +85,7 @@ type SessionBalance struct {

sequenceID uint64
notReceivedPromiseCount uint64
notReceivedPromiseLock sync.Mutex
maxNotReceivedPromises uint64
}

Expand Down Expand Up @@ -149,15 +150,21 @@ func (sb *SessionBalance) Start() error {
}

func (sb *SessionBalance) markPromiseNotReceived() {
atomic.AddUint64(&sb.notReceivedPromiseCount, 1)
sb.notReceivedPromiseLock.Lock()
defer sb.notReceivedPromiseLock.Unlock()
sb.notReceivedPromiseCount++
}

func (sb *SessionBalance) resetNotReceivedPromiseCount() {
atomic.SwapUint64(&sb.notReceivedPromiseCount, 0)
sb.notReceivedPromiseLock.Lock()
defer sb.notReceivedPromiseLock.Unlock()
sb.notReceivedPromiseCount = 0
}

func (sb *SessionBalance) getNotReceivedPromiseCount() uint64 {
return atomic.LoadUint64(&sb.notReceivedPromiseCount)
sb.notReceivedPromiseLock.Lock()
defer sb.notReceivedPromiseLock.Unlock()
return sb.notReceivedPromiseCount
}

func (sb *SessionBalance) sendBalanceExpectPromise() error {
Expand Down
49 changes: 32 additions & 17 deletions session/pingpong/invoice_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"math/rand"
"strings"
"sync"
"sync/atomic"
"time"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -96,17 +95,21 @@ type sentInvoice struct {

// InvoiceTracker keeps tab of invoices and sends them to the consumer.
type InvoiceTracker struct {
stop chan struct{}
accountantFailureCount uint64
stop chan struct{}
accountantFailureCount uint64
accountantFailureCountLock sync.Mutex

notReceivedExchangeMessageCount uint64
maxNotReceivedExchangeMessages uint64
once sync.Once
agreementID uint64
lastExchangeMessage crypto.ExchangeMessage
transactorFee uint64
invoicesSent map[string]sentInvoice
invoiceLock sync.Mutex
deps InvoiceTrackerDeps
exchangeMessageCountLock sync.Mutex

maxNotReceivedExchangeMessages uint64
once sync.Once
agreementID uint64
lastExchangeMessage crypto.ExchangeMessage
transactorFee uint64
invoicesSent map[string]sentInvoice
invoiceLock sync.Mutex
deps InvoiceTrackerDeps
}

// InvoiceTrackerDeps contains all the deps needed for invoice tracker.
Expand Down Expand Up @@ -373,15 +376,21 @@ func (it *InvoiceTracker) Start() error {
}

func (it *InvoiceTracker) markExchangeMessageNotReceived() {
atomic.AddUint64(&it.notReceivedExchangeMessageCount, 1)
it.exchangeMessageCountLock.Lock()
defer it.exchangeMessageCountLock.Unlock()
it.notReceivedExchangeMessageCount++
}

func (it *InvoiceTracker) resetNotReceivedExchangeMessageCount() {
atomic.SwapUint64(&it.notReceivedExchangeMessageCount, 0)
it.exchangeMessageCountLock.Lock()
defer it.exchangeMessageCountLock.Unlock()
it.notReceivedExchangeMessageCount = 0
}

func (it *InvoiceTracker) getNotReceivedExchangeMessageCount() uint64 {
return atomic.LoadUint64(&it.notReceivedExchangeMessageCount)
it.exchangeMessageCountLock.Lock()
defer it.exchangeMessageCountLock.Unlock()
return it.notReceivedExchangeMessageCount
}

func (it *InvoiceTracker) generateR() []byte {
Expand Down Expand Up @@ -446,15 +455,21 @@ func (it *InvoiceTracker) waitForInvoicePayment(hlock []byte) {
}

func (it *InvoiceTracker) incrementAccountantFailureCount() {
atomic.AddUint64(&it.accountantFailureCount, 1)
it.accountantFailureCountLock.Lock()
defer it.accountantFailureCountLock.Unlock()
it.accountantFailureCount++
}

func (it *InvoiceTracker) resetAccountantFailureCount() {
atomic.SwapUint64(&it.accountantFailureCount, 0)
it.accountantFailureCountLock.Lock()
defer it.accountantFailureCountLock.Unlock()
it.accountantFailureCount = 0
}

func (it *InvoiceTracker) getAccountantFailureCount() uint64 {
return atomic.LoadUint64(&it.accountantFailureCount)
it.accountantFailureCountLock.Lock()
defer it.accountantFailureCountLock.Unlock()
return it.accountantFailureCount
}

func (it *InvoiceTracker) validateExchangeMessage(em crypto.ExchangeMessage) error {
Expand Down

0 comments on commit a601203

Please sign in to comment.