Skip to content

Commit

Permalink
lessor: fix le.itemMap leak after lease is revoked
Browse files Browse the repository at this point in the history
Signed-off-by: qsyqian <qsyqian@gmail.com>
Co-authored-by: Thomas Jungblut <tjungblu@redhat.com>
  • Loading branch information
qsyqian and tjungblu committed Jun 12, 2023
1 parent a708bed commit 883fb38
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions server/lease/lessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,9 @@ func (le *lessor) Detach(id LeaseID, items []LeaseItem) error {

l := le.leaseMap[id]
if l == nil {
for _, it := range items {
delete(le.itemMap, it)
}
return ErrLeaseNotFound
}

Expand Down
37 changes: 37 additions & 0 deletions server/lease/lessor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"time"

"github.com/coreos/go-semver/semver"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"go.uber.org/zap/zaptest"

Expand Down Expand Up @@ -208,6 +209,42 @@ func TestLessorRevoke(t *testing.T) {
}
}

func TestLessorGrantAttachRevokeDetach(t *testing.T) {
lg := zap.NewNop()
_, be := NewTestBackend(t)
defer be.Close()

le := newLessor(lg, be, clusterLatest(), LessorConfig{MinLeaseTTL: minLeaseTTL})
defer le.Stop()
le.SetRangeDeleter(func() TxnDelete { return newFakeDeleter(be) })

// grant a lease with long term (100 seconds) to
// avoid early termination during the test.
l, err := le.Grant(1, 100)
if err != nil {
t.Fatalf("could not grant lease for 100s ttl (%v)", err)
}

items := []LeaseItem{
{"foo"},
{"bar"},
}

if err := le.Attach(l.ID, items); err != nil {
t.Fatalf("failed to attach items to the lease: %v", err)
}

if err := le.Revoke(l.ID); err != nil {
t.Fatalf("failed to revoke lease: %v", err)
}

// Detach items because fakeDeleter.DeleteRange will not call le.Detach.
err = le.Detach(l.ID, items)
assert.Equal(t, ErrLeaseNotFound, err)
assert.Equal(t, 0, len(le.leaseMap))
assert.Equal(t, 0, len(le.itemMap))
}

func renew(t *testing.T, le *lessor, id LeaseID) int64 {
ch := make(chan int64, 1)
errch := make(chan error, 1)
Expand Down

0 comments on commit 883fb38

Please sign in to comment.