Fix memory leak in liblinear - #34256
Merged
Merged
Conversation
thomasjpfan
approved these changes
Jun 11, 2026
thomasjpfan
left a comment
Member
There was a problem hiding this comment.
Codewise, I think this is a valid fix to a leak.
glemaitre
approved these changes
Jun 11, 2026
Member
|
I confirm this fixes the leak from the reproducer on my box as well and the diff LGTM as well. Let me add a changelog entry. |
ogrisel
approved these changes
Jun 12, 2026
Member
|
@jakevdp This fix is very welcome. May I ask how the bug was discovered? Was it on a particular use case? |
Member
Author
|
I was looking for the root cause of an unrelated memory leak outside scikit-learn, in code that uses scikit-learn, and the memory sanitizer caught this issue. It wasn’t the root cause of the issue I was debugging, but I figured I’d send a fix |
Contributor
prady0t
pushed a commit
to prady0t/scikit-learn
that referenced
this pull request
Sep 2, 2026
Co-authored-by: Olivier Grisel <[email protected]>
jeremiedbb
pushed a commit
that referenced
this pull request
Sep 10, 2026
Co-authored-by: Olivier Grisel <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
There is a memory leak in
LinearSVR, as demonstrated by this snippet (run on sklearn v1.9.0, but the issue predates this):Output:
Running under MSAN identified that the problem comes from memory allocated here:
scikit-learn/sklearn/svm/src/liblinear/linear.cpp
Lines 2433 to 2437 in 87fb943
via this line:
scikit-learn/sklearn/svm/src/liblinear/linear.cpp
Line 2456 in 87fb943
but the memory is only freed in the
elsebranch later in the function:scikit-learn/sklearn/svm/src/liblinear/linear.cpp
Lines 2590 to 2592 in 87fb943
The fix is straightforward: move these
freelines outside theelsebranch, so that they're executed unconditionally.AI usage disclosure
I used AI to research and understand the issue, but wrote the code in this PR manually.