Repository navigation
test: destroy the Qt widgets a test closes in that test's teardown - #870
hongquanli wants to merge 2 commits into
Conversation
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>
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
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>
What fails
Ubuntu 24.04 (2/3) segfaults in pytest-qt's
processEvents, in the teardown of the first test oftests/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
Microcontrollerread 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:
qtbot.addWidgetand callsdeleteLater(). ButprocessEvents()outside a running event loop never runs aDeferredDelete.ImageDisplayWindow's C++ object also outlives its Python wrapper.test_image_display_window.pyleaves about 40 windows alive, some 500 top-level widgets counting pyqtgraph scenes and menus.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.pygetsdestroy_closed_qt_widgets. At the end of every test that usesqtbot, it runs the pending deletes, thengc.collect()for wrappers held in reference cycles.test_destroying_the_gui_neither_crashes_nor_leaves_timers_firing_at_deleted_widgetsalready runs the deletes the same way.cleanup_leaked_hardwarerequests 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.pyandtest_objective_swap_gui.pyrun in processes of their own (CI's gui job), so they cannot leak widgets into a shard. They are markedqt_widgets_kept_until_exitand keep master's teardown.test_home_stage_proceeds_with_only_the_host_busy_flag_set.test_theme_menu.pybuilds the full GUI inside a shard and keeps the destroy.Test
tests/test_qt_widget_teardown.py:On master it fails (
DID NOT RAISE RuntimeError). Without thegc.collect(), the cycle check fails.Checked locally (macOS)
test_intensity_calibration_dialog.py: 645 passed, 2 skipped (Python 3.12, PyQt6 6.11). Aftertest_image_display_window.py, 0 top-level widgets are left; before the fix it was about 500.qtbotgive the same results as on master.test_HighContentScreeningGui.py: 53 passed.test_objective_swap_gui.py: 1 passed.qtbottest, about 80 ms of CPU each here. The suite has 868 such tests.Not verified
🤖 Generated with Claude Code
Summary by CodeRabbit