Skip to content

Fix _FanovaTree instances never being garbage collected - #6864

Closed
DeepakJaggupalli wants to merge 1 commit into
optuna:masterfrom
DeepakJaggupalli:fix/fanova-tree-lru-cache-leak
Closed

DeepakJaggupalli wants to merge 1 commit into
optuna:masterfrom
DeepakJaggupalli:fix/fanova-tree-lru-cache-leak

Conversation

@DeepakJaggupalli

Copy link
Copy Markdown

Motivation

I ran a static analysis pass looking for objects that never get garbage collected and found that _FanovaTree (optuna/importance/_fanova/_tree.py) leaks every instance it ever creates, for the lifetime of the process.

_is_node_leaf, _get_node_left_child, _get_node_right_child, _get_node_children, _get_node_value, _get_node_split_threshold, and _get_node_split_feature are all decorated with @lru_cache(maxsize=None) directly on the instance method. functools.lru_cache's cache lives on the function object itself, which is shared by every instance of the class, and it keys on all positional arguments - including self. So every _FanovaTree that ever calls one of these methods (which __init__ does immediately, via _precompute_statistics() etc.) stays referenced by that cache forever, and can never be collected even after nothing else references it.

_FanovaTree instances are created fresh on every call to _Fanova.fit() - one per tree in the random forest, 64 by default - and FanovaImportanceEvaluator.evaluate() calls fit() on every invocation. So any code path that computes fANOVA parameter importances more than once in the same process (a dashboard, a notebook, a report regenerated in a loop) leaks the entire previous forest every time it runs.

I verified this directly: built a few _FanovaTree instances, exercised the cached methods, dropped all references, and confirmed via weakref that they survive gc.collect() on the current code - and that they're correctly collected after this fix.

Description of the changes

Replaced the class-level @lru_cache(maxsize=None) decorators on those 7 methods with per-instance dict caches, initialized in __init__. Each _FanovaTree's cache now lives and dies with the instance instead of outliving it. Semantics are otherwise unchanged - same memoization behavior, just scoped correctly.

I also confirmed the fix doesn't change any computed values: ran the same seeded RandomForestRegressor through both the original and fixed _FanovaTree and compared variance/get_marginal_variance output - bit-identical.

Added a regression test (test_tree_is_garbage_collected) that builds a _FanovaTree, exercises the cached methods, drops all references, and asserts via weakref that the instance is actually collected. It fails on the current code and passes with this fix.

Checklist

  • I used an LLM while working on this PR.

_FanovaTree's _is_node_leaf/_get_node_* helper methods are decorated
with @lru_cache(maxsize=None) directly on the instance method. Since
lru_cache's cache lives on the function object (shared by every
instance of the class) and keys on all positional arguments including
self, every _FanovaTree that ever called one of these methods stays
referenced by that cache for the lifetime of the process - it can
never be garbage collected, even after nothing else references it.

_FanovaTree instances are created fresh on every call to
_Fanova.fit() (one per tree in the random forest, default 64), which
FanovaImportanceEvaluator.evaluate() calls on every invocation. Any
code path that computes fANOVA parameter importances more than once
in the same process - a dashboard, a notebook, a report generated in
a loop - leaks the full previous forest every time.

Verified directly: building a few _FanovaTree instances, exercising
the cached methods (as __init__ already does), and confirming via
weakref that they survive gc.collect() on the current code, but are
correctly collected after this fix. Also confirmed the fix produces
bit-identical variance/importance values to before, by running the
same seeded RandomForestRegressor through both implementations.

Replaced the class-level lru_cache with per-instance dict caches so
each _FanovaTree's cache dies with the instance instead of outliving
it.
@not522

not522 commented Sep 25, 2026

Copy link
Copy Markdown
Member

Thank you. However, this can be resolved with a simpler implementation, so I am closing this PR.

@not522 not522 closed this Sep 25, 2026
@not522 not522 mentioned this pull request Oct 13, 2026
1 task done
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