From c6bcc15066a8d3a40d1466b6bd83ec6550cd7444 Mon Sep 17 00:00:00 2001 From: "Dennis K. Paulsen" Date: Fri, 16 Oct 2026 13:52:01 -0500 Subject: [PATCH] feat: raise the new typed PortalSessionClosedError when the compositor closes a portal session mid-wait instead of hanging forever, build and test the declared libei 1.0/1.1 floor from source weekly in CI, and read upstream prerelease versions like Debian's 1.3.901 as the next minor in the ABI tests; release 0.7.0 --- .github/workflows/ci.yml | 72 ++++++++++++++++++++++++ CHANGELOG.md | 34 ++++++++++++ README.md | 2 +- docs/developers/verification.md | 2 +- pyproject.toml | 2 +- src/libei/__init__.py | 2 +- src/libei/portal.py | 58 +++++++++++++++++++- tests/test_abi.py | 15 ++++- tests/test_inputcapture.py | 97 +++++++++++++++++++++++++++++++++ 9 files changed, 277 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a47ab51..62c236c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -8,6 +8,12 @@ on: tags: ["v*"] pull_request: workflow_dispatch: + schedule: + # Weekly, for the libei-floor job below: the declared 1.0.0 floor is + # otherwise a claim nothing executes, since no distro CI runs ships a + # libei that old. Monday morning, so a break is found at the start of a + # week rather than during a release. + - cron: "0 5 * * 1" jobs: test: @@ -83,6 +89,72 @@ jobs: print("import-safe with no native libraries") PY + # The README declares "libei 1.0.0 or newer"; development tracks 1.6.x and + # the `test` job above runs Ubuntu's 1.2.1 -- which leaves 1.0.x and 1.1.x + # as a floor nothing has executed since it was declared. This builds those + # exact releases from source and runs the suite against them: the + # version-gated tests must *skip* (that is their whole mechanism firing for + # real), and everything else must pass. Weekly and on dispatch, not per + # push -- a from-source meson build is minutes of work and the floor only + # moves when a release changes it. Deliberately NOT in `publish`'s needs: + # a job that does not run on tag pushes would leave publish waiting on a + # skip. + libei-floor: + name: declared floor (libei ${{ matrix.libei }}) + if: github.event_name == 'schedule' || github.event_name == 'workflow_dispatch' + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + libei: ["1.0.0", "1.1.0"] + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: "3.12" + + # What libei's own meson build needs: jinja2/attrs generate the + # protocol code, libevdev and libsystemd (sd-bus, for liboeffis) are + # its two library dependencies. Documentation is switched off rather + # than left on auto so a runner image that happens to carry doxygen + # does not turn this into a docs build. + - name: Install the libei build dependencies + run: | + sudo apt-get update + sudo apt-get install -y --no-install-recommends \ + meson ninja-build pkg-config gcc libevdev-dev libsystemd-dev \ + python3-jinja2 python3-attr + + - name: Build and install libei ${{ matrix.libei }} from source + run: | + set -eu + git clone --depth 1 --branch "${{ matrix.libei }}" \ + https://gitlab.freedesktop.org/libinput/libei.git /tmp/libei + cd /tmp/libei + # Options verified against 1.0.0's own meson_options.txt: + # documentation is an array (default []), tests a feature whose + # auto would pull the munit subproject this job has no use for. + meson setup build --prefix=/usr/local -Ddocumentation=[] -Dtests=disabled + ninja -C build + sudo ninja -C build install + sudo ldconfig + + - run: pip install -e '.[dev]' + + - name: The loader must find exactly the version just built + run: | + python - <<'PY' + from libei import ei, eis, oeffis + for mod in (ei, eis, oeffis): + print(f"{mod.__name__}: {mod.is_available()}") + assert ei.is_available() and eis.is_available(), "the built libei was not found" + PY + + # -rs for the same reason as the `test` job: on a floor build the + # version-gated tests skipping is the finding, and the log should name + # every one of them. + - run: python -m pytest -q -rs + build: runs-on: ubuntu-latest steps: diff --git a/CHANGELOG.md b/CHANGELOG.md index 34ad2ce..b95752e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,40 @@ All notable changes to python-libei are recorded here. The format follows ## [Unreleased] +## [0.7.0] - 2026-10-16 + +### Added + +- **A portal session closed from the compositor's side now ends a wait + instead of hanging it.** `InputCaptureSession.wait_for_activation()` and + `wait_for_deactivation()` subscribe to the session object's own + `org.freedesktop.portal.Session::Closed` signal alongside the signal they + wait for, and raise the new typed `PortalSessionClosedError` when it + fires — naming what was being waited for. Before this, a wait with + `timeout=None` on a session the compositor or portal had ended (a + compositor restart, the portal process dying, capture revoked from a + settings panel) blocked forever, indistinguishable from a pointer barrier + nobody had crossed yet. Note `Closed` really is emitted on the session's + own object path — the opposite of `Activated`/`Deactivated`, which fire + on the portal object. +- **A weekly CI job builds libei 1.0.0 and 1.1.0 from source and runs the + suite against them.** The README's "libei 1.0.0 or newer" floor was a + claim nothing executed — development tracks 1.6.x and CI's Ubuntu ships + 1.2.1 — so the declared floor now runs for real, with the version-gated + tests skipping loudly as designed. + +### Fixed + +- **The ABI tests misread upstream's prerelease version numbers.** Debian 13 + ships libei 1.3.901 — a snapshot on the way to 1.4 that already exports + the 1.4 symbols — and the "marked functions really are absent from a + library that predates them" pin read 1.3.901 as predating every 1.4 + marker, failing on a library whose only sin was being packaged mid-cycle. + A micro of 900 or more is now normalized to the next minor, matching what + such a library actually contains. Found by running the suite across eight + live machines; the binding itself was never wrong, only the test's + reading of the version. + ## [0.6.1] - 2026-10-01 ### Changed diff --git a/README.md b/README.md index 8bfa263..11579db 100644 --- a/README.md +++ b/README.md @@ -135,7 +135,7 @@ and why, is in ## Status -Beta (`0.6.1`), published on [PyPI](https://pypi.org/project/python-libei/) +Beta (`0.7.0`), published on [PyPI](https://pypi.org/project/python-libei/) since `0.1.0`, and **the API is not frozen** — expect renames before 1.0. The injection path is exercised end to end against the real libraries by the diff --git a/docs/developers/verification.md b/docs/developers/verification.md index 3797fdf..01b4dc9 100644 --- a/docs/developers/verification.md +++ b/docs/developers/verification.md @@ -1,6 +1,6 @@ # What has actually been verified -The package is beta (`0.6.1`) and the API is not frozen — expect renames +The package is beta (`0.7.0`) and the API is not frozen — expect renames before 1.0. This page is what that qualifier covers, concretely: which paths have been driven against real libraries, which have only ever been driven against fakes, and which libei versions the whole thing has met. diff --git a/pyproject.toml b/pyproject.toml index a5f408c..2ebcde4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "python-libei" -version = "0.6.1" +version = "0.7.0" description = "Inject, receive and capture input on Wayland from Python: ctypes bindings for libei, libeis and liboeffis, plus the RemoteDesktop and InputCapture portals" readme = "README.md" license = "MIT" diff --git a/src/libei/__init__.py b/src/libei/__init__.py index 9a48a72..f008723 100644 --- a/src/libei/__init__.py +++ b/src/libei/__init__.py @@ -31,6 +31,6 @@ Beta: the API is not frozen. """ -__version__ = "0.6.1" +__version__ = "0.7.0" __all__ = ["__version__"] diff --git a/src/libei/portal.py b/src/libei/portal.py index 22ec6c4..cce9235 100644 --- a/src/libei/portal.py +++ b/src/libei/portal.py @@ -185,6 +185,28 @@ def __init__(self, step: str, message: str | None = None) -> None: self.message = message +class PortalSessionClosedError(PortalError): + """The portal session ended while something was still waiting on it. + + The ``org.freedesktop.portal.Session`` interface emits ``Closed`` when + the compositor or the portal ends a session from its side -- a + compositor restart, the portal process dying, a session revoked from a + settings panel. Distinct from :class:`PortalTimeoutError` (the session + is fine, the signal just has not come) and from + :class:`PortalDeniedError` (a request was answered, negatively): here + there is no session left to answer anything, so the wait that raised + this can never be satisfied by waiting longer. + """ + + def __init__(self, step: str) -> None: + """Name what was being waited for when the session went away.""" + super().__init__( + f"the portal session was closed while waiting for {step}; " + "the compositor or portal ended it from its side" + ) + self.step = step + + def _gio() -> tuple[Any, Any] | None: """Import Gio and GLib, or return None. @@ -1038,6 +1060,27 @@ def _wait_for_signal( loop = GLib.MainLoop() result: dict[str, Any] = {} timed_out = False + session_closed = False + + def on_closed(*_args: Any) -> None: + """End the wait: a closed session can never deliver the signal. + + ``Closed`` is emitted on the **session object's own path** (unlike + ``Activated``/``Deactivated``, which fire on the portal object -- + see above), so the subscription below uses ``session_handle`` as + the path and needs no payload filtering: any ``Closed`` there is + this session's. Without this subscription, a wait with + ``timeout=None`` on a session the compositor has ended -- a + compositor restart, the portal dying, capture revoked from a + settings panel -- blocked forever, indistinguishable from a signal + that merely had not fired yet. + """ + nonlocal session_closed + if result: + return + logger.debug("session %s closed while waiting for %s", session_handle, signal) + session_closed = True + loop.quit() def on_signal( _conn: Any, @@ -1086,6 +1129,16 @@ def on_timeout() -> bool: on_signal, None, ) + closed_subscription = connection.signal_subscribe( + busname, + _SESSION_INTERFACE, + "Closed", + session_handle, + None, + Gio.DBusSignalFlags.NONE, + on_closed, + None, + ) logger.debug( "waiting up to %s for %s.%s on %s for session %s", "forever" if timeout is None else f"{timeout:g}s", @@ -1095,7 +1148,7 @@ def on_timeout() -> bool: session_handle, ) try: - if not result: + if not result and not session_closed: timeout_source = None if timeout is not None: timeout_source = GLib.timeout_add(int(timeout * 1000), on_timeout) @@ -1115,6 +1168,9 @@ def on_timeout() -> bool: GLib.source_remove(timeout_source) finally: connection.signal_unsubscribe(subscription) + connection.signal_unsubscribe(closed_subscription) + if session_closed: + raise PortalSessionClosedError(signal) if timed_out: # timed_out is only ever set inside on_timeout, itself only ever # registered when timeout is not None -- so this always holds, but diff --git a/tests/test_abi.py b/tests/test_abi.py index 58b6eea..3c29de4 100644 --- a/tests/test_abi.py +++ b/tests/test_abi.py @@ -97,7 +97,15 @@ def _declared(prefix: str) -> dict[str, tuple[tuple[type, ...], type | None]]: def _installed_version(soname: str) -> tuple[int, ...] | None: - """The version in the loaded library's file name (``libei.so.1.6.0``).""" + """The version in the loaded library's file name (``libei.so.1.6.0``). + + A micro of 900 or more is upstream's prerelease numbering for the *next* + minor -- Debian 13 ships 1.3.901, a snapshot on the way to 1.4 that + already exports the 1.4 symbols -- so it is normalized to that next + minor. Read literally, 1.3.901 "predates" every 1.4 marker while + carrying everything they mark, which failed the absent-function pin + below on a library whose only sin was being packaged mid-cycle. + """ try: maps = Path("/proc/self/maps").read_text() except OSError: @@ -106,7 +114,10 @@ def _installed_version(soname: str) -> tuple[int, ...] | None: for line in maps.splitlines(): found = re.search(rf"/{re.escape(stem)}\.so\.(\d+\.\d+\.\d+)$", line) if found: - return _version(found.group(1)) + major, minor, micro = _version(found.group(1)) + if micro >= 900: + return (major, minor + 1, 0) + return (major, minor, micro) return None diff --git a/tests/test_inputcapture.py b/tests/test_inputcapture.py index cb21445..b3b04fc 100644 --- a/tests/test_inputcapture.py +++ b/tests/test_inputcapture.py @@ -83,6 +83,7 @@ def __init__( pending_activated: dict[str, Any] | None = None, pending_deactivated: dict[str, Any] | None = None, signal_session_handle: str | None = None, + pending_closed: bool = False, ) -> None: self.session_handle = session_handle self.signal_session_handle = signal_session_handle or session_handle @@ -105,6 +106,7 @@ def __init__( self._pending_reply: tuple[str, int, dict[str, Any]] | None = None self._pending_activated = pending_activated self._pending_deactivated = pending_deactivated + self._pending_closed = pending_closed # A permanent log, unlike _subscriptions: signal_unsubscribe removes # an entry from that (correctly -- see its own docstring), so a test # checking *what was subscribed to* after the fact needs a record @@ -184,6 +186,13 @@ def signal_subscribe( payload = self._pending_deactivated self._pending_deactivated = None self._emit_session_signal(callback, "Deactivated", payload) + elif signal == "Closed" and self._pending_closed: + # Closed fires on the session object's own path, not the + # portal's -- the opposite of Activated/Deactivated above -- so + # it is only delivered to a subscription made there. + if path == self.session_handle: + self._pending_closed = False + callback(None, None, path, None, "Closed", FakeReply(({},))) return self._next_subscription_id def _emit_session_signal( @@ -770,3 +779,91 @@ def call_sync(self, *args: Any, **kwargs: Any) -> FakeReply: with install_fake_gi(connection): session = portal.InputCaptureSession.negotiate(connection=connection) session.close() # must not raise + + +# -- the session Closed signal ------------------------------------------------ + + +def test_a_closed_session_ends_wait_for_activation_rather_than_hanging() -> None: + # The compositor-disconnect case: Session.Closed fires when the portal + # or compositor ends the session from its side. Before the Closed + # subscription existed, a wait with timeout=None on such a session + # blocked forever -- indistinguishable from a barrier nobody had + # crossed yet. + connection = FakeInputCaptureConnection(pending_closed=True) + with install_fake_gi(connection): + session = portal.InputCaptureSession.negotiate(connection=connection) + with pytest.raises(portal.PortalSessionClosedError) as excinfo: + session.wait_for_activation(timeout=None) + assert excinfo.value.step == "Activated" + + +def test_a_closed_session_ends_wait_for_deactivation_too() -> None: + connection = FakeInputCaptureConnection(pending_closed=True) + with install_fake_gi(connection): + session = portal.InputCaptureSession.negotiate(connection=connection) + with pytest.raises(portal.PortalSessionClosedError) as excinfo: + session.wait_for_deactivation(timeout=None) + assert excinfo.value.step == "Deactivated" + + +def test_closed_is_subscribed_on_the_session_path_not_the_portal() -> None: + # The mirror image of the Activated-path bug: Closed is the one signal + # that really does fire on the session object's own path, so the + # subscription has to go there. + connection = FakeInputCaptureConnection() + with install_fake_gi(connection): + session = portal.InputCaptureSession.negotiate(connection=connection) + with pytest.raises(portal.PortalTimeoutError): + session.wait_for_activation(timeout=0.01) + subscribed = { + key[2] for key in connection.signal_subscriptions if key[1] == "Closed" + } + assert subscribed == {connection.session_handle} + + +def test_every_wait_subscription_is_unsubscribed_again() -> None: + # Both the signal's own subscription and the Closed one: a leak here + # means a long-running capture loop accumulates one dead subscription + # per activation cycle. + connection = FakeInputCaptureConnection(pending_activated={"activation_id": 1}) + with install_fake_gi(connection): + session = portal.InputCaptureSession.negotiate(connection=connection) + session.wait_for_activation(timeout=1.0) + live = [k for k in connection._subscriptions if k[1] in ("Activated", "Closed")] + assert live == [] + + +def test_a_session_closed_mid_wait_still_cleans_up_its_subscriptions() -> None: + connection = FakeInputCaptureConnection(pending_closed=True) + with install_fake_gi(connection): + session = portal.InputCaptureSession.negotiate(connection=connection) + with pytest.raises(portal.PortalSessionClosedError): + session.wait_for_activation(timeout=None) + live = [k for k in connection._subscriptions if k[1] in ("Activated", "Closed")] + assert live == [] + + +def test_repeated_activation_cycles_each_get_their_own_signal() -> None: + # A capture loop waits, releases, waits again -- each wait is its own + # subscription, delivered its own payload, with nothing carried over. + connection = FakeInputCaptureConnection(pending_activated={"activation_id": 1}) + with install_fake_gi(connection): + session = portal.InputCaptureSession.negotiate(connection=connection) + first = session.wait_for_activation(timeout=1.0) + connection._pending_activated = {"activation_id": 2} + second = session.wait_for_activation(timeout=1.0) + assert (first.activation_id, second.activation_id) == (1, 2) + + +def test_an_activation_payload_without_an_id_defaults_to_zero() -> None: + # The spec says activation_id is always sent; a portal that omits it + # anyway gets the same 0 a compositor that "could not determine" sends, + # rather than a KeyError mid-wait. + connection = FakeInputCaptureConnection(pending_activated={}) + with install_fake_gi(connection): + session = portal.InputCaptureSession.negotiate(connection=connection) + activation = session.wait_for_activation(timeout=1.0) + assert activation == portal.Activation( + activation_id=0, cursor_position=None, barrier_id=None + )