From afa7d09678a1e262381c5916444a13b94f53ce64 Mon Sep 17 00:00:00 2001 From: "Dennis K. Paulsen" Date: Wed, 21 Oct 2026 14:17:23 -0500 Subject: [PATCH] =?UTF-8?q?fix:=20avoid=20the=20"Source=20ID=20N=20was=20n?= =?UTF-8?q?ot=20found"=20GLib=20critical=20when=20an=20InputCapture=20sign?= =?UTF-8?q?al=20and=20the=20timeout=20deadline=20fall=20in=20one=20poll=20?= =?UTF-8?q?=E2=80=94=20on=5Ftimeout's=20source=20is=20de-registered=20by?= =?UTF-8?q?=20whether=20it=20ran,=20not=20by=20whether=20the=20wait=20time?= =?UTF-8?q?d=20out=20=E2=80=94=20and=20install=20attrs/jinja2=20into=20the?= =?UTF-8?q?=20setup-python=20so=20the=20libei-from-source=20CI=20build=20f?= =?UTF-8?q?inds=20them;=20release=200.8.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/ci.yml | 10 +++++++++ CHANGELOG.md | 2 ++ README.md | 2 +- docs/developers/verification.md | 2 +- pyproject.toml | 2 +- src/libei/__init__.py | 2 +- src/libei/portal.py | 39 ++++++++++++++++++++++++++++----- tests/test_inputcapture.py | 39 +++++++++++++++++++++++++++++++++ tests/test_portal.py | 11 ++++++++++ 9 files changed, 100 insertions(+), 9 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 62c236c..aa6e6db 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -118,12 +118,22 @@ jobs: # 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. + # + # jinja2/attrs go in through BOTH apt and pip on purpose. `meson setup` + # resolves `python3` from PATH, where actions/setup-python has placed + # its own 3.12 ahead of the system one, so it is that interpreter -- + # not the system python3 the apt packages land in -- that must carry + # the modules, or the proto generation fails with "python3 is missing + # modules: attr, jinja2". The pip line installs into the setup-python; + # the apt line stays as the belt-and-braces for a meson that finds the + # system interpreter instead. - 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 + python -m pip install attrs jinja2 - name: Build and install libei ${{ matrix.libei }} from source run: | diff --git a/CHANGELOG.md b/CHANGELOG.md index 2df850a..2ca338f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ All notable changes to python-libei are recorded here. The format follows ## [Unreleased] +## [0.8.0] - 2026-10-20 + ### Added - **A malformed portal signal payload now raises the new typed diff --git a/README.md b/README.md index 11579db..bf01de6 100644 --- a/README.md +++ b/README.md @@ -135,7 +135,7 @@ and why, is in ## Status -Beta (`0.7.0`), published on [PyPI](https://pypi.org/project/python-libei/) +Beta (`0.8.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 01b4dc9..29f5fee 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.7.0`) and the API is not frozen — expect renames +The package is beta (`0.8.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 2ebcde4..fdf6b1d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "python-libei" -version = "0.7.0" +version = "0.8.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 f008723..340b03a 100644 --- a/src/libei/__init__.py +++ b/src/libei/__init__.py @@ -31,6 +31,6 @@ Beta: the API is not frozen. """ -__version__ = "0.7.0" +__version__ = "0.8.0" __all__ = ["__version__"] diff --git a/src/libei/portal.py b/src/libei/portal.py index 6b2ac58..37842a3 100644 --- a/src/libei/portal.py +++ b/src/libei/portal.py @@ -1071,9 +1071,19 @@ def _classify_signal_payload( ``"drop"`` -- another session's; keep waiting (logged, since a dropped signal is otherwise indistinguishable from one that never fired). ``"malformed"`` -- not the ``(o session_handle, a{sv} options)`` shape - the spec gives every signal here; the session filter runs *between* the - two shape checks on purpose, so a malformed payload for another session - is still dropped rather than raised in this waiter. + the spec gives every signal here. + + Two shape checks with the session filter between them, so how a + malformed payload is treated depends on how it is broken. A payload + whose *options* are the wrong shape (the second check) is reached only + after the filter has matched its handle, so another session's is already + dropped before that check runs -- it is raised only for this session. A + payload with no first element, or one that is not a string, cannot be + matched to any session at all: there is no handle to compare it with, so + it is malformed for every possible waiter and is raised here whichever + session it might have been meant for -- the filter cannot run before it, + only after. In the single-session process this almost always is, the + distinction never arises. """ if not args or not isinstance(args[0], str): return "malformed" @@ -1189,6 +1199,16 @@ def _wait_for_signal( result: dict[str, Any] = {} malformed: dict[str, Any] = {} timed_out = False + # Distinct from `timed_out`: whether `on_timeout` ran at all, either + # path. `on_timeout` returns False whichever branch it takes, and False + # is what makes GLib deregister the source -- so the finally block must + # key its "already removed?" check on "did on_timeout fire", not on "did + # the wait time out". When a signal and the deadline land in the same + # poll, on_timeout takes the `_settled()` early return (leaving + # `timed_out` False) yet its False return still removed the source; + # keying on `timed_out` there would double-remove it and trip the very + # "Source ID N was not found" critical the guard exists to avoid. + timeout_fired = False session_closed = False def _settled() -> bool: @@ -1257,7 +1277,11 @@ def on_signal( def on_timeout() -> bool: """Stop the loop and record that it timed out rather than finished.""" - nonlocal timed_out + nonlocal timed_out, timeout_fired + # Set first, before the early return: GLib removes the source the + # moment this returns False (which both paths do), so the finally + # block keys "already removed" on this, not on `timed_out`. + timeout_fired = True if _settled(): # A signal delivered in the same poll as the deadline has # already recorded its outcome; the timeout must not overwrite @@ -1312,7 +1336,12 @@ def on_timeout() -> bool: # every real timeout this session hit, never against # test_portal.py's fake GLib.source_remove, which just # clears pending_timeout with no complaint either way. - if timeout_source is not None and not timed_out: + # Keyed on `timeout_fired` (did on_timeout run), not + # `timed_out` (did the wait time out): a signal landing in + # the same poll as the deadline runs on_timeout down its + # `_settled` early return, which removes the source yet + # leaves `timed_out` False. + if timeout_source is not None and not timeout_fired: GLib.source_remove(timeout_source) finally: connection.signal_unsubscribe(subscription) diff --git a/tests/test_inputcapture.py b/tests/test_inputcapture.py index 6be1c63..0475a55 100644 --- a/tests/test_inputcapture.py +++ b/tests/test_inputcapture.py @@ -32,6 +32,7 @@ class docstring. from libei import portal from test_portal import ( FakeGLibError, + FakeMainLoop, FakeReply, FakeUnixFDList, FakeVariant, @@ -776,6 +777,44 @@ def test_wait_for_activation_does_not_double_remove_the_timeout_source_on_timeou source_remove.assert_not_called() +def test_a_signal_and_the_deadline_in_one_poll_do_not_double_remove_the_source() -> ( + None +): + # The harder half of the same bug: GLib dispatches every source ready in + # one iteration, so a real Activated can land in the *same poll* as the + # timeout deadline. on_signal records the activation and quits; on_timeout + # still runs, sees the wait already settled, and returns False -- which + # GLib treats as "remove this source". The finally block must not remove + # it a second time. Keyed on `timed_out` (which this early return leaves + # False) rather than on "did on_timeout fire", it did -- tripping the + # "Source ID N was not found" critical that is fatal under + # G_DEBUG=fatal-criticals. The fake delivers the signal in the same run() + # as the timeout via inject_before_timeout to model the race. + connection = FakeInputCaptureConnection() # nothing fires on subscribe + + def deliver_activation() -> None: + key = (portal._INPUT_CAPTURE, "Activated", portal._OBJECT_PATH) + connection._subscriptions[key]( + None, + None, + portal._OBJECT_PATH, + None, + "Activated", + FakeReply((connection.session_handle, {"activation_id": 7})), + ) + + with install_fake_gi(connection): + glib = sys.modules["gi.repository.GLib"] + session = portal.InputCaptureSession.negotiate(connection=connection) + FakeMainLoop.inject_before_timeout = deliver_activation + with mock.patch.object(glib, "source_remove") as source_remove: + activation = session.wait_for_activation(timeout=5.0) + # The delivered signal wins -- not a timeout -- and the source the firing + # on_timeout already removed is not removed again. + assert activation.activation_id == 7 + source_remove.assert_not_called() + + 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 diff --git a/tests/test_portal.py b/tests/test_portal.py index 77e42f7..90183b0 100644 --- a/tests/test_portal.py +++ b/tests/test_portal.py @@ -112,6 +112,11 @@ class FakeMainLoop: # What GLib.timeout_add was asked to wait, so a test can check the # Response leg got only the time the call leg left of the deadline. pending_timeout_msec: int | None = None + # A callable a test can set to model real GLib dispatching several + # sources ready in one iteration: it runs just before the stashed + # timeout callback, so a signal it delivers and the timeout both fire in + # the same `run()`. Left None, `run()` behaves exactly as before. + inject_before_timeout: Any = None def __init__(self) -> None: self._quit = False @@ -122,6 +127,10 @@ def run(self) -> None: callback = FakeMainLoop.pending_timeout if callback is not None: FakeMainLoop.pending_timeout = None + inject = FakeMainLoop.inject_before_timeout + if inject is not None: + FakeMainLoop.inject_before_timeout = None + inject() callback() return raise AssertionError("run() called before a synchronous quit()") @@ -296,6 +305,8 @@ def install_fake_gi(connection: Any = None) -> Any: # timeout_add stashes the callback rather than scheduling it; only the # timeout tests, which never quit() the loop, actually let it fire. + FakeMainLoop.inject_before_timeout = None + def timeout_add(ms: int, callback: Any) -> int: FakeMainLoop.pending_timeout = callback FakeMainLoop.pending_timeout_msec = ms