Fix mutable default argument in get_resource_tree causing shared state across calls#2196
Open
prajakta128 wants to merge 1 commit into
Open
Conversation
Using seen_resources=set() as a default argument is a classic Python bug: the set is created once when the function is defined, not on each call. All calls share the same set object. This causes resources visited in one call to be silently skipped in subsequent calls. When multiple projects are processed in the same Python process, the second project's resource tree can be silently incomplete — missing resources already seen in the first. Fix: use seen_resources=None and initialize a fresh set() inside the function body when seen_resources is None. Add a regression test that calls get_resource_tree twice with separate projects to verify the seen_resources set is not shared between calls. Signed-off-by: Prajakta <your-email@gmail.com>
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.
What
Fix
get_resource_treeinscanpipe/pipes/codebase.pyusing a mutable default argumentseen_resources=set().Why
In Python, mutable default arguments are created once when the function is defined — not on each call. This means all calls to
get_resource_treeshare the sameseen_resourcesset object.Resources visited in the first call are silently skipped in all later calls. When multiple projects are processed in the same Python process, later projects get silently incomplete resource trees with no error or warning shown to the user.
Fix
Replace:
def get_resource_tree(resource, fields, codebase=None, seen_resources=set()):
With:
def get_resource_tree(resource, fields, codebase=None, seen_resources=None):
if seen_resources is None:
seen_resources = set()
This ensures a fresh set is created for each independent call.
Test added
Regression test in
scanpipe/tests/pipes/test_codebase.pythat callsget_resource_treetwice with separate projects and verifies the second call returns a complete tree unaffected by the first call.