Skip to content

SelectiveSampleService uses a mutable default argument #73

Description

@sequoiap

pyrolab/drivers/sample.py:192:

class SelectiveSampleService(Service):
    def __init__(self, items: List[Any] = []) -> None:
        self._items = items

The default list is created once at function definition and shared by every instance that does not pass items. sort(), set_item() and the items setter all mutate it, so state leaks between instances.

This matters more than usual here because the class is a hosted service: under instance_mode="session" each client connection gets its own instance, all of which share the one default list; under "single" it is shared by definition. Either way, one client's set_item() is visible to the next.

It is also a sample service -- it is what users read when writing their own drivers, so the pattern propagates.

Fix:

def __init__(self, items: Optional[List[Any]] = None) -> None:
    self._items = list(items) if items is not None else []

Found in a full-codebase audit at v0.4.0 (commit 1ce3146).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions