Skip to contentSkip to conversation

dbus: manager: don't try to export the same chip twice

PATCH Applied Bartosz Golaszewski 2 participants 9 messages Nov 14, 2025 – Nov 17, 2025 GPIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dbus: manager: don't try to export the same chip twice

From: Bartosz Golaszewski <[email protected]>

It's possible to get a second bind event from udevd (for instance:
manually triggered with udevadm trigger -c bind) on the same GPIO chip so it already existing in the hashmap may actually happen unlike what the comment in daemon.c states. We must not try to export the same chip twice as it will crash the gpio-manager on the subsequent assertion.

Fixes a5ab76da1e0a ("dbus: add the D-Bus daemon, command-line client and tests")
---
 dbus/manager/daemon.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
dbus/manager/daemon.c +6 -1
index 8e1de8a..4627e8c 100644
@@ -688,6 +688,12 @@ static void gpiodbus_daemon_export_chip(GpiodbusDaemon *self, GUdevDevice *dev)
 	gboolean ret;
 
 	devname = g_udev_device_get_name(dev);
+
+	if (g_hash_table_contains(self->chips, devname)) {
+		g_debug("chip %s is already exported", devname);
+		return;
+	}
+
 	devpath = g_udev_device_get_device_file(dev);
 	obj_prefix = g_dbus_object_manager_get_object_path(
 				G_DBUS_OBJECT_MANAGER(self->chip_manager));
@@ -740,7 +746,6 @@ static void gpiodbus_daemon_export_chip(GpiodbusDaemon *self, GUdevDevice *dev)
 
 	ret = g_hash_table_insert(self->chips, g_strdup(devname),
 				  g_steal_pointer(&chip_data));
-	/* It's a programming bug if the chip is already in the hashmap. */
 	g_assert(ret);
 }
 
Signature · 1 line
2.51.0
Tested-by on v1 1/1
#1 lore raw

Hi Bartosz, thanks for the patch!

Quoted 11 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

It's possible to get a second bind event from udevd (for instance:
manually triggered with udevadm trigger -c bind) on the same GPIO chip so it already existing in the hashmap may actually happen unlike what the comment in daemon.c states. We must not try to export the same chip twice as it will crash the gpio-manager on the subsequent assertion.

Fixes a5ab76da1e0a ("dbus: add the D-Bus daemon, command-line client and tests")
Quoted 31 lines

---
 dbus/manager/daemon.c | 7 ++++++-

 1 file changed, 6 insertions(+), 1 deletion(-)
dbus/manager/daemon.c +0 -0
index 8e1de8a..4627e8c 100644
@@ -688,6 +688,12 @@ static void gpiodbus_daemon_export_chip(GpiodbusDaemon *self, GUdevDevice *dev)
  	gboolean ret;


  devname = g_udev_device_get_name(dev);

dbus/manager/daemon.c +6 -0
+
+	if (g_hash_table_contains(self->chips, devname)) {
+		g_debug("chip %s is already exported", devname);
+		return;
+	}
+

  devpath = g_udev_device_get_device_file(dev); obj_prefix = g_dbus_object_manager_get_object_path( G_DBUS_OBJECT_MANAGER(self->chip_manager));

dbus/manager/daemon.c +0 -0
@@ -740,7 +746,6 @@ static void gpiodbus_daemon_export_chip(GpiodbusDaemon *self, GUdevDevice *dev)


  ret = g_hash_table_insert(self->chips, g_strdup(devname), g_steal_pointer(&chip_data));

dbus/manager/daemon.c +0 -1
-	/* It's a programming bug if the chip is already in the hashmap. */

  g_assert(ret);
 }

Signature · 3 lines
Alexander Sverdlin
Siemens AG
www.siemens.com
#2 lore raw
In reply to Bartosz Golaszewski

Hi Bartosz,

Quoted 8 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

It's possible to get a second bind event from udevd (for instance:
manually triggered with udevadm trigger -c bind) on the same GPIO chip so it already existing in the hashmap may actually happen unlike what the comment in daemon.c states. We must not try to export the same chip twice as it will crash the gpio-manager on the subsequent assertion.

I'm still trying to understand the initial race, which made me to report the issue, and I now think that the patch maybe doesn't tackle the root cause.

Initially I suspected that there is a race of gpiodbus_daemon_process_chip_dev() against gpiodbus_daemon_on_uevent() because I thought the gpio-manager crash because of the same assert() in its startup:

gpio-manager[588]: initializing gpio-manager
gpio-manager[588]: gpio-manager started
systemd[1]: Started Centralized GPIO manager daemon.
gpio-manager[588]: /usr/src/debug/glib-2.0/2.78.6/gio/gdbusobjectmanagerserver.c:1128: Error registering manager at /io/gpiod1/chips/gpiochip5: An object is already exported for the interface org.freedesktop.DBus.ObjectManager at /io/gpiod1/chips/gpiochip5
gpio-manager[588]: /usr/src/debug/glib-2.0/2.78.6/gio/gdbusobjectmanagerserver.c:1148: Error registering object at /io/gpiod1/chips/gpiochip5/line0 with interface io.gpiod1.Line: An object is already exported for the interface io.gpiod1.Line at /io/gpiod1/chips/gpiochip5/line0
gpio-manager[588]: /usr/src/debug/glib-2.0/2.78.6/gio/gdbusobjectmanagerserver.c:360: Error registering object at /io/gpiod1/chips/gpiochip5/line1 with interface io.gpiod1.Line: An object is already exported for the interface io.gpiod1.Line at /io/gpiod1/chips/gpiochip5/line1
gpio-manager[588]: /usr/src/debug/glib-2.0/2.78.6/gio/gdbusobjectmanagerserver.c:360: Error registering object at /io/gpiod1/chips/gpiochip5/line2 with interface io.gpiod1.Line: An object is already exported for the interface io.gpiod1.Line at /io/gpiod1/chips/gpiochip5/line2
gpio-manager[588]: /usr/src/debug/glib-2.0/2.78.6/gio/gdbusobjectmanagerserver.c:360: Error registering object at /io/gpiod1/chips/gpiochip5/line3 with interface io.gpiod1.Line: An object is already exported for the interface io.gpiod1.Line at /io/gpiod1/chips/gpiochip5/line3
gpio-manager[588]: g_variant_is_object_path: assertion 'string != NULL' failed
gpio-manager[588]: g_dbus_object_manager_server_unexport_unlocked: assertion 'g_variant_is_object_path (object_path)' failed
gpio-manager[588]: **
gpio-manager[588]: gpio-manager:ERROR:/usr/src/debug/libgpiod/2.2.1/dbus/manager/daemon.c:744:gpiodbus_daemon_export_chip: assertion failed: (ret)
gpio-manager[588]: Bail out! gpio-manager:ERROR:/usr/src/debug/libgpiod/2.2.1/dbus/manager/daemon.c:744:gpiodbus_daemon_export_chip: assertion failed: (ret) systemd[1]: gpio-manager.service: Main process exited, code=dumped, status=6/ABRT
systemd[1]: gpio-manager.service: Failed with result 'core-dump'.

Unfortunately, I still to come up with a way to reproduce it, it's still not clear to me, based on the logs I have, what influences it...
(Above is really a first startup of the gpio-manager, not a restart)

Quoted 33 lines
Fixes a5ab76da1e0a ("dbus: add the D-Bus daemon, command-line client and tests")

---
 dbus/manager/daemon.c | 7 ++++++-

 1 file changed, 6 insertions(+), 1 deletion(-)
dbus/manager/daemon.c +0 -0
index 8e1de8a..4627e8c 100644
@@ -688,6 +688,12 @@ static void gpiodbus_daemon_export_chip(GpiodbusDaemon *self, GUdevDevice *dev)
  	gboolean ret;


  devname = g_udev_device_get_name(dev);

dbus/manager/daemon.c +6 -0
+
+	if (g_hash_table_contains(self->chips, devname)) {
+		g_debug("chip %s is already exported", devname);
+		return;
+	}
+

  devpath = g_udev_device_get_device_file(dev); obj_prefix = g_dbus_object_manager_get_object_path( G_DBUS_OBJECT_MANAGER(self->chip_manager));

dbus/manager/daemon.c +0 -0
@@ -740,7 +746,6 @@ static void gpiodbus_daemon_export_chip(GpiodbusDaemon *self, GUdevDevice *dev)


  ret = g_hash_table_insert(self->chips, g_strdup(devname), g_steal_pointer(&chip_data));

dbus/manager/daemon.c +0 -1
-	/* It's a programming bug if the chip is already in the hashmap. */

  g_assert(ret);
 }

Signature · 3 lines
Alexander Sverdlin
Siemens AG
www.siemens.com
#3 lore raw
Quoted 41 lines · Alexander

Hi Bartosz,

Quoted 8 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

It's possible to get a second bind event from udevd (for instance:
manually triggered with udevadm trigger -c bind) on the same GPIO chip so it already existing in the hashmap may actually happen unlike what the comment in daemon.c states. We must not try to export the same chip twice as it will crash the gpio-manager on the subsequent assertion.

I'm still trying to understand the initial race, which made me to report the issue, and I now think that the patch maybe doesn't tackle the root cause.

Initially I suspected that there is a race of gpiodbus_daemon_process_chip_dev() against gpiodbus_daemon_on_uevent() because I thought the gpio-manager crash because of the same assert() in its startup:

gpio-manager[588]: initializing gpio-manager
gpio-manager[588]: gpio-manager started
systemd[1]: Started Centralized GPIO manager daemon.
gpio-manager[588]: /usr/src/debug/glib-2.0/2.78.6/gio/gdbusobjectmanagerserver.c:1128: Error registering manager at /io/gpiod1/chips/gpiochip5: An object is already exported for the interface org.freedesktop.DBus.ObjectManager at /io/gpiod1/chips/gpiochip5
gpio-manager[588]: /usr/src/debug/glib-2.0/2.78.6/gio/gdbusobjectmanagerserver.c:1148: Error registering object at /io/gpiod1/chips/gpiochip5/line0 with interface io.gpiod1.Line: An object is already exported for the interface io.gpiod1.Line at /io/gpiod1/chips/gpiochip5/line0
gpio-manager[588]: /usr/src/debug/glib-2.0/2.78.6/gio/gdbusobjectmanagerserver.c:360: Error registering object at /io/gpiod1/chips/gpiochip5/line1 with interface io.gpiod1.Line: An object is already exported for the interface io.gpiod1.Line at /io/gpiod1/chips/gpiochip5/line1
gpio-manager[588]: /usr/src/debug/glib-2.0/2.78.6/gio/gdbusobjectmanagerserver.c:360: Error registering object at /io/gpiod1/chips/gpiochip5/line2 with interface io.gpiod1.Line: An object is already exported for the interface io.gpiod1.Line at /io/gpiod1/chips/gpiochip5/line2
gpio-manager[588]: /usr/src/debug/glib-2.0/2.78.6/gio/gdbusobjectmanagerserver.c:360: Error registering object at /io/gpiod1/chips/gpiochip5/line3 with interface io.gpiod1.Line: An object is already exported for the interface io.gpiod1.Line at /io/gpiod1/chips/gpiochip5/line3
gpio-manager[588]: g_variant_is_object_path: assertion 'string != NULL' failed
gpio-manager[588]: g_dbus_object_manager_server_unexport_unlocked: assertion 'g_variant_is_object_path (object_path)' failed
gpio-manager[588]: **
gpio-manager[588]: gpio-manager:ERROR:/usr/src/debug/libgpiod/2.2.1/dbus/manager/daemon.c:744:gpiodbus_daemon_export_chip: assertion failed: (ret)
gpio-manager[588]: Bail out! gpio-manager:ERROR:/usr/src/debug/libgpiod/2.2.1/dbus/manager/daemon.c:744:gpiodbus_daemon_export_chip: assertion failed: (ret) systemd[1]: gpio-manager.service: Main process exited, code=dumped, status=6/ABRT
systemd[1]: gpio-manager.service: Failed with result 'core-dump'.

Quoted 4 lines

Unfortunately, I still to come up with a way to reproduce it, it's still not clear to me, based on the logs I have, what influences it...
(Above is really a first startup of the gpio-manager, not a restart)

Is this with my patch though? gpio-manager does spawn 4 additional threads but this is internal to GLib and GDBus and AFAIK all the processing we explicitly do in the daemon happens in a single thread. Not sure how this would race.

Bart

#4 lore raw

Hi Bartosz,

Quoted 40 lines · Bartosz Golaszewski
Quoted 35 lines
Quoted 6 lines

It's possible to get a second bind event from udevd (for instance:
manually triggered with udevadm trigger -c bind) on the same GPIO chip so it already existing in the hashmap may actually happen unlike what the comment in daemon.c states. We must not try to export the same chip twice as it will crash the gpio-manager on the subsequent assertion.

I'm still trying to understand the initial race, which made me to report the issue, and I now think that the patch maybe doesn't tackle the root cause.

Initially I suspected that there is a race of gpiodbus_daemon_process_chip_dev() against gpiodbus_daemon_on_uevent() because I thought the gpio-manager crash because of the same assert() in its startup:

gpio-manager[588]: initializing gpio-manager
gpio-manager[588]: gpio-manager started
systemd[1]: Started Centralized GPIO manager daemon.
gpio-manager[588]: /usr/src/debug/glib-2.0/2.78.6/gio/gdbusobjectmanagerserver.c:1128: Error registering manager at /io/gpiod1/chips/gpiochip5: An object is already exported for the interface org.freedesktop.DBus.ObjectManager at /io/gpiod1/chips/gpiochip5
gpio-manager[588]: /usr/src/debug/glib-2.0/2.78.6/gio/gdbusobjectmanagerserver.c:1148: Error registering object at /io/gpiod1/chips/gpiochip5/line0 with interface io.gpiod1.Line: An object is already exported for the interface io.gpiod1.Line at /io/gpiod1/chips/gpiochip5/line0
gpio-manager[588]: /usr/src/debug/glib-2.0/2.78.6/gio/gdbusobjectmanagerserver.c:360: Error registering object at /io/gpiod1/chips/gpiochip5/line1 with interface io.gpiod1.Line: An object is already exported for the interface io.gpiod1.Line at /io/gpiod1/chips/gpiochip5/line1
gpio-manager[588]: /usr/src/debug/glib-2.0/2.78.6/gio/gdbusobjectmanagerserver.c:360: Error registering object at /io/gpiod1/chips/gpiochip5/line2 with interface io.gpiod1.Line: An object is already exported for the interface io.gpiod1.Line at /io/gpiod1/chips/gpiochip5/line2
gpio-manager[588]: /usr/src/debug/glib-2.0/2.78.6/gio/gdbusobjectmanagerserver.c:360: Error registering object at /io/gpiod1/chips/gpiochip5/line3 with interface io.gpiod1.Line: An object is already exported for the interface io.gpiod1.Line at /io/gpiod1/chips/gpiochip5/line3
gpio-manager[588]: g_variant_is_object_path: assertion 'string != NULL' failed
gpio-manager[588]: g_dbus_object_manager_server_unexport_unlocked: assertion 'g_variant_is_object_path (object_path)' failed
gpio-manager[588]: **
gpio-manager[588]: gpio-manager:ERROR:/usr/src/debug/libgpiod/2.2.1/dbus/manager/daemon.c:744:gpiodbus_daemon_export_chip: assertion failed: (ret)
gpio-manager[588]: Bail out! gpio-manager:ERROR:/usr/src/debug/libgpiod/2.2.1/dbus/manager/daemon.c:744:gpiodbus_daemon_export_chip: assertion failed: (ret) systemd[1]: gpio-manager.service: Main process exited, code=dumped, status=6/ABRT
systemd[1]: gpio-manager.service: Failed with result 'core-dump'.

Quoted 4 lines

Unfortunately, I still to come up with a way to reproduce it, it's still not clear to me, based on the logs I have, what influences it...
(Above is really a first startup of the gpio-manager, not a restart)

Quoted 4 lines

Is this with my patch though? gpio-manager does spawn 4 additional threads but this is internal to GLib and GDBus and AFAIK all the processing we explicitly do in the daemon happens in a single thread. Not sure how this would race.

no, the above log is from the unpatched version, the original race. I'd say that with the patch the race would become more subtle (less probable, but ultimately not worked around).

But never mind, I think the patch will always be required for the case of re-triggered uevents (when it doesn't race with startup and gpiodbus_daemon_process_chip_dev() any longer).

I'll try to reproduce gpiodbus_daemon_process_chip_dev() vs gpiodbus_daemon_on_uevent() race (if there is one)...

Signature · 3 lines
Alexander Sverdlin
Siemens AG
www.siemens.com
#5 lore raw
Quoted 13 lines · Alexander

Hi Bartosz,

Quoted 5 lines

Is this with my patch though? gpio-manager does spawn 4 additional threads but this is internal to GLib and GDBus and AFAIK all the processing we explicitly do in the daemon happens in a single thread. Not sure how this would race.

no, the above log is from the unpatched version, the original race. I'd say that with the patch the race would become more subtle (less probable, but ultimately not worked around).

Quoted 7 lines

But never mind, I think the patch will always be required for the case of re-triggered uevents (when it doesn't race with startup and gpiodbus_daemon_process_chip_dev() any longer).

I'll try to reproduce gpiodbus_daemon_process_chip_dev() vs gpiodbus_daemon_on_uevent() race (if there is one)...

But they cannot possibly race, they run - and I just verified it - on the same thread.

Bartosz

#6 lore raw

Hi Bartosz,

Quoted 13 lines · Bartosz Golaszewski
Quoted 13 lines
Quoted 4 lines

Is this with my patch though? gpio-manager does spawn 4 additional threads but this is internal to GLib and GDBus and AFAIK all the processing we explicitly do in the daemon happens in a single thread. Not sure how this would race.

no, the above log is from the unpatched version, the original race. I'd say that with the patch the race would become more subtle (less probable, but ultimately not worked around).

But never mind, I think the patch will always be required for the case of re-triggered uevents (when it doesn't race with startup and gpiodbus_daemon_process_chip_dev() any longer).

Quoted 6 lines
Quoted 3 lines

I'll try to reproduce gpiodbus_daemon_process_chip_dev() vs gpiodbus_daemon_on_uevent() race (if there is one)...

But they cannot possibly race, they run - and I just verified it - on the same thread.

thanks for double-checking!

Seems that my initial issue has been caused by slightly delayed uevents then, arriving right after registrartion from gpiodbus_daemon_process_chip_dev() in a normal system startup.

And you patch shall fix this scenario as well.

Thank you and have a nice weekend!

Signature · 3 lines
Alexander Sverdlin
Siemens AG
www.siemens.com
#7 lore raw
In reply to Sverdlin, Alexander

Hi Bartosz,

Quoted 37 lines · Alexander Sverdlin
Quoted 8 lines

From: Bartosz Golaszewski <[email protected]>

It's possible to get a second bind event from udevd (for instance:
manually triggered with udevadm trigger -c bind) on the same GPIO chip so it already existing in the hashmap may actually happen unlike what the comment in daemon.c states. We must not try to export the same chip twice as it will crash the gpio-manager on the subsequent assertion.

I'm still trying to understand the initial race, which made me to report the issue, and I now think that the patch maybe doesn't tackle the root cause.

Initially I suspected that there is a race of gpiodbus_daemon_process_chip_dev() against gpiodbus_daemon_on_uevent() because I thought the gpio-manager crash because of the same assert() in its startup:

gpio-manager[588]: initializing gpio-manager
gpio-manager[588]: gpio-manager started
systemd[1]: Started Centralized GPIO manager daemon.
gpio-manager[588]: /usr/src/debug/glib-2.0/2.78.6/gio/gdbusobjectmanagerserver.c:1128: Error registering manager at /io/gpiod1/chips/gpiochip5: An object is already exported for the interface org.freedesktop.DBus.ObjectManager at /io/gpiod1/chips/gpiochip5
gpio-manager[588]: /usr/src/debug/glib-2.0/2.78.6/gio/gdbusobjectmanagerserver.c:1148: Error registering object at /io/gpiod1/chips/gpiochip5/line0 with interface io.gpiod1.Line: An object is already exported for the interface io.gpiod1.Line at /io/gpiod1/chips/gpiochip5/line0
gpio-manager[588]: /usr/src/debug/glib-2.0/2.78.6/gio/gdbusobjectmanagerserver.c:360: Error registering object at /io/gpiod1/chips/gpiochip5/line1 with interface io.gpiod1.Line: An object is already exported for the interface io.gpiod1.Line at /io/gpiod1/chips/gpiochip5/line1
gpio-manager[588]: /usr/src/debug/glib-2.0/2.78.6/gio/gdbusobjectmanagerserver.c:360: Error registering object at /io/gpiod1/chips/gpiochip5/line2 with interface io.gpiod1.Line: An object is already exported for the interface io.gpiod1.Line at /io/gpiod1/chips/gpiochip5/line2
gpio-manager[588]: /usr/src/debug/glib-2.0/2.78.6/gio/gdbusobjectmanagerserver.c:360: Error registering object at /io/gpiod1/chips/gpiochip5/line3 with interface io.gpiod1.Line: An object is already exported for the interface io.gpiod1.Line at /io/gpiod1/chips/gpiochip5/line3
gpio-manager[588]: g_variant_is_object_path: assertion 'string != NULL' failed
gpio-manager[588]: g_dbus_object_manager_server_unexport_unlocked: assertion 'g_variant_is_object_path (object_path)' failed
gpio-manager[588]: **
gpio-manager[588]: gpio-manager:ERROR:/usr/src/debug/libgpiod/2.2.1/dbus/manager/daemon.c:744:gpiodbus_daemon_export_chip: assertion failed: (ret)
gpio-manager[588]: Bail out! gpio-manager:ERROR:/usr/src/debug/libgpiod/2.2.1/dbus/manager/daemon.c:744:gpiodbus_daemon_export_chip: assertion failed: (ret) systemd[1]: gpio-manager.service: Main process exited, code=dumped, status=6/ABRT
systemd[1]: gpio-manager.service: Failed with result 'core-dump'.

Quoted 3 lines

Unfortunately, I still to come up with a way to reproduce it, it's still not clear to me, based on the logs I have, what influences it...
(Above is really a first startup of the gpio-manager, not a restart)

looks like D-Bus related functions, like g_dbus_object_manager_server_export() may call g_main_context_iteration() which may in turn process uevents and call gpiodbus_daemon_on_uevent()...

I'll try to prove it with some code instrumentation...

Signature · 3 lines
Alexander Sverdlin
Siemens AG
www.siemens.com
author Applied
#8 lore raw
In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Quoted 8 lines · Bartosz Golaszewski

It's possible to get a second bind event from udevd (for instance:
manually triggered with udevadm trigger -c bind) on the same GPIO chip so it already existing in the hashmap may actually happen unlike what the comment in daemon.c states. We must not try to export the same chip twice as it will crash the gpio-manager on the subsequent assertion.

[...]

Applied, thanks!

[1/1] dbus: manager: don't try to export the same chip twice

      https://git.kernel.org/brgl/libgpiod/c/859b3c7106589831458962cb0eb06a0c5e1b679d

Best regards,

Signature · 1 line
Bartosz Golaszewski <[email protected]>