Repository: etcd-io/etcd
File: server/etcdserver/v3_server.go (Go)

Code before:
		if !errorspkg.Is(err, lease.ErrNotPrimary) {
			return -1, err
		}
	}

	cctx, cancel := context.WithTimeout(ctx, s.Cfg.ReqTimeout())
	defer cancel()

	// renewals don't go through raft; forward to leader manually
	for cctx.Err() == nil {
		leader, lerr := s.waitLeader(cctx)
		if lerr != nil {
			return -1, lerr
		}
		for _, url := range leader.PeerURLs {
			lurl := url + leasehttp.LeasePrefix
			ttl, err := leasehttp.RenewHTTP(cctx, id, lurl, s.peerRt)
			if err == nil || errorspkg.Is(err, lease.ErrLeaseNotFound) {
				return ttl, err
			}
		}
		// Throttle in case of e.g. connection problems.
		select {
		case <-time.After(50 * time.Millisecond):
		case <-cctx.Done():
		}
	}

	err := cctx.Err()
	switch {
	case errorspkg.Is(err, context.DeadlineExceeded):
		return -1, errors.ErrTimeout
	case errorspkg.Is(err, context.Canceled):
		return -1, errors.ErrCanceled
	// Should be unreachable, but we keep it defensive.
	default:
		s.Logger().Warn("Unexpected lease renew context error", zap.Error(err))
		return -1, errors.ErrTimeout
	}
}

func (s *EtcdServer) checkLeaseTimeToLive(ctx context.Context, leaseID lease.LeaseID) (uint64, error) {
	rev := s.AuthStore().Revision()
	if !s.AuthStore().IsAuthEnabled() {
		return rev, nil
	}
	authInfo, err := s.AuthInfoFromCtx(ctx)
	if err != nil {
		return rev, err
	}
	if authInfo == nil {

Review comment (suggestion):
Regarding line 404, could we just keep it as it is right now? I don’t think it’s related to this fix.

1. time.Sleep(50 * time.Millisecond) is a short delay, and the loop will already break if any error occurs.

2. `cctx` is created using `context.WithTimeout`, not `WithCancelCause`, `WithDeadlineCause`, or `WithTimeoutCause`. If I understand correctly, the error should only be `context.DeadlineExceeded` or `context.Canceled`.

Code after:
		if !errorspkg.Is(err, lease.ErrNotPrimary) {
			return -1, err
		}
	}

	cctx, cancel := context.WithTimeout(ctx, s.Cfg.ReqTimeout())
	defer cancel()

	// renewals don't go through raft; forward to leader manually
	for cctx.Err() == nil {
		leader, lerr := s.waitLeader(cctx)
		if lerr != nil {
			return -1, lerr
		}
		for _, url := range leader.PeerURLs {
			lurl := url + leasehttp.LeasePrefix
			ttl, err := leasehttp.RenewHTTP(cctx, id, lurl, s.peerRt)
			if err == nil || errorspkg.Is(err, lease.ErrLeaseNotFound) {
				return ttl, err
			}
		}
		// Throttle in case of e.g. connection problems.
		time.Sleep(50 * time.Millisecond)
	}

	err := cctx.Err()
	switch {
	case errorspkg.Is(err, context.DeadlineExceeded):
		return -1, errors.ErrTimeout
	case errorspkg.Is(err, context.Canceled):
		return -1, errors.ErrCanceled
	default:
		s.Logger().Warn("Unexpected lease renew context error", zap.Error(err))
		return -1, errors.ErrCanceled
	}
}

func (s *EtcdServer) checkLeaseTimeToLive(ctx context.Context, leaseID lease.LeaseID) (uint64, error) {
	rev := s.AuthStore().Revision()
	if !s.AuthStore().IsAuthEnabled() {
		return rev, nil
	}
	authInfo, err := s.AuthInfoFromCtx(ctx)
	if err != nil {
		return rev, err
	}
	if authInfo == nil {
		return rev, auth.ErrUserEmpty
	}

	l := s.lessor.Lookup(leaseID)