Skip to content

Commit 745cdbb

Browse files
authored
Merge pull request #5 from ctrondlp/P2-fix-request-timeout-double-remove
fix: stop _request() from double-removing its GLib timeout source on …
2 parents 15282b2 + 058e4e3 commit 745cdbb

3 files changed

Lines changed: 64 additions & 4 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ All notable changes to python-libei are recorded here. The format follows
4747
tests' fake `GLib.source_remove` doesn't reproduce the warning, so it was
4848
invisible to the suite.
4949

50+
- **`_request` had the identical double-remove bug as its sibling above,
51+
never fixed alongside it.** `_request` backs every Request-returning
52+
RemoteDesktop/InputCapture call (`CreateSession`, `SelectDevices`,
53+
`Start`, `GetZones`, `SetPointerBarriers`) -- far more heavily used than
54+
`_wait_for_signal` -- so this covers the single most realistic timeout
55+
scenario in the module: a consent dialog nobody answers. Found by a
56+
self-review sweep after the `_wait_for_signal` fix, not live; fixed the
57+
same way, with a test mirroring `test_inputcapture.py`'s existing one for
58+
`_wait_for_signal`.
59+
5060
## [0.5.1] - 2026-09-08
5161

5262
### Fixed

‎src/libei/portal.py‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -486,10 +486,16 @@ def subscribe(path: str) -> None:
486486
try:
487487
loop.run()
488488
finally:
489-
# Removing an already-fired one-shot source is harmless
490-
# (GLib warns at most); leaking a live one holds a reference
491-
# to this closure and fires it into a dead loop later.
492-
GLib.source_remove(timeout_source)
489+
# on_timeout() returns False, which is GLib's own signal to
490+
# deregister a fired one-shot source -- removing it again
491+
# here trips a real "Source ID N was not found when
492+
# attempting to remove it" warning, the same bug already
493+
# fixed for _wait_for_signal below. Only remove it when the
494+
# *Response* woke the loop instead; leaving a live source in
495+
# that case holds a reference to this closure and fires it
496+
# into a dead loop on some later request.
497+
if not timed_out:
498+
GLib.source_remove(timeout_source)
493499
finally:
494500
for subscription in subscriptions:
495501
connection.signal_unsubscribe(subscription)

‎tests/test_portal.py‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,50 @@ def call_sync(
549549
assert excinfo.value.timeout == 0.01
550550

551551

552+
def test_request_does_not_double_remove_the_timeout_source_on_timeout() -> None:
553+
# The same bug already fixed in _wait_for_signal (see
554+
# test_inputcapture.py's test of the same name): on_timeout() returning
555+
# False already deregisters a one-shot GLib timeout source, so calling
556+
# GLib.source_remove on it again logs a real "Source ID N was not found
557+
# when attempting to remove it" warning. _request() had this fixed only
558+
# in its sibling, never here, even though _request is the far more
559+
# heavily used of the two (CreateSession, SelectDevices, Start, GetZones,
560+
# SetPointerBarriers all go through it) -- so this is the single most
561+
# realistic timeout scenario in the module: a consent dialog nobody
562+
# answers. The fake source_remove doesn't model an already-deregistered
563+
# source (it just clears pending_timeout either way), so only asserting
564+
# the call was never made catches this.
565+
class SilentConnection(FakeConnection):
566+
def call_sync(
567+
self,
568+
bus_name: Any,
569+
object_path: Any,
570+
interface: Any,
571+
method: str,
572+
parameters: FakeVariant | None,
573+
reply_type: Any,
574+
flags: Any,
575+
timeout: Any,
576+
cancellable: Any,
577+
) -> FakeReply:
578+
self.calls.append(
579+
(method, None if parameters is None else parameters.value)
580+
)
581+
if method == "Get":
582+
return FakeReply((self.version,))
583+
return FakeReply(()) # accepted, but no Response ever fires
584+
585+
connection = SilentConnection()
586+
with install_fake_gi(connection):
587+
glib = sys.modules["gi.repository.GLib"]
588+
with mock.patch.object(glib, "source_remove") as source_remove:
589+
with pytest.raises(portal.PortalTimeoutError):
590+
portal.RemoteDesktopSession.negotiate(
591+
connection=connection, timeout=0.01
592+
)
593+
source_remove.assert_not_called()
594+
595+
552596
def test_close_closes_an_unclaimed_eis_fd() -> None:
553597
# The fd arrives dup'd and owned. If nobody ever reads `eis_fd` (so it
554598
# was never handed to a Sender, which would close it itself), nothing

0 commit comments

Comments
 (0)