From 058e4e3320ff169a0de597815902ec9fa4fc00c8 Mon Sep 17 00:00:00 2001 From: "Dennis K. Paulsen" Date: Thu, 10 Sep 2026 16:23:24 -0500 Subject: [PATCH] fix: stop _request() from double-removing its GLib timeout source on timeout --- CHANGELOG.md | 10 ++++++++++ src/libei/portal.py | 14 ++++++++++---- tests/test_portal.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2bfa1ed..8796273 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -47,6 +47,16 @@ All notable changes to python-libei are recorded here. The format follows tests' fake `GLib.source_remove` doesn't reproduce the warning, so it was invisible to the suite. +- **`_request` had the identical double-remove bug as its sibling above, + never fixed alongside it.** `_request` backs every Request-returning + RemoteDesktop/InputCapture call (`CreateSession`, `SelectDevices`, + `Start`, `GetZones`, `SetPointerBarriers`) -- far more heavily used than + `_wait_for_signal` -- so this covers the single most realistic timeout + scenario in the module: a consent dialog nobody answers. Found by a + self-review sweep after the `_wait_for_signal` fix, not live; fixed the + same way, with a test mirroring `test_inputcapture.py`'s existing one for + `_wait_for_signal`. + ## [0.5.1] - 2026-09-08 ### Fixed diff --git a/src/libei/portal.py b/src/libei/portal.py index b99b636..9a43a40 100644 --- a/src/libei/portal.py +++ b/src/libei/portal.py @@ -486,10 +486,16 @@ def subscribe(path: str) -> None: try: loop.run() finally: - # Removing an already-fired one-shot source is harmless - # (GLib warns at most); leaking a live one holds a reference - # to this closure and fires it into a dead loop later. - GLib.source_remove(timeout_source) + # on_timeout() returns False, which is GLib's own signal to + # deregister a fired one-shot source -- removing it again + # here trips a real "Source ID N was not found when + # attempting to remove it" warning, the same bug already + # fixed for _wait_for_signal below. Only remove it when the + # *Response* woke the loop instead; leaving a live source in + # that case holds a reference to this closure and fires it + # into a dead loop on some later request. + if not timed_out: + GLib.source_remove(timeout_source) finally: for subscription in subscriptions: connection.signal_unsubscribe(subscription) diff --git a/tests/test_portal.py b/tests/test_portal.py index e7dd865..49257f5 100644 --- a/tests/test_portal.py +++ b/tests/test_portal.py @@ -549,6 +549,50 @@ def call_sync( assert excinfo.value.timeout == 0.01 +def test_request_does_not_double_remove_the_timeout_source_on_timeout() -> None: + # The same bug already fixed in _wait_for_signal (see + # test_inputcapture.py's test of the same name): on_timeout() returning + # False already deregisters a one-shot GLib timeout source, so calling + # GLib.source_remove on it again logs a real "Source ID N was not found + # when attempting to remove it" warning. _request() had this fixed only + # in its sibling, never here, even though _request is the far more + # heavily used of the two (CreateSession, SelectDevices, Start, GetZones, + # SetPointerBarriers all go through it) -- so this is the single most + # realistic timeout scenario in the module: a consent dialog nobody + # answers. The fake source_remove doesn't model an already-deregistered + # source (it just clears pending_timeout either way), so only asserting + # the call was never made catches this. + class SilentConnection(FakeConnection): + def call_sync( + self, + bus_name: Any, + object_path: Any, + interface: Any, + method: str, + parameters: FakeVariant | None, + reply_type: Any, + flags: Any, + timeout: Any, + cancellable: Any, + ) -> FakeReply: + self.calls.append( + (method, None if parameters is None else parameters.value) + ) + if method == "Get": + return FakeReply((self.version,)) + return FakeReply(()) # accepted, but no Response ever fires + + connection = SilentConnection() + with install_fake_gi(connection): + glib = sys.modules["gi.repository.GLib"] + with mock.patch.object(glib, "source_remove") as source_remove: + with pytest.raises(portal.PortalTimeoutError): + portal.RemoteDesktopSession.negotiate( + connection=connection, timeout=0.01 + ) + source_remove.assert_not_called() + + def test_close_closes_an_unclaimed_eis_fd() -> None: # The fd arrives dup'd and owned. If nobody ever reads `eis_fd` (so it # was never handed to a Sender, which would close it itself), nothing