Skip to content

Fix mutable default argument in get_resource_tree causing shared state across calls#2196

Open
prajakta128 wants to merge 1 commit into
aboutcode-org:mainfrom
prajakta128:fix/get-resource-tree-mutable-default-argument
Open

Fix mutable default argument in get_resource_tree causing shared state across calls#2196
prajakta128 wants to merge 1 commit into
aboutcode-org:mainfrom
prajakta128:fix/get-resource-tree-mutable-default-argument

Conversation

@prajakta128

Copy link
Copy Markdown
Contributor

What

Fix get_resource_tree in scanpipe/pipes/codebase.py using a mutable default argument seen_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_tree share the same seen_resources set 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.py that calls get_resource_tree twice with separate projects and verifies the second call returns a complete tree unaffected by the first call.

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>
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.

1 participant