From 0ede5e412b820cf2a490c7ba56c688eefe7728da Mon Sep 17 00:00:00 2001 From: "Dennis K. Paulsen" Date: Wed, 9 Sep 2026 21:47:47 -0500 Subject: [PATCH] Fix InputCaptureSession.wait_for_activation()/wait_for_deactivation() subscribing to Activated/Deactivated with the session handle as the D-Bus object path instead of the portal's own object, which matched nothing and was misdiagnosed as a Mutter bug across two GNOME versions and an upstream bug report until an xdg-desktop-portal developer caught the mistake --- CHANGELOG.md | 14 ++++ docs/developers/verification.md | 15 +++++ src/libei/portal.py | 60 +++++++++++++++-- tests/test_inputcapture.py | 116 ++++++++++++++++++++++++++++++-- 4 files changed, 193 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 40e8c61..2bfa1ed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,20 @@ All notable changes to python-libei are recorded here. The format follows ### Fixed +- **`InputCaptureSession.wait_for_activation()`/`wait_for_deactivation()` + now actually receive their signals.** Both subscribed to `Activated`/ + `Deactivated` with the *session handle* as the D-Bus object path, but the + portal emits these on its own object (`/org/freedesktop/portal/desktop`), + identifying the session by the signal's first argument instead. The + subscription therefore matched nothing and the wait always ran to its + timeout -- indistinguishable, from the caller's side, from a compositor + that never activates capture, which is exactly how it was misdiagnosed: + as a Mutter bug, across two GNOME versions, a standalone C reproducer and + an upstream bug report, until an xdg-desktop-portal developer pointed out + the mistake. Now subscribes on the portal object and filters on the + payload's session handle, so a second concurrent session's signals are + ignored rather than answered. + - **`_wait_for_signal` (used by `InputCaptureSession.wait_for_activation`/ `wait_for_deactivation`) no longer double-removes its own GLib timeout source on timeout**, which logged a real GLib warning ("Source ID N was diff --git a/docs/developers/verification.md b/docs/developers/verification.md index 71aadd7..1d6ab36 100644 --- a/docs/developers/verification.md +++ b/docs/developers/verification.md @@ -55,6 +55,21 @@ By hand: and unit-tested against a fake connection reproducing its documented shapes (`tests/test_inputcapture.py`) instead. + **What that cost, on record:** `wait_for_activation()`/ + `wait_for_deactivation()` subscribed to their signals on the session's own + object path, where the portal never emits them — so they always ran to + their timeout. The fake connection modelled the same misreading (it fired + a pending signal on whatever path was subscribed), so a wrong + subscription and a wrong fake agreed all the way to a green suite, and + the live symptom — a wait that never returns — was indistinguishable from + a compositor that never activates capture. It was diagnosed as the + latter, across two GNOME versions, a standalone C reproducer and an + upstream Mutter report, before an xdg-desktop-portal developer identified + it (2026-09-09). Fixed, with the fake corrected to emit on the portal + object as a real one does. Two things generalise: a fake written from the + same assumption as the code proves only that they agree, and a + never-arriving signal looks exactly like a signal never sent. + ## Which libei versions this has met - Verified against **libei 1.6.0** on Fedora 44 / GNOME 50.4. diff --git a/src/libei/portal.py b/src/libei/portal.py index 5863a03..b99b636 100644 --- a/src/libei/portal.py +++ b/src/libei/portal.py @@ -989,10 +989,10 @@ def _wait_for_signal( busname: str, interface: str, signal: str, - path: str, + session_handle: str, timeout: float | None, ) -> tuple[Any, ...]: - """Block for one emission of ``signal`` on ``path``, unpacked. + """Block for one emission of ``signal`` for ``session_handle``, unpacked. Unlike `_request`, nothing here *triggers* the signal: ``Activated`` and ``Deactivated`` fire whenever the compositor decides a pointer barrier @@ -1001,6 +1001,22 @@ def _wait_for_signal( late) or not for a long time. ``timeout=None`` waits indefinitely -- the read a caller wants when there is nothing else useful to do but wait for a human to move the pointer. + + **These signals are emitted on the portal object, not on the session + object.** Subscribing with the session handle as the D-Bus object path + -- the obvious reading of "a signal for this session", and what this + did until 2026-09-09 -- matches nothing, delivers nothing, and is + indistinguishable from a compositor that never fires the signal at all: + it cost this project a live investigation across two GNOME versions, a + standalone C reproducer and an upstream Mutter bug report before an + xdg-desktop-portal developer pointed out the mistake. The session is + identified by the signal's own first argument instead (``o + session_handle``, per the portal spec), so the subscription is on + `_OBJECT_PATH` and the filtering happens here, on the payload. It is + deliberately not done with ``signal_subscribe``'s ``arg0`` filter: the + D-Bus specification restricts plain ``arg0=`` match rules to arguments + of type STRING, and this one is an OBJECT_PATH, which is the same shape + of silent non-delivery all over again. """ loop = GLib.MainLoop() result: dict[str, Any] = {} @@ -1017,7 +1033,22 @@ def on_signal( ) -> None: if result: # a subscription that outlives its own wait can fire twice return - result["args"] = params.unpack() + args = params.unpack() + if args[0] != session_handle: + # Logged, not silently dropped: this filter can hide a signal + # that *did* arrive just as effectively as the wrong-path + # subscription above did, and the two are indistinguishable + # from the outside -- both look exactly like a compositor that + # never fired. One debug line is what tells them apart. + logger.debug( + "ignoring %s for session %s while waiting for %s", + signal, + args[0], + session_handle, + ) + return + logger.debug("received %s for session %s", signal, session_handle) + result["args"] = args loop.quit() def on_timeout() -> bool: @@ -1030,12 +1061,20 @@ def on_timeout() -> bool: busname, interface, signal, - path, + _OBJECT_PATH, None, Gio.DBusSignalFlags.NONE, on_signal, 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", + interface, + signal, + _OBJECT_PATH, + session_handle, + ) try: if not result: timeout_source = None @@ -1344,7 +1383,18 @@ def set_pointer_barriers( ) if code != 0: raise PortalDeniedError("SetPointerBarriers") - return list(results.get("failed_barriers", [])) + failed = list(results.get("failed_barriers", [])) + # A partially-refused set is the quiet failure here: the call + # succeeds, some barriers stand, and an edge the caller believes is + # armed simply never triggers. Callers see only the returned list, + # which they may or may not act on -- this says it either way. + logger.debug( + "SetPointerBarriers: %d requested %s, refused: %s", + len(barriers), + [b[0] for b in barriers], + failed or "none", + ) + return failed def wait_for_activation(self, timeout: float | None = None) -> Activation: """Block until the compositor activates capture, or ``timeout``. diff --git a/tests/test_inputcapture.py b/tests/test_inputcapture.py index 9c88479..81c9dea 100644 --- a/tests/test_inputcapture.py +++ b/tests/test_inputcapture.py @@ -64,6 +64,13 @@ class FakeInputCaptureConnection: it. A test that wants the *timeout* path instead simply leaves both at their default of None; FakeMainLoop.pending_timeout picks up from there exactly as it does for every other timeout test in this package. + + Both fire on the **portal's** object path, carrying the session handle + as the signal's first argument, because that is what a real portal + does -- see `_wait_for_signal`'s docstring for what modelling this + wrongly hid. `signal_session_handle` overrides that first argument + alone, so a test can deliver a *different* session's activation to a + subscriber and prove it is ignored. """ def __init__( @@ -75,8 +82,10 @@ def __init__( session_handle: str = "/session/1", pending_activated: dict[str, Any] | None = None, pending_deactivated: dict[str, Any] | None = None, + signal_session_handle: str | None = None, ) -> None: self.session_handle = session_handle + self.signal_session_handle = signal_session_handle or session_handle self.responses = responses or { "Start": (0, {"capabilities": 3}), # v1 CreateSession is Request-shaped too, and answers with the @@ -170,13 +179,25 @@ def signal_subscribe( elif signal == "Activated" and self._pending_activated is not None: payload = self._pending_activated self._pending_activated = None - callback(None, None, path, None, "Activated", FakeReply((path, payload))) + self._emit_session_signal(callback, "Activated", payload) elif signal == "Deactivated" and self._pending_deactivated is not None: payload = self._pending_deactivated self._pending_deactivated = None - callback(None, None, path, None, "Deactivated", FakeReply((path, payload))) + self._emit_session_signal(callback, "Deactivated", payload) return self._next_subscription_id + def _emit_session_signal( + self, callback: Any, signal: str, payload: dict[str, Any] + ) -> None: + callback( + None, + None, + portal._OBJECT_PATH, + None, + signal, + FakeReply((self.signal_session_handle, payload)), + ) + def signal_unsubscribe(self, subscription_id: int) -> None: key = self._subscription_paths.pop(subscription_id, None) if key is not None: @@ -441,6 +462,23 @@ def test_set_pointer_barriers_reports_the_failed_ones() -> None: assert failed == [2] +def test_partially_refused_barriers_are_logged( + caplog: pytest.LogCaptureFixture, +) -> None: + # A partial refusal is returned but nothing forces a caller to look at + # it, and the symptom -- one edge that never triggers -- is silent. The + # log line is what makes it visible without reading the return value. + connection = FakeInputCaptureConnection( + responses={"SetPointerBarriers": (0, {"failed_barriers": [2]})} + ) + with install_fake_gi(connection), caplog.at_level("DEBUG", logger="libei.portal"): + session = portal.InputCaptureSession.negotiate(connection=connection) + session.set_pointer_barriers( + [(1, 0, 0, 1919, 0), (2, 1920, 0, 1920, 1079)], zone_set=7 + ) + assert "refused: [2]" in caplog.text + + def test_an_empty_barrier_list_clears_every_barrier() -> None: connection = FakeInputCaptureConnection() with install_fake_gi(connection): @@ -551,16 +589,80 @@ def test_wait_for_activation_does_not_double_remove_the_timeout_source_on_timeou source_remove.assert_not_called() -def test_wait_for_activation_subscribes_on_this_sessions_own_path() -> None: - # A caller managing several sessions on one MainContext must only ever - # hear about its own session's activation, not another's. +def test_wait_for_activation_subscribes_on_the_portal_not_the_session() -> None: + # The bug that made this whole feature look like a compositor gap: the + # portal emits Activated on its own object, never on the session's, so + # a subscription filtered to the session handle hears nothing at all -- + # identical, from the caller's side, to a compositor that never fires. + # See _wait_for_signal's docstring. + 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] == "Activated" + } + assert subscribed == {portal._OBJECT_PATH} + assert connection.session_handle not in subscribed + + +def test_wait_for_deactivation_subscribes_on_the_portal_not_the_session() -> None: + # Same fix, second call site -- the original bug was in both. connection = FakeInputCaptureConnection() + with install_fake_gi(connection): + session = portal.InputCaptureSession.negotiate(connection=connection) + with pytest.raises(portal.PortalTimeoutError): + session.wait_for_deactivation(timeout=0.01) + subscribed = { + key[2] for key in connection.signal_subscriptions if key[1] == "Deactivated" + } + assert subscribed == {portal._OBJECT_PATH} + + +def test_wait_for_activation_ignores_another_sessions_activation() -> None: + # Subscribing on the shared portal object means hearing every session's + # signals, so the session handle in the payload is now what tells them + # apart. A foreign one must leave the wait running -- here, until it + # times out. + connection = FakeInputCaptureConnection( + pending_activated={"activation_id": 5, "cursor_position": (1.0, 2.0)}, + signal_session_handle="/session/someone-else", + ) with install_fake_gi(connection): session = portal.InputCaptureSession.negotiate(connection=connection) with pytest.raises(portal.PortalTimeoutError): session.wait_for_activation(timeout=0.01) - subscribed_paths = {key[2] for key in connection.signal_subscriptions} - assert connection.session_handle in subscribed_paths + + +def test_ignoring_another_sessions_activation_says_so_at_debug( + caplog: pytest.LogCaptureFixture, +) -> None: + # The point of the log line, not incidental coverage: dropping a signal + # here is invisible from outside and looks identical to a compositor + # that never fired one -- which is the mistake this whole code path is + # being fixed for. A silent filter would just move that trap. + connection = FakeInputCaptureConnection( + pending_activated={"activation_id": 5}, + signal_session_handle="/session/someone-else", + ) + with install_fake_gi(connection), caplog.at_level("DEBUG", logger="libei.portal"): + session = portal.InputCaptureSession.negotiate(connection=connection) + with pytest.raises(portal.PortalTimeoutError): + session.wait_for_activation(timeout=0.01) + assert "/session/someone-else" in caplog.text + assert connection.session_handle in caplog.text + + +def test_wait_for_deactivation_ignores_another_sessions_deactivation() -> None: + connection = FakeInputCaptureConnection( + pending_deactivated={"activation_id": 5}, + signal_session_handle="/session/someone-else", + ) + with install_fake_gi(connection): + session = portal.InputCaptureSession.negotiate(connection=connection) + with pytest.raises(portal.PortalTimeoutError): + session.wait_for_deactivation(timeout=0.01) def test_wait_for_deactivation_returns_the_activation_id() -> None: