Files
ledgrab/server/tests/test_win_shutdown.py
T
alexei.dolgolyov e24f9d33cc fix(shutdown): survive PC restart with WAL fsync + Win32 session-end guard
Two bugs caused user data ('G502' target's color-strip ref, etc.) to
revert after PC restart while persisting fine across normal app
restarts:

1. SQLite was in WAL mode with synchronous=NORMAL and Database.close()
   was never called. On graceful Python exit the sqlite3 finalizer
   checkpoints the WAL, but on an unclean PC shutdown (power loss,
   forced reboot, or Windows force-terminating pythonw.exe) the WAL
   stayed in OS cache, never reached disk, and the next boot rolled the
   DB back to the last checkpoint -- losing recent edits.

2. Nothing handled WM_QUERYENDSESSION / WM_ENDSESSION, so on PC
   shutdown Windows force-killed pythonw.exe after ~5s and the FastAPI
   lifespan never ran. The 'stop_targets' setting was silently ignored
   and devices were left at their last frame.

Changes:
- Database: PRAGMA synchronous=FULL + wal_autocheckpoint=100, plus an
  explicit wal_checkpoint(TRUNCATE) inside Database.close().
- New utils/win_shutdown.py: hidden top-level window in a daemon thread
  with a ctypes WindowProc that catches WM_QUERYENDSESSION (calls
  ShutdownBlockReasonCreate to extend Windows' 5s hung-app timeout up
  to the ~20s GUI ceiling), fires the shutdown callback, then waits in
  WM_ENDSESSION on a completion event before returning. Also raises
  the process shutdown priority via SetProcessShutdownParameters. All
  Win32 argtypes/restypes are bound once at import to avoid LPARAM
  overflow on x64.
- New shutdown_state.py: leaf module owning the cross-thread Event so
  __main__ does not import the heavy ledgrab.main at startup.
- main.py lifespan: per-step asyncio.wait_for budgets (8s for
  processor_manager.stop_all, 1.5s each for HA/MQTT, etc.) so a hung
  device cannot starve the DB checkpoint, then db.close() and
  shutdown_complete.set() always run.
- __main__.py: install the Windows shutdown guard before tray start;
  install SIGINT/SIGTERM/SIGBREAK handlers only on the tray path
  (uvicorn overwrites them on no-tray); raise server_thread.join to 20s.
- Tests cover WM_QUERYENDSESSION (fires callback, returns TRUE,
  idempotent), WM_ENDSESSION (waits on event, times out cleanly,
  cancel-path returns instantly), signal handler installation, and
  that main and shutdown_state share the same Event instance.
2026-05-22 21:43:41 +03:00

169 lines
5.1 KiB
Python

"""Tests for the Windows shutdown guard.
The guard is a no-op outside Windows, so the cross-platform tests just
check that ``start()`` returns ``False`` and never touches Win32.
On Windows we exercise the full WM_QUERYENDSESSION → WM_ENDSESSION
sequence end-to-end by ``SendMessage``-ing the hidden window directly:
the guard should fire the callback synchronously, then block in
WM_ENDSESSION until the completion event is signalled.
"""
from __future__ import annotations
import sys
import threading
import time
import pytest
from ledgrab.utils.win_shutdown import WindowsShutdownGuard
IS_WINDOWS = sys.platform == "win32"
@pytest.mark.skipif(IS_WINDOWS, reason="Non-Windows behaviour")
def test_start_returns_false_off_windows() -> None:
guard = WindowsShutdownGuard(
on_shutdown=lambda: None,
shutdown_complete=threading.Event(),
)
assert guard.start() is False
@pytest.mark.skipif(not IS_WINDOWS, reason="Requires Win32 user32")
def test_start_creates_hidden_window() -> None:
guard = WindowsShutdownGuard(
on_shutdown=lambda: None,
shutdown_complete=threading.Event(),
)
try:
assert guard.start() is True
assert guard._hwnd is not None
finally:
guard.stop()
@pytest.mark.skipif(not IS_WINDOWS, reason="Requires Win32 user32")
def test_query_endsession_fires_callback_and_returns_true() -> None:
import ctypes
WM_QUERYENDSESSION = 0x0011
fired: list[str] = []
complete = threading.Event()
guard = WindowsShutdownGuard(
on_shutdown=lambda: fired.append("cb"),
shutdown_complete=complete,
wait_seconds=0.5,
)
try:
assert guard.start() is True
result = ctypes.windll.user32.SendMessageW(guard._hwnd, WM_QUERYENDSESSION, 0, 0)
assert result == 1, "WM_QUERYENDSESSION must return TRUE so Windows ends the session"
assert fired == ["cb"], "shutdown callback should fire exactly once on WM_QUERYENDSESSION"
finally:
guard.stop()
@pytest.mark.skipif(not IS_WINDOWS, reason="Requires Win32 user32")
def test_query_endsession_is_idempotent() -> None:
"""Two WM_QUERYENDSESSION messages must not run the callback twice."""
import ctypes
WM_QUERYENDSESSION = 0x0011
fired: list[str] = []
guard = WindowsShutdownGuard(
on_shutdown=lambda: fired.append("cb"),
shutdown_complete=threading.Event(),
wait_seconds=0.5,
)
try:
assert guard.start() is True
ctypes.windll.user32.SendMessageW(guard._hwnd, WM_QUERYENDSESSION, 0, 0)
ctypes.windll.user32.SendMessageW(guard._hwnd, WM_QUERYENDSESSION, 0, 0)
assert fired == ["cb"]
finally:
guard.stop()
@pytest.mark.skipif(not IS_WINDOWS, reason="Requires Win32 user32")
def test_endsession_waits_for_completion_event() -> None:
import ctypes
WM_ENDSESSION = 0x0016
complete = threading.Event()
guard = WindowsShutdownGuard(
on_shutdown=lambda: None,
shutdown_complete=complete,
wait_seconds=2.0,
)
try:
assert guard.start() is True
def signal_after(delay: float) -> None:
time.sleep(delay)
complete.set()
threading.Thread(target=signal_after, args=(0.2,), daemon=True).start()
t0 = time.monotonic()
result = ctypes.windll.user32.SendMessageW(guard._hwnd, WM_ENDSESSION, 1, 0)
elapsed = time.monotonic() - t0
assert result == 0
assert (
0.15 < elapsed < 1.0
), f"WM_ENDSESSION should wait for completion, took {elapsed:.2f}s"
finally:
guard.stop()
@pytest.mark.skipif(not IS_WINDOWS, reason="Requires Win32 user32")
def test_endsession_gives_up_after_timeout() -> None:
"""If cleanup never finishes, WM_ENDSESSION must still return — Windows
will hard-kill us otherwise."""
import ctypes
WM_ENDSESSION = 0x0016
guard = WindowsShutdownGuard(
on_shutdown=lambda: None,
shutdown_complete=threading.Event(), # never set
wait_seconds=0.3,
)
try:
assert guard.start() is True
t0 = time.monotonic()
result = ctypes.windll.user32.SendMessageW(guard._hwnd, WM_ENDSESSION, 1, 0)
elapsed = time.monotonic() - t0
assert result == 0
assert (
0.25 < elapsed < 1.0
), f"WM_ENDSESSION must time out near wait_seconds, took {elapsed:.2f}s"
finally:
guard.stop()
@pytest.mark.skipif(not IS_WINDOWS, reason="Requires Win32 user32")
def test_endsession_with_cancel_does_not_wait() -> None:
"""wParam=0 on WM_ENDSESSION means the session was cancelled — no cleanup needed."""
import ctypes
WM_ENDSESSION = 0x0016
guard = WindowsShutdownGuard(
on_shutdown=lambda: None,
shutdown_complete=threading.Event(), # never set
wait_seconds=5.0,
)
try:
assert guard.start() is True
t0 = time.monotonic()
result = ctypes.windll.user32.SendMessageW(guard._hwnd, WM_ENDSESSION, 0, 0)
elapsed = time.monotonic() - t0
assert result == 0
assert elapsed < 0.2, f"WM_ENDSESSION with wParam=0 should be instant, took {elapsed:.2f}s"
finally:
guard.stop()