From 2987d6efa7e6f2b9848808cdeb471215d9a37e4d Mon Sep 17 00:00:00 2001 From: Krzysztof kuhy Rudnicki Date: Fri, 20 Nov 2026 11:11:46 +0100 Subject: [PATCH] Arm stall watch once the Tk loop runs The watch was armed in __init__, before the lock window build (xrandr, setxkbmap, surfaces, grab), so the ~1.5 s build was reported as a tk.mainloop freeze. Arm it via after_idle in run() instead; a real freeze after the loop starts is still reported. Error-Signature: screen-locker:c890f31a21 Co-Authored-By: Claude Opus 5.5 --- screen_locker/screen_lock.py | 5 +++-- screen_locker/tests/test_errors.py | 20 ++++++++++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/screen_locker/screen_lock.py b/screen_locker/screen_lock.py index 3fd9fff..11c44da 100755 --- a/screen_locker/screen_lock.py +++ b/screen_locker/screen_lock.py @@ -142,8 +142,6 @@ class ScreenLocker( self._check_early_exits() try: self.root = GateRoot() - if _errors.SLOT.reporter is not None: - watch_stalls(self.root, _errors.SLOT.reporter) except tk.TclError as exc: _logger.warning( "Could not open a window on DISPLAY=%s (%s) — this run cannot " @@ -237,6 +235,9 @@ class ScreenLocker( def run(self) -> None: """Start the Tkinter main event loop.""" + if (reporter := _errors.SLOT.reporter) is not None: + # Armed here, not in __init__: the build is no freeze (c890f31a21). + self.root.after_idle(lambda: watch_stalls(self.root, reporter)) if self._lock is not None: self._lock.run() else: diff --git a/screen_locker/tests/test_errors.py b/screen_locker/tests/test_errors.py index 4985cf9..288adb6 100644 --- a/screen_locker/tests/test_errors.py +++ b/screen_locker/tests/test_errors.py @@ -43,12 +43,25 @@ def test_cli_installs_the_reporter_before_any_mode() -> None: def test_lock_window_watches_stalls_once_installed( monkeypatch: pytest.MonkeyPatch, mock_tk: MagicMock, tmp_path: Path ) -> None: - """A lock window built after install reports its own freezes.""" + """A lock window built after install reports its own freezes. + + The watch arms only once the event loop is pumping: building the window + (xrandr scan, setxkbmap, surfaces, grab) runs before ``mainloop`` and + nothing is on screen to freeze yet, so arming it in ``__init__`` reported + the build as a 1.5 s ``tk.mainloop`` stall (screen-locker:c890f31a21). + """ watch = MagicMock() monkeypatch.setattr(screen_lock, "watch_stalls", watch) monkeypatch.setattr(_errors.SLOT, "reporter", MagicMock()) locker = create_locker(mock_tk, tmp_path) + watch.assert_not_called() + locker._lock = None + locker.run() + (arm,) = locker.root.after_idle.call_args.args + watch.assert_not_called() + arm() watch.assert_called_once_with(locker.root, _errors.SLOT.reporter) + locker.root.mainloop.assert_called_once() def test_lock_window_skips_the_watch_without_install( @@ -57,5 +70,8 @@ def test_lock_window_skips_the_watch_without_install( """Tests and library callers that never installed get no watch.""" watch = MagicMock() monkeypatch.setattr(screen_lock, "watch_stalls", watch) - create_locker(mock_tk, tmp_path) + locker = create_locker(mock_tk, tmp_path) + locker._lock = None + locker.run() + locker.root.after_idle.assert_not_called() watch.assert_not_called()