"""Bounded QThread shutdown for window close.""" from __future__ import annotations from PyQt6.QtCore import QThread def stop_thread(worker: QThread | None, wait_ms: int = 4000) -> None: """Let a worker finish, then terminate it as a last resort. QThread must never be destroyed while still running; Qt aborts the process. Network and PowerShell calls can outlive a window close, so request interruption, wait, and only then terminate. This runs during application shutdown, never on a normal user action. """ if worker is None: return try: if worker.isRunning(): worker.requestInterruption() if not worker.wait(max(250, int(wait_ms))): worker.terminate() worker.wait(1000) except RuntimeError: # The C++ object was already deleted by deleteLater(). pass