steam-backlog-enforcer/steam_backlog_enforcer/_research_tick.py
Krzysztof kuhy Rudnicki c1f0562995
Some checks are pending
dependency freshness / dependencies on newest stable (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (mypy, mypy, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (pylint, pylint, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (rest, , mypy,pylint) (push) Waiting to run
Tests / test (push) Waiting to run
Tests / web-coverage (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
Research tick survives pacman timeouts
Under load pacman -Qi / iptables exceed their 10 s timeouts inside the
research-window pass; the uncaught TimeoutExpired crashed the daemon
(systemd restart loop). The pass now logs and retries next tick.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 14:56:00 +01:00

116 lines
4.8 KiB
Python

"""One daemon pass under a total block: the research window or the full block.
The enforce loop hands its whole total-block branch here. With a research
window open (see :mod:`steam_backlog_enforcer._research_window`) Steam is left
installed and reachable, and the *only* thing kept is the one-allowed-game
rule, bent to a single game: the loop-local allowed set is ``{Librarian}``. The
real assignment in ``state.json`` is never read or written, so there is
nothing to restore afterwards. Everything else the block does to Steam itself
(kill, uninstall, purge) is skipped while the window is open.
Once the deadline passes the record is latched closed (a clock set back cannot
reopen it) and every pass is the ordinary :func:`enforce_total_block_tick`
again: it kills Steam, removes the package, purges its files, rebuilds the
firewall chain from the kept IP cache, and puts the full hosts section back.
The lock's end date is never involved.
"""
from __future__ import annotations
from dataclasses import replace
import logging
import subprocess
from typing import TYPE_CHECKING
from leisure_watch.procscan import LAUNCHER_PROCESS_NAMES, kill_target_names
from steam_backlog_enforcer import _research_net as net
from steam_backlog_enforcer import _research_window as window
from steam_backlog_enforcer import config as config_module
from steam_backlog_enforcer._enforce_guards import guard_installed_games
from steam_backlog_enforcer._research_net import reconcile_open_window
from steam_backlog_enforcer._steam_launch import steam_is_installed
from steam_backlog_enforcer._total_block import enforce_total_block_tick
from steam_backlog_enforcer._total_block_purge import kill_and_uninstall_launchers
from steam_backlog_enforcer.enforcer import (
enforce_allowed_game,
kill_processes_by_name,
send_notification,
)
from steam_backlog_enforcer.store_blocker import restore_store_block
if TYPE_CHECKING:
from steam_backlog_enforcer._research_record import ResearchRecord
from steam_backlog_enforcer.config import Config
logger = logging.getLogger(__name__)
def _latch_closed(record: ResearchRecord) -> ResearchRecord:
"""Persist that the window is over; blocking does not wait for the write."""
closed = replace(record, closed=True)
try:
window.write_record(closed)
except OSError:
logger.exception("Could not latch the research window closed")
return closed
def _reblock_store(config: Config, record: ResearchRecord) -> None:
"""Put the normal store block back once, after the window ended.
The total block's own hosts section covers the store until the lock ends,
but ``end_total_block_cleanup`` does not restore the normal block, so
without this the store would stay open after the lock. Unconditional
(:func:`restore_store_block`): the section being back makes the store look
blocked already.
"""
if not net.attempt_due("store"):
return
if config.block_store and not restore_store_block():
net.note_failure("store")
return
try:
window.write_record(replace(record, store_reblocked=True))
except OSError:
logger.exception("Could not record that the store block is back")
def _research_pass(record: ResearchRecord) -> None:
"""Keep Steam open and Librarian the only game while the window is open."""
reconcile_open_window(record)
# Non-Steam launchers and counted games are still forbidden; Steam itself
# is not killed, uninstalled or purged (that is the block's tick).
kill_and_uninstall_launchers(LAUNCHER_PROCESS_NAMES)
kill_processes_by_name(kill_target_names(config_module.CONFIG_FILE))
if not steam_is_installed():
return
allowed = {window.LIBRARIAN_APP_ID}
killed = enforce_allowed_game(allowed, kill_unauthorized=True, strict=True)
for _pid, app_id in killed:
send_notification("⛔🎮", f"{app_id} → 🎯 {window.LIBRARIAN_NAME}")
guard_installed_games(allowed, strict=True)
def total_block_pass(config: Config) -> None:
"""The total-block branch of one enforce pass.
Args:
config: Loaded configuration (``block_store`` gates the store re-block).
"""
at = window.now()
record = window.live_record(at)
if record is not None and record.is_open(at):
# A loaded machine can push pacman/iptables past their timeouts; that
# costs this tick (the next one retries), never the daemon.
try:
_research_pass(record)
except OSError, subprocess.SubprocessError:
logger.exception("Research window pass failed; retrying next tick")
return
if record is not None and not record.closed and at >= record.until:
record = _latch_closed(record)
enforce_total_block_tick()
if record is not None and record.closed and not record.store_reblocked:
_reblock_store(config, record)