Skip to content

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

Merged
vstinner merged 4 commits intopython:mainfrom
raminfp:fix-lsprof-clearentries-memleak
Apr 7, 2026
Merged

gh-145846: Fix memory leak in _lsprof clearEntries() context chain#145847
vstinner merged 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.

Comment thread Lib/test/test_profiling/test_tracing_profiler.py Outdated
Comment thread Modules/_lsprof.c Outdated
- 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.

@vstinner vstinner merged commit 957b2cc into python:main Apr 7, 2026
51 checks passed
@vstinner
Copy link
Copy Markdown
Member

vstinner commented Apr 7, 2026

Merged, thanks for the fix!

@raminfp raminfp deleted the fix-lsprof-clearentries-memleak branch April 8, 2026 02:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants