Skip to content

Close the main window cleanly on Ctrl+C - #149

Open
dhairyajangir wants to merge 5 commits into
dronecan:masterfrom
dhairyajangir:fix/gui-sigint-shutdown
Open

dhairyajangir wants to merge 5 commits into
dronecan:masterfrom
dhairyajangir:fix/gui-sigint-shutdown

Conversation

@dhairyajangir

@dhairyajangir dhairyajangir commented Nov 10, 2026 •

Copy link
Copy Markdown

Summary

When this code installs a SIGINT handler, queue the main-window close on Qt's event loop on SIGINT. This gives the active callback time to finish and lets the existing window and CAN-node cleanup run. The previous handler is restored when the event loop returns. An inherited ignored handler or an unknown native handler is left unchanged.

Fixes #90.

Verification

  • On an earlier PR head, ran the real Linux CLI with --interface mcast:0 and QT_QPA_PLATFORM=offscreen. With SIGINT sent directly to the GUI process, upstream printed KeyboardInterrupt from _spin_node and was still running at five seconds. The patched CLI exited with status 0 and no traceback on Python 3.11 and 3.14.
  • At current head ab3d92f, the complete local pytest selection passed 30 tests with four deprecation warnings on Linux, Python 3.11 / PyQt 6.2.3 / Qt 6.2.4, and 30 tests on Python 3.14 / PyQt and Qt 6.12.0. Plugin autoload and pytest cache were disabled; both runs used reused dependencies and no physical CAN adapter. These are not upstream CI results.
  • A separate process-group replay closed the main GUI with a modal dialog or the Bus Monitor open. The multicast adapter and Bus Monitor run in child processes and can still emit their own interrupt tracebacks.

AI assistance was used for investigation, implementation and verification.

@dakejahl

Copy link
Copy Markdown

Claude review on behalf of @dakejahl

Verdict: merge. It routes Ctrl+C through the same close() path as File → Quit, which fixes the case reported in #90.

Must-fix: none.

Optional

  • dronecan_gui_tool/main.py:682: on Qt 6.2, which Ubuntu 22.04 ships, a close request for a window blocked by a modal dialog is dropped and Ctrl+C does nothing until the dialog is dismissed, so queue app.closeAllWindows instead of window.close.
  • dronecan_gui_tool/main.py:680: while the GUI thread is inside a blocking call each Ctrl+C only queues another close where master's KeyboardInterrupt broke out of the call, so reset SIGINT to signal.SIG_DFL in the handler and let a second Ctrl+C kill the process.
  • dronecan_gui_tool/main.py:684: the handler is installed after the setup dialog returns, so Ctrl+C at the interface dialog still raises KeyboardInterrupt into the timer slot at dronecan_gui_tool/setup_window.py:344 and the app keeps running, which installing it before the setup loop should fix.
  • dronecan_gui_tool/main.py:684: a GUI started as a background job from a script inherits SIGINT ignored and the PR overrides that, so skip the install when signal.getsignal(signal.SIGINT) is signal.SIG_IGN.

Worth doing while there

  • dronecan_gui_tool/widgets/bus_monitor/__init__.py:55 and dronecan_gui_tool/widgets/plotter/__init__.py:56: the Bus Monitor and plotter children still print KeyboardInterrupt tracebacks on a terminal Ctrl+C, which is half the log in CTRL+C will not exit #90, and ignoring SIGINT at the top of _process_entry_point should silence them.

@dhairyajangir

dhairyajangir commented Nov 10, 2026 •

Copy link
Copy Markdown
Author

Thanks for the detailed review. I reproduced the modal case with the actual GUI at this PR head on Python 3.11, PyQt 6.2.3 / Qt 6.2.4, using mcast:0 and the offscreen platform: with a synthetic child QDialog in exec(), SIGINT left the process running past five seconds. A local closeAllWindows() diagnostic exited cleanly in that setup. This is one Linux/Qt configuration; the earlier modal replay in the PR description used a newer Qt version.

I also checked a possible tradeoff before changing the handler. In an isolated Qt 6.2 control using an actual ControlPanelWindow constructed with a fake interface and a synthetic unsaved-changes flag, and no real adapter, an already-open “go back and save?” confirmation stayed open with the current handler. Both closeAllWindows() and an alternative that closes the active modal dismissed that confirmation, which returned False and allowed the panel to close. An independent rerun matched these results. This demonstrates a potential change in confirmation behavior, not observed data loss on a real adapter.

Would maintainers prefer SIGINT to dismiss even an unanswered save confirmation, or should it leave that confirmation for the user? I can adjust the implementation and add regression coverage once the intended behavior is clear. The other suggestions involve separate setup, repeated-signal, inherited-handler, and child-process paths; I have not yet established a complete fix for them. Codex assisted with the investigation and tests.

@dhairyajangir

dhairyajangir commented Nov 10, 2026 •

Copy link
Copy Markdown
Author

Thanks for pointing out the inherited SIG_IGN case. I pushed 9aac50de95565915104471438beb6ae94eb5cabf: the GUI now leaves SIGINT ignored if it inherited that disposition, and restores the prior handler on exit only if it installed its own handler.

With the same subprocess regression tests on Python 3.11 / Qt 6.2.4, the upstream base passed both ignored-handler cases, the previous PR head failed them, and the revised head passed all four ignored/default/custom-handler cases. The full local test selection passed 29 tests with four warnings on Python 3.11 / Qt 6.2.4 and 29 on Python 3.14 / Qt 6.12.0. An independent real-GUI loopback multicast replay also found the base and revision remained running after SIGINT when it was inherited as ignored, while the previous PR head exited; the surviving test processes were terminated afterward, so that replay is not a graceful-exit test. These are Linux local tests using reused dependencies, not hardware or upstream CI results.

GitHub reports action_required for the three new-head workflows; could a maintainer check whether they need approval to run? The separate Qt 6.2 modal/save-confirmation question above remains open; this revision does not change that behavior or the setup-dialog, repeated-interrupt or child-process cases. Codex assisted with the change and tests.

I also checked the setup-dialog suggestion without hardware: an external SIGINT sent after the actual setup dialog became visible caused a KeyboardInterrupt traceback in its interface-update callback, and the process was still running when the test's 1.8-second cleanup timer fired and rejected the active dialog. I saw this on both the sampled upstream base and current PR head, with synthetic interface discovery on Linux using Python 3.11 / Qt 6.2.4 and Python 3.14 / Qt 6.12.0. On the latter run the supervised child exited with a signal status after test teardown, so I am not treating that as a clean exit. This setup behavior predates the PR and is not changed by the revision above.

@dakejahl

Copy link
Copy Markdown

Claude review on behalf of @dakejahl

Verdict: merge after one fix. The main.py change in 9aac50d is correct, but the new test fails when the dependencies are installed under ~/.local.

Must-fix

  • tests/test_sigint_inheritance.py:61: overriding HOME moves Python's user site into tmp_path, so with PyQt6 or dronecan installed by pip install --user the child dies with ModuleNotFoundError and all four cases fail at line 75, which dropping the HOME override or passing PYTHONUSERBASE=site.getuserbase() fixes.

Optional

  • dronecan_gui_tool/main.py:682: I withdraw my closeAllWindows suggestion, because with an unsaved adapter control panel open it sometimes closes the main window first and then leaves the process running with no windows once the prompt is answered No, so close QApplication.activeModalWidget() before window.close to cover Qt 6.2 instead.
  • dronecan_gui_tool/main.py:682: on Qt 6.4 and 6.11 the current handler already answers an open save confirmation as No and exits, so dismissing it on Qt 6.2 as well only makes the versions agree.
  • dronecan_gui_tool/main.py:685: when getsignal returns None for a handler installed outside Python, line 693 raises TypeError and node.close() is skipped, so treat None like SIG_IGN.
  • tests/test_sigint_inheritance.py:38: the 500 ms liveness timer starts at show() alongside the 100 ms kill, so a first event-loop pass 500 ms late prints alive_after_sigint and fails the default and callable cases on correct code, which arming it from the kill callback avoids.
  • tests/test_sigint_inheritance.py:61: importing dronecan_gui_tool.main creates a log file with delete=False, so each run leaves four dronecan_gui_tool-*.log files in the system temp directory unless TMPDIR also points at tmp_path.

The other optional items from my first review need not block this PR.

@dhairyajangir

Copy link
Copy Markdown
Author

Thanks for catching this. I pushed d2bf7f3b419a780cd6b6b72fc012f182ba041aa5: the subprocess test keeps the parent’s Python user base when isolating HOME, so user-site dependencies remain importable. It also places child temporary files under tmp_path and schedules the liveness/replacement timers after sending SIGINT.

An independent Linux reproduction used Python 3.11 with user-site discovery enabled and existing dependencies linked into a temporary user site: the previous PR head failed all four cases because the child could not import PyQt6, and the revision passed all four. This uses reused dependencies and is not an upstream CI or physical-adapter test. Codex assisted with the change and validation.

This revision changes only the tests. The Qt modal/confirmation scope discussed above remains open, and I have not yet verified the optional externally installed native-handler case.

@dhairyajangir

Copy link
Copy Markdown
Author

Thanks for identifying the getsignal() is None case. I reproduced it using embedded CPython 3.11 with a real C SIGINT handler installed before Python initialization and a fake CAN node/window: at the previous head, the GUI replaced the native handler, restoration raised TypeError, and node cleanup did not run.

I pushed f5cd2c2068735b71e6dd1b1d3a1f6e295e80786b: when Python cannot identify the existing handler, the GUI now leaves it untouched, as it already does for SIG_IGN. In the same local harness, the native handler ran during and after the GUI loop and node cleanup completed. This means SIGINT remains owned by the embedding host in that case rather than triggering the GUI close handler.

The added subprocess regression simulates Python reporting an unknown handler; it failed on the previous head and passed with this change. The separate native-C harness exercises the real handler case. The full local test selection passed 30 tests with four deprecation warnings on Linux, Python 3.11 / Qt 6.2.4. Tests used reused dependencies and no physical CAN adapter; they are not upstream CI or cross-platform results. The modal/save-confirmation question remains separate. Codex assisted with the fix and tests.

@dakejahl

Copy link
Copy Markdown

Claude review on behalf of @dakejahl

Verdict: merge. d2bf7f3 and f5cd2c2 fix the user-site import failure and the unknown-handler TypeError, and each of the five test cases fails on the commit it guards against.

Must-fix: none.

Optional

  • dronecan_gui_tool/main.py:682: while a slot is stuck in Python code every Ctrl+C only queues another close, where master's KeyboardInterrupt broke out of it, so restore the previous handler inside close_on_interrupt and let a second Ctrl+C fall through.
  • dronecan_gui_tool/main.py:699: the process exits 0 after Ctrl+C, so a calling script cannot tell it was interrupted, which exiting 130 when the handler fired would fix.
  • tests/test_sigint_inheritance.py:103: restored True cannot fail for unknown because the patched getsignal returns None on both sides, so skip that assertion for that case.
  • The description still says the previous handler is always restored and counts 25 tests, so update it for the ignored and unknown-handler cases and the new test.

The Qt 6.2 modal dialog, the setup dialog and the child-process tracebacks from my earlier reviews are still open and need not block this PR.

@dhairyajangir

Copy link
Copy Markdown
Author

Thanks for catching the stale description. I updated it to describe when the SIGINT handler is installed and restored, and to distinguish the 30-test current-head Python 3.11 run from the earlier 29-test Python 3.14 run. The existing discussion of the other suggestions remains open.

Codex assisted with the description update and this reply.

@dhairyajangir

Copy link
Copy Markdown
Author

Thanks for pointing out the tautological assertion. In ab3d92f8dc967a55bbe4760ceb14aa10ecfeecb2, the unknown-handler case no longer checks restored True; it retains the signal_calls 0 assertion that verifies the GUI neither installs nor restores a Python SIGINT handler in that case. The other handler cases and production behavior are unchanged.

On Linux, the complete local pytest selection passed 30 tests (four existing deprecation warnings) with Python 3.11 / PyQt 6.2.3 / Qt 6.2.4, and 30 tests with Python 3.14 / PyQt and Qt 6.12.0. Both runs used reused dependencies, disabled pytest plugin autoload and cache, and ran offscreen without a physical CAN adapter; these are not upstream CI results. I updated the description with the current-head results. The other optional signal-behavior and modal questions remain separate.

Codex assisted with this test-only change and verification.

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.

CTRL+C will not exit

2 participants