From 9c8abf6ccfc484ae3d87f904bbe0c926f6b9d095 Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Sun, 9 Aug 2020 12:29:38 +0200
Subject: [PATCH] Adding logs to understand why lektord is not exiting on 'lkt
 adm kill'

---
 src/base/commands.c      | 5 ++++-
 src/module/module_repo.c | 6 +++++-
 src/module/module_sdl2.c | 4 ++++
 3 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/src/base/commands.c b/src/base/commands.c
index 2795b388..5fc5b0ac 100644
--- a/src/base/commands.c
+++ b/src/base/commands.c
@@ -71,15 +71,18 @@ command_rescan(struct lkt_state *srv, size_t c, char *argv[LKT_MESSAGE_ARGS_MAX]
     return ! MOD_PROC(srv->repo_mod, "rescan");
 }
 
-inline bool
+bool
 command_kill(struct lkt_state *srv, size_t c,
              char __attribute__((unused)) *argv[LKT_MESSAGE_ARGS_MAX])
 {
     RETURN_UNLESS(lkt_client_auth(srv, c, false), "Failed to authentificate user", false);
     LOG_INFO("GENERAL",  "Stopping lektord");
+    MOD_PROC(srv->repo_mod, "free");
+    MOD_PROC(srv->window_mod, "free");
     close(srv->fds[0].fd);
     lkt_queue_free(&srv->queue);
     database_close_all();
+    LOG_INFO("GENERAL", "lektord will now exit");
     exit(EXIT_SUCCESS);
 }
 
diff --git a/src/module/module_repo.c b/src/module/module_repo.c
index 348756da..2e08eedd 100644
--- a/src/module/module_repo.c
+++ b/src/module/module_repo.c
@@ -401,10 +401,12 @@ __worker_rescan(void *__repo)
 static inline void
 module_repo_close(struct module_repo_internal *repo)
 {
+    LOG_INFO("REPO", "Waiting for workers to finish");
     worker_pool_waitall(&repo->workers);
+    LOG_INFO("REPO", "All workers have finished");
 }
 
-static bool
+static inline bool
 module_repo_new(struct module_repo_internal *repo_, struct queue *queue, volatile sqlite3 *db)
 {
     RETURN_UNLESS(repo_ && queue && db, "Invalid argument", 1);
@@ -504,6 +506,7 @@ mod_free(va_list *va)
     repo = (struct module_repo_internal **) va_arg(copy, void **);
 
     module_repo_close(*repo);
+    worker_pool_free(&(*repo)->workers);
     --__curl_init;
     if (!__curl_init)
         curl_global_cleanup();
@@ -513,6 +516,7 @@ mod_free(va_list *va)
     free((*repo)->base_url);
     free((*repo)->get_all_json);
 
+    LOG_INFO("REPO", "Repo module terminated");
     va_end(copy);
     return 0;
 }
diff --git a/src/module/module_sdl2.c b/src/module/module_sdl2.c
index 6b4d4e24..874756c3 100644
--- a/src/module/module_sdl2.c
+++ b/src/module/module_sdl2.c
@@ -452,6 +452,7 @@ module_sdl2_close(struct module_sdl2_window *win)
     RETURN_UNLESS(win->mpv, "Missing mpv ctx", NOTHING);
     static const char *cmd[] = { "stop", NULL };
     mpv_command_async((mpv_handle *) win->mpv, 0, cmd);
+    LOG_INFO("SDL2", "Module closed");
     /* TODO: make the SDL window background be black. */
 }
 
@@ -459,10 +460,13 @@ static void
 module_sdl2_free(struct module_sdl2_window *win)
 {
     RETURN_UNLESS(win, "Invalid arguments", NOTHING);
+    module_sdl2_close(win);
+    LOG_INFO("SDL2", "Waiting for window thread");
     win->launched = 2;
     while (win->launched)
         sched_yield();
     poller_join(&win->self, NULL);
+    LOG_INFO("SDL2", "Module terminated");
 }
 
 static bool
-- 
GitLab