Skip to content

fix: OOM, potential goroutine leaks#2138

Merged
nimrod-teich merged 11 commits into
mainfrom
unified-memory-leaks-fixes-2
Dec 8, 2025
Merged

fix: OOM, potential goroutine leaks#2138
nimrod-teich merged 11 commits into
mainfrom
unified-memory-leaks-fixes-2

Conversation

@Tomelia1999

Copy link
Copy Markdown
Contributor

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...

  • read the contribution guide
  • included the correct type prefix in the PR title, you can find examples of the prefixes below:
  • confirmed ! in the type prefix if API or client breaking change
  • targeted the main branch
  • provided a link to the relevant issue or specification
  • reviewed "Files changed" and left comments if necessary
  • included the necessary unit and integration tests
  • updated the relevant documentation or specification, including comments for documenting Go code
  • confirmed all CI checks have passed

Reviewers 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...

  • confirmed the correct type prefix in the PR title
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic, API design and naming, documentation is accurate, tests and test coverage

@Tomelia1999 Tomelia1999 changed the title Unified memory leaks fixes 2 fix: OOM, potential goroutine leaks Dec 7, 2025
@github-actions

github-actions Bot commented Dec 7, 2025

Copy link
Copy Markdown

Test Results

    7 files  ± 0    131 suites  ±0   35m 29s ⏱️ + 13m 50s
3 148 tests +30  3 147 ✅ +31  1 💤 ±0  0 ❌ ±0 
3 228 runs  +30  3 227 ✅ +31  1 💤 ±0  0 ❌ ±0 

Results for commit d7d6321. ± Comparison against base commit 294407b.

♻️ This comment has been updated with latest results.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Add a nil check in line 656

if holder != nil { // Nil check for safety
holder.CancelFunc()
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Replace magic numbers with constants. e.g.

const (
MaxSubscriptionMapSizeWarningThreshold = 5000
SubscriptionCleanupInterval = 1 * time.Minute
PairingInitializationTimeout = 30 * time.Second
PairingCheckInterval = 1 * time.Second
ProbeScatterMaxDelayMs = 500
)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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()

@nimrod-teich nimrod-teich merged commit 1c23877 into main Dec 8, 2025
48 of 49 checks passed
@nimrod-teich nimrod-teich deleted the unified-memory-leaks-fixes-2 branch December 8, 2025 17:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants