Skip to content

gh-145846: Fix memory leak in _lsprof clearEntries() context chain#145847

Open
raminfp wants to merge 4 commits intopython:mainfrom
raminfp:fix-lsprof-clearentries-memleak
Open

gh-145846: Fix memory leak in _lsprof clearEntries() context chain#145847
raminfp wants to merge 4 commits intopython:mainfrom
raminfp:fix-lsprof-clearentries-memleak

Conversation

@raminfp
Copy link
Copy Markdown
Contributor

@raminfp raminfp commented Mar 12, 2026

Fix clearEntries() to walk the entire currentProfilerContext linked list instead of only freeing the top context, preventing memory leaks when clear() is called during nested profiled calls.

clearEntries() only freed the top currentProfilerContext but did not
walk the previous linked list. When clear() is called during active
profiling with nested calls, all contexts except the top one were
leaked. Fix by iterating the entire linked list, matching the existing
freelistProfilerContext cleanup pattern.
Copy link
Copy Markdown
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

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

LGTM.

I confirm that ./python -m test test_profiling.test_tracing_profiler -m test_clear_with_nested_calls -R 3:3 leaks without the fix, and doesn't leak with the fix.

- Rename 'c' to 'pContext' in clearEntries() loop for consistency
- Rename 'p' to 'profiler' in test_clear_with_nested_calls()
@raminfp
Copy link
Copy Markdown
Contributor Author

raminfp commented Mar 27, 2026

@vstinner Thanks for review, done!

@vstinner
Copy link
Copy Markdown
Member

"Tests / All required checks pass" fails with: Error: The template is not valid. .github/workflows/build.yml (Line: 679, Col: 24): Error reading JToken from JsonReader. Path '', line 0, position 0.

Let me try to [Update branch] to see if it repairs the CI.

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.

2 participants