utils/gatelock/gatelock/_recovery_grab.py
Krzysztof kuhy Rudnicki 2d341dd41e
Some checks failed
dependency freshness / dependencies on newest stable (push) Waiting to run
file length / file-length (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
palette drift / palette (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
shared gate tests / gate unit tests (push) Waiting to run
gatelock pre-commit / pre-commit (push) Has been cancelled
gatelock tests / test (push) Has been cancelled
gatelock 0.13.0: locks stay clickable together
A keyring prompt (gcr-prompter) that grabbed input before the
leetcode lock armed left the lock visible but unclickable: every
click went to the prompt hidden under it. Each failed
grab_set_global also froze Tk ~1 s, so the lock barely repainted.

- Raise known credential prompts above the lock after 2 s of a
  blocked grab; games and other grabbers stay covered.
- Probe the grab on a side Xlib connection first so a blocked
  grab never stalls the Tk loop (worst gap 1.05 s -> 0.05 s).
- Log the Tk grab error and stop blaming our own claim.
- 1.5 s startup grace in wait_for_turn, and a weaker armed lock
  steps back (windowless, re-arms later) when a stronger one is
  live, so only one lock is ever on screen.
- Real-X checks: verify_prompt_yield, verify_grab_responsive,
  verify_all_lockers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WzxdWZbpxQZj41WpbQPuxB
2026-11-11 12:49:40 +01:00

58 lines
1.9 KiB
Python

# Copyright (c) 2026 Krzysztof Rudnicki
"""Re-taking a lost global grab, once per recovery tick.
Split out of :mod:`gatelock._recovery` for the 250-line cap, and kept in its
``_recovery_*`` family on purpose: the static invariant test globs the family,
so this file is held to the same rule -- it may take the grab, never release it.
"""
from __future__ import annotations
import logging
import tkinter as tk
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from gatelock._grabwatch import GrabWatch
from gatelock._recovery_types import RecoveryCollaborators
_logger = logging.getLogger(__name__)
def reassert_grab(
root: tk.Misc, parts: RecoveryCollaborators, watch: GrabWatch
) -> bool:
"""Re-take the global grab if we lost it. Never releases one.
A failed attempt is reported with Tk's own message (once per distinct
message: this runs every tick) and timed by the prompt-yield policy. A
grab we hold ends any yield, and then the lock is lifted over whatever it
had stepped aside for.
Args:
root: The Tk root that holds the grab.
parts: The loop's collaborators.
watch: Answers "is the grab ours?" and throttles the warning.
Returns:
True if the grab had to be re-taken.
"""
if parts.config.resolved_grab() != "global":
return False
held = watch.holds_grab()
reasserted = False
if not held:
try:
parts.prompt_yield.check_grab()
root.grab_set_global()
except tk.TclError as exc:
parts.prompt_yield.grab_blocked(str(exc))
watch.warn_once(f"could not re-take the global grab ({exc})")
else:
_logger.warning("global grab had been lost; re-acquired it")
reasserted = held = True
if held and parts.prompt_yield.grab_held():
parts.surfaces.raise_all()
parts.on_grab_held(held=held)
return reasserted