fix(race): again

Signed-off-by: Marcel Fest <marcel.fest@telekom.de>
This commit is contained in:
Marcel Fest
2026-08-18 17:20:24 +02:00
parent 048dedf84a
commit 675e3d7213
2 changed files with 10 additions and 2 deletions

View File

@@ -290,8 +290,8 @@ func (p *Processor) startLeaderElection(svcCtx *servicecontext.Context, service
default:
leaseNamespace, serviceLease := lease.ServiceName(service)
id := lease.NewID(p.config.LeaderElectionType, leaseNamespace, serviceLease)
// The lease is only dropped once its last service is gone, which races
// with this loop noticing its own service context is done.
// The lease is retired once its last service is gone, so an absent one means
// this loop has nothing left to elect for.
l := p.leaseMgr.Get(id)
if l == nil {
return

View File

@@ -46,6 +46,14 @@ func (p *Processor) StartServicesLeaderElection(svcCtx *servicecontext.Context,
return fmt.Errorf("no existing lease found for service %q with UID %q", service.Name, service.UID)
}
// A cancelled service context means this call belongs to a torn-down incarnation of
// the service. Its replacement is built as Cancel -> Delete -> Add, so the lease
// fetched above may already be the replacement's. Registering on it here would let
// the cleanup goroutine below retire a lease that is still in use.
if err := svcCtx.Ctx.Err(); err != nil {
return fmt.Errorf("service context cancelled before election start: %w", err)
}
isNew := svcLease.Add(objectName)
svcLease.Lock()