Skip to content

test: destroy the Qt widgets a test closes in that test's teardown - #870

Open
hongquanli wants to merge 2 commits into
masterfrom
test/destroy-closed-qt-widgets-at-teardown
Open

hongquanli wants to merge 2 commits into
masterfrom
test/destroy-closed-qt-widgets-at-teardown

Conversation

@hongquanli

@hongquanli hongquanli commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

What fails

Ubuntu 24.04 (2/3) segfaults in pytest-qt's processEvents, in the teardown of the first test of tests/control/test_intensity_calibration_dialog.py. It happened on #869 twice in a row and on #851 once. The 22.04 runners run the same files in shard 2 and pass.

The crash dump shows one other thread, a Microcontroller read thread. That thread is the dialog test's own controller: pytest-qt processes events before fixtures are torn down. No thread from an earlier test survives; this was checked after every file, in that shard's order.

What does survive is Qt widgets:

  • pytest-qt closes each widget registered with qtbot.addWidget and calls deleteLater(). But processEvents() outside a running event loop never runs a DeferredDelete.
  • An ImageDisplayWindow's C++ object also outlives its Python wrapper.
  • So test_image_display_window.py leaves about 40 windows alive, some 500 top-level widgets counting pyqtgraph scenes and menus.
  • They are destroyed whenever a later test first runs an event loop, or when the garbage collector runs. That happens inside test_intensity_calibration_dialog.py, the next file in shard 2 on master and on fix(fluidics): update the fluidics library to Squid-Fluidics main (fluidics saves work on Windows) #869.

Fix

tests/conftest.py gets destroy_closed_qt_widgets. At the end of every test that uses qtbot, it runs the pending deletes, then gc.collect() for wrappers held in reference cycles.

  • test_destroying_the_gui_neither_crashes_nor_leaves_timers_firing_at_deleted_widgets already runs the deletes the same way.
  • cleanup_leaked_hardware requests the new fixture, so the widgets are destroyed after any leaked hardware is closed.

The two full-GUI files keep the old teardown. test_HighContentScreeningGui.py and test_objective_swap_gui.py run in processes of their own (CI's gui job), so they cannot leak widgets into a shard. They are marked qt_widgets_kept_until_exit and keep master's teardown.

  • With the destroy applied there, the 22.04 gui job segfaulted once, in test_home_stage_proceeds_with_only_the_host_busy_flag_set.
  • Locally that file then failed 3 of 7 runs under heavy load; master failed 0 of 5.
  • The cause of that crash is not known.
  • test_theme_menu.py builds the full GUI inside a shard and keeps the destroy.

Test

tests/test_qt_widget_teardown.py:

  • The first test leaves two widgets to pytest-qt: one held by a reference, one held only in a reference cycle.
  • The second test checks that both are gone.

On master it fails (DID NOT RAISE RuntimeError). Without the gc.collect(), the cycle check fails.

Checked locally (macOS)

  • Shard 2/3's files in CI order, up to and including test_intensity_calibration_dialog.py: 645 passed, 2 skipped (Python 3.12, PyQt6 6.11). After test_image_display_window.py, 0 top-level widgets are left; before the fix it was about 500.
  • The 58 files that use qtbot give the same results as on master.
  • test_HighContentScreeningGui.py: 53 passed. test_objective_swap_gui.py: 1 passed.
  • Cost: one full garbage collection per qtbot test, about 80 ms of CPU each here. The suite has 868 such tests.

Not verified

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Tests
    • Added coverage for Qt widget cleanup during test teardown, including deferred deletions and cyclic references.
    • Verified that hardware is closed before widget cleanup, helping detect teardown issues in automated tests.
    • Added coverage for tests that keep Qt widgets available until process exit, ensuring their teardown behavior remains distinct.

pytest-qt closes each widget registered with qtbot.addWidget and calls
deleteLater() on it, but processEvents() outside a running event loop never
runs a DeferredDelete. The widget lived on until a later test ran an event
loop and was destroyed inside that test. test_image_display_window.py left
about 40 ImageDisplayWindows (some 500 top-level widgets) to the next file in
CI's shard 2/3, test_intensity_calibration_dialog.py, where Ubuntu 24.04's
shard segfaulted in pytest-qt's processEvents (twice on #869, once on #851).

tests/conftest.py now runs the pending deletes, then a garbage collection, at
the end of every test that uses qtbot, after the leaked hardware is closed.
test_qt_widget_teardown.py checks both: a closed widget is deleted, and a
wrapper held in a reference cycle is freed, by the end of its test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Test teardown now processes deferred Qt widget deletions and runs garbage collection after hardware cleanup. A pytest marker exempts selected tests from widget destruction. New tests check widget deletion, collection of a cyclic reference, and widget retention.

Changes: Qt widget teardown

Layer / File(s) Summary
Qt teardown fixture and retention marker
software/tests/conftest.py, software/pyproject.toml, software/tests/control/test_HighContentScreeningGui.py, software/tests/control/test_objective_swap_gui.py
Teardown processes deferred-delete events and runs garbage collection after hardware cleanup for tests using qtbot, unless they use the qt_widgets_kept_until_exit marker. Two GUI test modules use that marker.
Widget teardown checks
software/tests/test_qt_widget_teardown.py
Ordered tests check that unmarked widgets are deleted and a cyclic reference is collected. Separate tests check that a widget in a marked test remains accessible.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Other

Possibly related PRs

  • Cephla-Lab/Squid#750: Addresses a Qt widget-lifetime crash in GUI code. This pull request changes test teardown for a related deferred-deletion failure path.

Suggested reviewers: alpaca233

Merge Risk: Minimal · up to 0f588

This change only affects test teardown. It makes test runs cleaner by destroying leftover Qt widgets after each test, with an opt-out for the two full-GUI test files. It has no production impact, and no merge-blocking concerns were found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "test: destroy the Qt widgets a test closes in that test's teardown" matches the main change. The new conftest.py fixture destroys closed Qt widgets at teardown of tests that use qtbot, unle…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "test: destroy the Qt widgets a test closes in that test's teardown" matches the main change. The new conftest.py fixture destroys closed Qt widgets at teardown of tests that use qtbot, unless they carry the qt_widgets_kept_until_exit marker. The title is specific and describes the primary change. The later commit that excludes the two full-GUI files is a narrower detail and does not change the title's accuracy.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Team
  • Run ID: 3b01c9c6-1f41-4418-b7d2-f7481fe02ab9
Commits

Reviewing files that changed from the base of the PR and between ca4efaf and 0f588d3.

Files selected for processing: 5

  • software/pyproject.toml
  • software/tests/conftest.py
  • software/tests/control/test_HighContentScreeningGui.py
  • software/tests/control/test_objective_swap_gui.py
  • software/tests/test_qt_widget_teardown.py

Included review availability: This review used your included allowance. 2 included reviews remain after this review. Your included PR review attempts over the past 7 days set your current allowance at 4 reviews per hour.

With the per-test destroy, CI's gui job segfaulted on Ubuntu 22.04 inside
test_home_stage_proceeds_with_only_the_host_busy_flag_set of
test_HighContentScreeningGui.py (the main thread in that test's waitUntil,
its own homing thread waiting on the controller; no thread of an earlier test
was alive). It did not reproduce locally.

The two full-GUI files run in processes of their own in CI's gui job, so
nothing they leave can reach another file, and the destroy buys them
nothing. They are marked qt_widgets_kept_until_exit and keep the teardown
their tests were written against: windows live until a later test's event
loop destroys them. Files in the shards keep the destroy, including
test_theme_menu.py, which builds the full GUI too.

test_qt_widget_teardown.py checks that a marked test's widget survives.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.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