dbus2 / zbus-old

Rust D-Bus crate.
https://gitlab.freedesktop.org/dbus/zbus
Other
49 stars 13 forks source link

ObjectServer::at destroys interfaces and returns false when one already exists #245

Open zeenix opened 2 years ago

zeenix commented 2 years ago

In GitLab by @richard.maw on Jan 3, 2022, 19:11

I have a D-Bus interface with a method for creating a connection object that should return an object path referring to that connection. I can't guarantee a unique ID from the underlying type, such as port number, since it may be a WebRTC connection and can change which ports are used over the life of the connection.

My first idea for how to solve this is to generate a random number to use as an ID, and in the unlikely event of a collision to generate a new one and retry. However ObjectServer::at can't be used to detect a collision and retry because it takes the Interface by value, and if there's already an Interface of that type bound to the object path it destroys the Interface and returns Ok(false).

As a work-around I am checking if there's already a bound Interface with ObjectServer::interface, but there's the (unlikely in practice) chance of a race where another thread may bind an Interface between checking.

An alternative design of having the connection ID not be an object path, and pass as a parameter to an interface at a fixed path is not ideal, since that would duplicate lifetime management.