Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/developers/verification.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/libei/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@
Beta: the API is not frozen.
"""

__version__ = "0.7.0"
__version__ = "0.8.0"

__all__ = ["__version__"]
39 changes: 34 additions & 5 deletions src/libei/portal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
39 changes: 39 additions & 0 deletions tests/test_inputcapture.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class docstring.
from libei import portal
from test_portal import (
FakeGLibError,
FakeMainLoop,
FakeReply,
FakeUnixFDList,
FakeVariant,
Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions tests/test_portal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()")
Expand Down Expand Up @@ -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
Expand Down
Loading