fix: OOM, potential goroutine leaks#2138
Conversation
…he context is killed), buffered channel to avoid deadlocks unit tests + goleak installation
…eating 1 timer instance, handling routin exit when context is done
There was a problem hiding this comment.
Add a nil check in line 656
if holder != nil { // Nil check for safety
holder.CancelFunc()
}
There was a problem hiding this comment.
Replace magic numbers with constants. e.g.
const (
MaxSubscriptionMapSizeWarningThreshold = 5000
SubscriptionCleanupInterval = 1 * time.Minute
PairingInitializationTimeout = 30 * time.Second
PairingCheckInterval = 1 * time.Second
ProbeScatterMaxDelayMs = 500
)
There was a problem hiding this comment.
In cleanupStaleSubscriptions we hold the lock while iterating through all the subscriptions. Consider the following approach
rpcss.connectedSubscriptionsLock.Lock()
// Create a snapshot of holders to check
holdersToCheck := make(map[string]*subscriptionHolder, len(rpcss.connectedSubscriptionsContexts))
for key, holder := range rpcss.connectedSubscriptionsContexts {
holdersToCheck[key] = holder
}
rpcss.connectedSubscriptionsLock.Unlock()
// Check contexts WITHOUT holding the lock
staleKeys := []string{}
for key, holder := range holdersToCheck {
select {
case <-holder.Ctx.Done():
staleKeys = append(staleKeys, key)
default:
}
}
// Re-acquire lock only for deletion
rpcss.connectedSubscriptionsLock.Lock()
for _, key := range staleKeys {
if holder, exists := rpcss.connectedSubscriptionsContexts[key]; exists {
holder.CancelFunc()
delete(rpcss.connectedSubscriptionsContexts, key)
}
}
currentSize := len(rpcss.connectedSubscriptionsContexts)
rpcss.connectedSubscriptionsLock.Unlock()
…anet/lava into unified-memory-leaks-fixes-2
Description
Closes: #XXXX
Author Checklist
All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.
I have...
!in the type prefix if API or client breaking changemainbranchReviewers Checklist
All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.
I have...