Skip to content

Commit cd0ed1d

Browse files
committed
fix: safe operations for uint64 conversions
1 parent c2a371c commit cd0ed1d

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

pkg/activator/net/lb_policy.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,11 @@ func leastConnectionsPolicy(ctx context.Context, targets []*podTracker) (func(),
176176
for i, t := range targets {
177177
if t != nil {
178178
// Use the weight field as a proxy for in-flight connections
179-
trackerLoads[i] = TrackerLoad{tracker: t, inFlight: uint64(t.weight.Load())}
179+
weight := t.weight.Load()
180+
if weight < 0 {
181+
weight = 0
182+
}
183+
trackerLoads[i] = TrackerLoad{tracker: t, inFlight: uint64(weight)}
180184
}
181185
}
182186
sort.Slice(trackerLoads, func(i, j int) bool {

pkg/activator/net/throttler.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ func newRevisionThrottler(revID types.NamespacedName,
237237
protocol: proto,
238238
podTrackers: []*podTracker{},
239239
}
240+
if containerConcurrency < 0 {
241+
containerConcurrency = 0
242+
}
240243
t.containerConcurrency.Store(uint32(containerConcurrency))
241244
t.lbPolicy.Store(lbp)
242245

@@ -615,10 +618,14 @@ func (t *Throttler) revisionUpdated(obj any) {
615618
zap.Error(err), zap.String(logkey.Key, revID.String()))
616619
} else if rt != nil {
617620
// Update the lbPolicy dynamically if the revision's spec policy changed
618-
newPolicy, name := pickLBPolicy(rev.Spec.LoadBalancingPolicy, nil, int(rev.Spec.GetContainerConcurrency()), t.logger)
621+
containerConcurrency := rev.Spec.GetContainerConcurrency()
622+
if containerConcurrency < 0 {
623+
containerConcurrency = 0
624+
}
625+
newPolicy, name := pickLBPolicy(rev.Spec.LoadBalancingPolicy, nil, int(containerConcurrency), t.logger)
619626
// Use atomic store for lock-free access in the hot request path
620627
rt.lbPolicy.Store(newPolicy)
621-
rt.containerConcurrency.Store(uint32(rev.Spec.GetContainerConcurrency()))
628+
rt.containerConcurrency.Store(uint32(containerConcurrency))
622629
t.logger.Infof("Updated revision throttler LB policy to: %s", name)
623630
}
624631
}
@@ -692,12 +699,14 @@ func (rt *revisionThrottler) handlePubEpsUpdate(eps *corev1.Endpoints, selfIP st
692699
}
693700

694701
na, ai := rt.numActivators.Load(), rt.activatorIndex.Load()
695-
if na == uint32(newNA) && ai == newAI {
702+
if newNA >= 0 && na == uint32(newNA) && ai == newAI {
696703
// The state didn't change, do nothing
697704
return
698705
}
699706

700-
rt.numActivators.Store(uint32(newNA))
707+
if newNA >= 0 {
708+
rt.numActivators.Store(uint32(newNA))
709+
}
701710
rt.activatorIndex.Store(newAI)
702711
rt.logger.Infof("This activator index is %d/%d was %d/%d",
703712
newAI, newNA, ai, na)

0 commit comments

Comments
 (0)