From 5b76c2fbfa77e2f46ce17f22978c33a2aac58181 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 2 Apr 2024 13:33:49 +1030 Subject: [PATCH] lightningd: fix memleak where we didn't free plugin_hook_request when it was finished. Covered up by notleak() :( Changelog-Fixed: lightingd: slow memory leak when using plugin hooks fixed (introduced in v23.11) Signed-off-by: Rusty Russell --- lightningd/plugin_hook.c | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/lightningd/plugin_hook.c b/lightningd/plugin_hook.c index 62dc9d438ec4..dbd382daff2e 100644 --- a/lightningd/plugin_hook.c +++ b/lightningd/plugin_hook.c @@ -119,6 +119,19 @@ bool plugin_hook_continue(void *unused, const char *buffer, const jsmntok_t *tok return resrestok && json_tok_streq(buffer, resrestok, "continue"); } +static void cleanup_ph_req(struct plugin_hook_request *ph_req) +{ + /* We need to remove the destructors from the remaining + * call-chain, otherwise they'd still be called when the + * plugin dies or we shut down. */ + for (size_t i=0; ihooks); i++) { + tal_del_destructor2(ph_req->hooks[i], + destroy_hook_in_ph_req, ph_req); + } + + tal_free(ph_req); +} + /** * Callback to be passed to the jsonrpc_request. * @@ -159,7 +172,8 @@ static void plugin_hook_callback(const char *buffer, const jsmntok_t *toks, if (!ph_req->hook->deserialize_cb(ph_req->cb_arg, buffer, resulttok)) { tal_free(ph_req->cb_arg); - goto cleanup; + cleanup_ph_req(ph_req); + return; } } else { log_debug(ph_req->ld->log, "Plugin died from %s hook call", @@ -167,18 +181,6 @@ static void plugin_hook_callback(const char *buffer, const jsmntok_t *toks, } plugin_hook_call_next(ph_req); - return; - -cleanup: - /* We need to remove the destructors from the remaining - * call-chain, otherwise they'd still be called when the - * plugin dies or we shut down. */ - for (size_t i=0; ihooks); i++) { - tal_del_destructor2(ph_req->hooks[i], - destroy_hook_in_ph_req, ph_req); - } - - tal_free(ph_req); } static void plugin_hook_call_next(struct plugin_hook_request *ph_req) @@ -192,6 +194,7 @@ static void plugin_hook_call_next(struct plugin_hook_request *ph_req) ph_req->hook_index++; if (ph_req->hook_index >= tal_count(ph_req->hooks)) { ph_req->hook->final_cb(ph_req->cb_arg); + cleanup_ph_req(ph_req); return; } } while (ph_req->hooks[ph_req->hook_index] == NULL);