From 6b39b3eb74631952c348aa3ef3361d1ecbb8def9 Mon Sep 17 00:00:00 2001 From: LDA Date: Sat, 9 Nov 2024 17:56:42 +0100 Subject: [PATCH] [FIX] Fix SEGV, notify syncers on exit --- src/Main.c | 3 ++- src/Room/V1/Send.c | 2 +- src/User.c | 32 ++++++++++++++++++++++++-------- src/include/User.h | 9 ++++++++- 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/Main.c b/src/Main.c index a08d503..6778fc2 100644 --- a/src/Main.c +++ b/src/Main.c @@ -56,6 +56,7 @@ static Array *httpServers; static volatile int restart; +static MatrixHttpHandlerArgs matrixArgs; static void SignalHandler(int signal) @@ -76,6 +77,7 @@ SignalHandler(int signal) return; } + UserNotifyAll(matrixArgs.db); for (i = 0; i < ArraySize(httpServers); i++) { HttpServer *server = ArrayGet(httpServers, i); @@ -143,7 +145,6 @@ Main(Array * args) /* Signal handling */ struct sigaction sigAction; - MatrixHttpHandlerArgs matrixArgs; Cron *cron; char startDir[PATH_MAX]; diff --git a/src/Room/V1/Send.c b/src/Room/V1/Send.c index 5557885..dce44a7 100644 --- a/src/Room/V1/Send.c +++ b/src/Room/V1/Send.c @@ -323,7 +323,7 @@ RoomAddEventV1(Room *room, PduV1 pdu, PduV1Status status) /* If we have a membership change, then add it to the * proper table. */ - if (RelationFromJson(relates_to, &rel, NULL)) + if (relates_to && RelationFromJson(relates_to, &rel, NULL)) { DbRef *relate = DbLock( room->db, diff --git a/src/User.c b/src/User.c index f011dbb..f1eab86 100644 --- a/src/User.c +++ b/src/User.c @@ -1152,7 +1152,7 @@ UserAddJoin(User *user, char *roomId) data = DbJson(user->joinRef); JsonValueFree(HashMapSet(data, roomId, JsonValueObject(HashMapCreate()))); - UserNotifyUser(user); + UserNotifyUser(user->name); } void @@ -1176,7 +1176,7 @@ UserRemoveJoin(User *user, char *roomId) data = DbJson(user->joinRef); JsonValueFree(HashMapDelete(data, roomId)); - UserNotifyUser(user); + UserNotifyUser(user->name); } Array * UserListJoins(User *user) @@ -1285,7 +1285,7 @@ UserPushInviteSync(User *user, char *roomId) DbUnlock(user->db, syncRef); } DbListFree(entries); - UserNotifyUser(user); + UserNotifyUser(user->name); } void UserPushJoinSync(User *user, char *roomId) @@ -1327,7 +1327,7 @@ UserPushJoinSync(User *user, char *roomId) DbUnlock(user->db, syncRef); } DbListFree(entries); - UserNotifyUser(user); + UserNotifyUser(user->name); } void @@ -1368,7 +1368,7 @@ UserPushEvent(User *user, HashMap *event) } DbListFree(entries); - UserNotifyUser(user); + UserNotifyUser(user->name); } void UserDropSync(User *user, char *batch) @@ -1810,7 +1810,7 @@ UserInitialisePushTable(void) } void -UserNotifyUser(User *user) +UserNotifyUser(char *user) { NotificationEntry *entry; Array *entries; @@ -1820,7 +1820,7 @@ UserNotifyUser(User *user) return; } pthread_mutex_lock(&pushLock); - entries = HashMapGet(pushTable, user->name); + entries = HashMapGet(pushTable, user); size = ArraySize(entries); entry = ArrayGet(entries, 0); if (entry && entry->type == NOTIF_AWAIT) @@ -1850,7 +1850,7 @@ UserNotifyUser(User *user) entry->type = NOTIF_GOTTEN; ArrayAdd(entries, entry); - HashMapSet(pushTable, user->name, entries); + HashMapSet(pushTable, user, entries); pthread_mutex_unlock(&pushLock); return; } @@ -1998,3 +1998,19 @@ UserAwaitNotification(char *user, int await) return notified; } +void +UserNotifyAll(Db *db) +{ + Array *list; + size_t i, len; + if (!db) + { + return; + } + list = DbList(db, 1, "users"); + for (i = 0, len = ArraySize(list); i < len; i++) + { + UserNotifyUser(ArrayGet(list, i)); + } + DbListFree(list); +} diff --git a/src/include/User.h b/src/include/User.h index 56699bf..a7650b3 100644 --- a/src/include/User.h +++ b/src/include/User.h @@ -476,7 +476,14 @@ extern void UserInitialisePushTable(void); /** * Notifies the user of a sync update. */ -extern void UserNotifyUser(User *); +extern void UserNotifyUser(char *); + +/** + * Notifies *all* users of a sync update. This is meant to be used + * in cases where Telodendria needs to gracefully close any longrunning + * /sync requests. + */ +extern void UserNotifyAll(Db *); /** * Waits for a notification for a specific user for a specific