Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix race in paych manager add funds #4597

Merged
merged 1 commit into from
Oct 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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