altdesktop / python-dbus-next

🚌 The next great DBus library for Python with asyncio support
https://python-dbus-next.readthedocs.io/en/latest/
MIT License
191 stars 60 forks source link

Support file descriptors #151

Closed mike-matera closed 1 year ago

mike-matera commented 1 year ago

Though the documentation states that FDs are in the type system the following code fails with a hang:

import asyncio
import dbus_next 
from dbus_next.aio import MessageBus

async def main():
    bus = await MessageBus(bus_type=dbus_next.constants.BusType.SYSTEM).connect()
    introspection = await bus.introspect('org.freedesktop.login1', '/org/freedesktop/login1')
    obj = bus.get_proxy_object('org.freedesktop.login1', '/org/freedesktop/login1', introspection)
    manager = obj.get_interface('org.freedesktop.login1.Manager')
    infd = await manager.call_inhibit("sleep", "My Program", "Wait while I save...", "delay")
    print("My inhibit fd is:", infd)

asyncio.run(main())
mike-matera commented 1 year ago

To anyone reading this! Don't suffer like I did. This library supports FDs if you change the code to this:

bus = await MessageBus(bus_type=dbus_next.constants.BusType.SYSTEM, negotiate_unix_fd=True).connect()
elParaguayo commented 1 year ago

@mike-matera Firstly, thank you for posting this. I was wondering why my code was hanging and had traced it to the client not supporting file descriptors which, in turn, led me here.

I'm looking to implement exactly the same think (i.e. a sleep inhibitor) but, for some reason, python isn't closing the file descriptor that's returned by the call to Inhibit. I can call os.close on it without error but the inhibitor isn't removed.

Do you have some fuller code that shows how you did it? Appreciate that this is almost 6 months after you posted this but thought I'd ask!

EDIT: I may have an idea as to my issue. I'm using the low-level interface and I was reading the spec and I see that the value returned by the inhibit call is the index to the relevant FD in the array of FDs that is passed with the message. I had just done a shortcut and taken the first FD in the array as I had just been getting one FD. I guess I'm getting more now!