Skip to content

Commit 14b8df1

Browse files
committed
CES-2235 Delay publication of the Charon service to the bus
Tweak the initialization order of the Charon DBus service. We should initially create the `dbus.service.Object` *without* claiming a well-known name on the bus. If we immediately claim a name within the `FileService.__init__()` initializer, the DBus service will (briefly) become visible on the DBus bus in a half-initialized state. The symptom of this is clients seeing empty or incomplete introspection data on `nl.ultimaker.charon`. If, instead, we first allow `FileService` to completely finish its `__init__()`, and then separately claim the well-known name in `FileService.publish()`, this publishes the service to the bus "atomically".
1 parent 0d18ba7 commit 14b8df1

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

Charon/Service/FileService.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,31 @@
1818
# Note: This class does not currently use type hinting since type hints,
1919
# dbus-python decorators and Python 3.4 do not mix well.
2020
class FileService(dbus.service.Object):
21+
2122
def __init__(self, dbus_bus: dbus.Bus) -> None:
23+
self.__dbus_bus = dbus_bus
2224
super().__init__(
23-
bus_name = dbus.service.BusName("nl.ultimaker.charon", dbus_bus),
24-
object_path = "/nl/ultimaker/charon"
25+
conn=self.__dbus_bus,
26+
object_path="/nl/ultimaker/charon",
27+
# Postpone claiming a well-known name until the class is fully initialized.
28+
# If we do this right now, the DBus service will be published to the
29+
# bus in an incomplete state, and clients connecting early on may
30+
# encounter, for instance, emptry introspection data on this service.
31+
bus_name=None,
2532
)
33+
self.__bus_name = None
2634

2735
log.debug("FileService initialized")
2836
self.__queue = RequestQueue.RequestQueue()
2937

38+
## Publish the fully initialized DBus service to the bus
39+
def publish(self) -> None:
40+
# Store a reference to the BusName for as long as the DBus service is alive;
41+
# otherwise it gets GC'd, and the well-known name gets released again.
42+
# Taking ownership of this well-known name is also the trigger for other services
43+
# to notice that we are alive & available.
44+
self.__bus_name = dbus.service.BusName("nl.ultimaker.charon", self.__dbus_bus)
45+
3046
## Start a request for data from a file.
3147
#
3248
# This function will start a request for data from a certain file.

Charon/Service/main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@
3131
_bus = dbus.SystemBus(private=True, mainloop=dbus.mainloop.glib.DBusGMainLoop())
3232

3333
_service = Charon.Service.FileService(_bus)
34+
_service.publish()
3435

3536
_loop.run()

0 commit comments

Comments
 (0)