From 737a2052a60ef7abf20d5b9b36b2ecd5b9fc2a8c Mon Sep 17 00:00:00 2001 From: Arkadiusz Bokowy Date: Fri, 30 Sep 2022 23:36:37 +0200 Subject: [PATCH] [Linux] Mark GLib source as removed when exiting callback (#22841) * [Linux] Mark GLib source as removed when exiting callback Fixes #22840 * Add TODO for investigating GLib watch tag ID some more --- src/platform/Linux/bluez/Helper.cpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/platform/Linux/bluez/Helper.cpp b/src/platform/Linux/bluez/Helper.cpp index 1337c4e69a1003..f65dbde49862f8 100644 --- a/src/platform/Linux/bluez/Helper.cpp +++ b/src/platform/Linux/bluez/Helper.cpp @@ -401,7 +401,17 @@ static gboolean BluezCharacteristicWriteFD(GIOChannel * aChannel, GIOCondition a isSuccess = true; exit: - return isSuccess ? TRUE : FALSE; + if (!isSuccess && (conn != nullptr)) + { + // Returning G_SOURCE_REMOVE from the source callback removes the source object + // from the context. Unset self source ID tag, so we will not call g_source_remove() + // in BluezOTConnectionDestroy() on already removed source. + // + // TODO: Investigate whether there is a batter way to handle this. + conn->mC1Channel.mWatch = 0; + } + + return isSuccess ? G_SOURCE_CONTINUE : G_SOURCE_REMOVE; } static void Bluez_gatt_characteristic1_complete_acquire_write_with_fd(GDBusMethodInvocation * invocation, int fd, guint16 mtu) @@ -417,6 +427,13 @@ static void Bluez_gatt_characteristic1_complete_acquire_write_with_fd(GDBusMetho static gboolean bluezCharacteristicDestroyFD(GIOChannel * aChannel, GIOCondition aCond, gpointer apClosure) { + BluezConnection * conn = static_cast(apClosure); + // Returning G_SOURCE_REMOVE from the source callback removes the source object + // from the context. Unset self source ID tag, so we will not call g_source_remove() + // in BluezOTConnectionDestroy() on already removed source. + // + // TODO: Investigate whether there is a batter way to handle this. + conn->mC2Channel.mWatch = 0; return G_SOURCE_REMOVE; }