Skip to content

Add sweep registry to prevent GC of ERROR sweeps and enable discovery#29

Closed
Copilot wants to merge 4 commits intomasterfrom
copilot/add-sweep-registry-functionality
Closed

Add sweep registry to prevent GC of ERROR sweeps and enable discovery#29
Copilot wants to merge 4 commits intomasterfrom
copilot/add-sweep-registry-functionality

Conversation

Copy link
Contributor

Copilot AI commented Jan 26, 2026

ERROR state sweeps are currently lost if the user reference is deleted, may be GC'd before inspection, and reference cycles prevent cleanup of KILLED sweeps.

Changes

Registry System

  • WeakValueDictionary tracks all sweep instances (allows normal GC)
  • set holds strong references to ERROR sweeps (prevents GC until explicit release)
  • Thread-safe with threading.Lock on all registry operations

Lifecycle Management

  • mark_error() adds sweep to error hold
  • kill() and clear_error() remove from error hold
  • kill() calls new clear_sweep_ref() methods on runner/plotter to break cycles

Public API

from measureit import get_error_sweeps, get_all_sweeps

# ERROR sweeps remain findable even after del
s = Sweep1D(...)
s.start()
# ... error occurs
del s

# Still accessible
for sweep in get_error_sweeps():
    print(sweep.progressState.error_message)
    sweep.kill()  # Now GC-eligible

Reference Cycle Breaking

  • Added clear_sweep_ref() to RunnerThread and Plotter
  • Called in kill() before nulling references
  • Breaks Sweep ↔ Runner ↔ Plotter cycles

Implementation Details

  • _next_id counter assigns unique IDs during __init__
  • get_error_sweeps() returns directly from error_hold (O(1))
  • _clear_registry_for_testing() helper for test isolation
  • Fully backward compatible - zero impact on existing code
Original prompt

This section details on the original issue you should resolve

<issue_title>Sweep GC: ERROR sweeps should be held and findable via registry</issue_title>
<issue_description>## Problem

Currently, sweep instances in ERROR state:

  1. Cannot be found if the user loses their reference
  2. May be garbage collected before the user can inspect them
  3. Have reference cycles (Sweep ↔ Runner ↔ Plotter) that prevent proper GC even for KILLED sweeps

Expected Behavior

  1. ERROR state sweeps should be held in memory (prevent GC) until explicitly kill()ed or clear_error()ed
  2. A sweep registry should allow finding sweeps by state: get_error_sweeps(), get_all_sweeps()
  3. KILLED sweeps should be properly garbage collected (reference cycles should be broken)

Proposed Solution

1. Add Sweep Registry

import weakref

class BaseSweep(QObject):
    _registry = weakref.WeakValueDictionary()  # All sweeps (weak refs)
    _error_hold = set()  # Strong refs for ERROR sweeps (prevents GC)
    _next_id = 0

    @classmethod
    def get_all_sweeps(cls):
        return list(cls._registry.values())

    @classmethod
    def get_error_sweeps(cls):
        return [s for s in cls._registry.values()
                if s.progressState.state == SweepState.ERROR]

2. Hold ERROR sweeps

In mark_error():

BaseSweep._error_hold.add(self)  # Prevent GC

In kill() and clear_error():

BaseSweep._error_hold.discard(self)  # Allow GC

3. Break reference cycles

Add clear_sweep_ref() to RunnerThread and Plotter, call it in kill() before setting to None.

Files to Modify

  • src/measureit/sweep/base_sweep.py
  • src/measureit/_internal/runner_thread.py
  • src/measureit/_internal/plotter_thread.py
  • src/measureit/__init__.py

Usage After Implementation

from measureit import Sweep1D, get_error_sweeps

s = Sweep1D(...)
s.start()
# ... error occurs, reference lost ...
del s

# Can still find it:
for sweep in get_error_sweeps():
    print(sweep.progressState.error_message)
    sweep.kill()  # Now it can be GC'd

See full plan: .claude/plans/pure-tumbling-cherny.md</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 3 commits January 26, 2026 00:53
Co-authored-by: caidish <16291224+caidish@users.noreply.github.com>
Co-authored-by: caidish <16291224+caidish@users.noreply.github.com>
… helper

Co-authored-by: caidish <16291224+caidish@users.noreply.github.com>
Copilot AI changed the title [WIP] Add sweep registry to manage ERROR state sweeps Add sweep registry to prevent GC of ERROR sweeps and enable discovery Jan 26, 2026
Copilot AI requested a review from caidish January 26, 2026 01:06
@caidish caidish closed this Jan 28, 2026
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.

Sweep GC: ERROR sweeps should be held and findable via registry

2 participants