Skip to content

Global State in Widget Manager is not thread safe #149

@hannesdelbeke

Description

@hannesdelbeke

⚠️ Global State in Widget Manager

[!warning] Thread Safety Issue
Module-level lists for widget tracking are not thread-safe and hard to test in isolation.

Current Issue:

# manager.py
__widgets = []  # Module-level global
__excluded_widgets = []

Problems:

  • Not thread-safe (Qt can be multi-threaded)
  • Hard to test in isolation
  • No clear ownership or lifecycle
  • Implicit coupling

[!example] Recommended Solution
Refactor to singleton or instance-managed registry

class WidgetRegistry:
    """Thread-safe widget registry with clear lifecycle"""
    _instance = None

    def __new__(cls):
        if cls._instance is None:
            cls._instance = super().__new__(cls)
            cls._instance._widgets = []
            cls._instance._excluded = []
            cls._instance._lock = threading.Lock()
        return cls._instance

    def register(self, widget, **kwargs):
        with self._lock:
            # ... safe registration

# Usage
registry = WidgetRegistry()
registry.register(my_widget)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions