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 runtime error: comparing uncomparable type #18893

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 9 additions & 4 deletions client/v3/lease.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,11 @@ func (l *lessor) KeepAlive(ctx context.Context, id LeaseID) (<-chan *LeaseKeepAl
default:
}
ka, ok := l.keepAlives[id]

if ctx.Done() != nil {
// mark this context for later retrieval
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// mark this context for later retrieval

ctx = context.WithValue(ctx, &ctx, struct{}{})
}
if !ok {
// create fresh keep alive
ka = &keepAlive{
Expand All @@ -296,7 +301,7 @@ func (l *lessor) KeepAlive(ctx context.Context, id LeaseID) (<-chan *LeaseKeepAl
l.mu.Unlock()

if ctx.Done() != nil {
go l.keepAliveCtxCloser(ctx, id, ka.donec)
go l.keepAliveCtxCloser(&ctx, id, ka.donec)
}
l.firstKeepAliveOnce.Do(func() {
go l.recvKeepAliveLoop()
Expand Down Expand Up @@ -329,13 +334,13 @@ func (l *lessor) Close() error {
return nil
}

func (l *lessor) keepAliveCtxCloser(ctx context.Context, id LeaseID, donec <-chan struct{}) {
func (l *lessor) keepAliveCtxCloser(ctx *context.Context, id LeaseID, donec <-chan struct{}) {
select {
case <-donec:
return
case <-l.donec:
return
case <-ctx.Done():
case <-(*ctx).Done():
}

l.mu.Lock()
Expand All @@ -348,7 +353,7 @@ func (l *lessor) keepAliveCtxCloser(ctx context.Context, id LeaseID, donec <-cha

// close channel and remove context if still associated with keep alive
for i, c := range ka.ctxs {
if c == ctx {
if val := c.Value(ctx); val != nil {
ktalg marked this conversation as resolved.
Show resolved Hide resolved
close(ka.chs[i])
ka.ctxs = append(ka.ctxs[:i], ka.ctxs[i+1:]...)
ka.chs = append(ka.chs[:i], ka.chs[i+1:]...)
Expand Down
15 changes: 14 additions & 1 deletion tests/integration/clientv3/lease/lease_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,13 @@ func TestLeaseKeepAlive(t *testing.T) {
t.Errorf("failed to create lease %v", err)
}

rc, kerr := lapi.KeepAlive(context.Background(), resp.ID)
type uncomparableCtx struct {
context.Context
_ func()
}

ctx, cancel := context.WithCancel(context.Background())
rc, kerr := lapi.KeepAlive(uncomparableCtx{Context: ctx}, resp.ID)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The easiest workaround is to pass in a pointer &uncomparableCtx{Context: ctx}.

if kerr != nil {
t.Errorf("failed to keepalive lease %v", kerr)
}
Expand All @@ -150,6 +156,13 @@ func TestLeaseKeepAlive(t *testing.T) {
t.Fatalf("unexpected null response")
}

cancel()

_, ok = <-rc
if ok {
t.Errorf("chan is not closed, want cancel stop keepalive")
}

if kresp.ID != resp.ID {
t.Errorf("ID = %x, want %x", kresp.ID, resp.ID)
}
Expand Down