Skip to content

Commit

Permalink
Merge pull request #4597 from filecoin-project/fix/paych-mgr-add-fund…
Browse files Browse the repository at this point in the history
…s-race

fix race in paych manager add funds
  • Loading branch information
magik6k authored Oct 26, 2020
2 parents ce9cce9 + 5ed57d3 commit 43b4922
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
5 changes: 4 additions & 1 deletion paychmgr/paychget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,7 @@ func TestPaychGetMergeAddFunds(t *testing.T) {
defer addFundsSent.Done()

// Request add funds - should block until create channel has completed
var err error
addFundsCh1, addFundsMcid1, err = mgr.GetPaych(ctx, from, to, addFundsAmt1)
require.NoError(t, err)
}()
Expand All @@ -671,6 +672,7 @@ func TestPaychGetMergeAddFunds(t *testing.T) {
defer addFundsSent.Done()

// Request add funds again - should merge with waiting add funds request
var err error
addFundsCh2, addFundsMcid2, err = mgr.GetPaych(ctx, from, to, addFundsAmt2)
require.NoError(t, err)
}()
Expand Down Expand Up @@ -766,6 +768,7 @@ func TestPaychGetMergeAddFundsCtxCancelOne(t *testing.T) {
defer addFundsSent.Done()

// Request add funds again - should merge with waiting add funds request
var err error
addFundsCh2, addFundsMcid2, err = mgr.GetPaych(ctx, from, to, addFundsAmt2)
require.NoError(t, err)
}()
Expand Down Expand Up @@ -861,7 +864,6 @@ func TestPaychGetMergeAddFundsCtxCancelAll(t *testing.T) {

// Request add funds again - should merge with waiting add funds request
_, _, addFundsErr2 = mgr.GetPaych(addFundsCtx2, from, to, big.NewInt(3))
require.NoError(t, err)
}()
// Wait for add funds requests to be queued up
waitForQueueSize(t, mgr, from, to, 2)
Expand Down Expand Up @@ -950,6 +952,7 @@ func TestPaychAvailableFunds(t *testing.T) {
defer addFundsSent.Done()

// Request add funds - should block until create channel has completed
var err error
_, addFundsMcid, err = mgr.GetPaych(ctx, from, to, addFundsAmt)
require.NoError(t, err)
}()
Expand Down
7 changes: 5 additions & 2 deletions paychmgr/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,13 @@ type mergedFundsReq struct {

func newMergedFundsReq(reqs []*fundsReq) *mergedFundsReq {
ctx, cancel := context.WithCancel(context.Background())

rqs := make([]*fundsReq, len(reqs))
copy(rqs, reqs)
m := &mergedFundsReq{
ctx: ctx,
cancel: cancel,
reqs: reqs,
reqs: rqs,
}

for _, r := range m.reqs {
Expand Down Expand Up @@ -201,7 +204,7 @@ func (ca *channelAccessor) processQueue(channelID string) (*api.ChannelAvailable
// Merge all pending requests into one.
// For example if there are pending requests for 3, 2, 4 then
// amt = 3 + 2 + 4 = 9
merged := newMergedFundsReq(ca.fundsReqQueue[:])
merged := newMergedFundsReq(ca.fundsReqQueue)
amt := merged.sum()
if amt.IsZero() {
// Note: The amount can be zero if requests are cancelled as we're
Expand Down

0 comments on commit 43b4922

Please sign in to comment.