From 051cbf7cc41ca8f4110c68b42679ae538c459d01 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 5 May 2020 10:44:21 +0930 Subject: [PATCH] lightningd: make plugin_kill() free the plugin. This is what I expected from plugin_kill, and now all the callers do the equivalent anywat, it's easy. Signed-off-by: Rusty Russell --- lightningd/plugin.c | 8 +++++--- lightningd/plugin_control.c | 8 +------- tests/test_plugin.py | 4 ++-- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/lightningd/plugin.c b/lightningd/plugin.c index 8678bf01f..c5c416d4b 100644 --- a/lightningd/plugin.c +++ b/lightningd/plugin.c @@ -216,6 +216,7 @@ void plugin_kill(struct plugin *plugin, char *fmt, ...) } check_plugins_resolved(plugin->plugins); + tal_free(plugin); } /** @@ -483,7 +484,8 @@ static struct io_plan *plugin_read_json(struct io_conn *conn, if (err) { plugin_kill(plugin, "%s", err); - return io_close(plugin->stdout_conn); + /* plugin_kill frees plugin */ + return io_close(NULL); } } while (success); @@ -966,11 +968,12 @@ static const char *plugin_hooks_add(struct plugin *plugin, const char *buffer, static void plugin_manifest_timeout(struct plugin *plugin) { + bool startup = plugin->plugins->startup; plugin_kill(plugin, "failed to respond to \"%s\" in time, terminating.", plugin->plugin_state == AWAITING_GETMANIFEST_RESPONSE ? "getmanifest" : "init"); - if (plugin->plugins->startup) + if (startup) fatal("Can't recover from plugin failure, terminating."); } @@ -1286,7 +1289,6 @@ bool plugins_send_getmanifest(struct plugins *plugins) fatal("error starting plugin '%s': %s", p->cmd, strerror(errno)); plugin_kill(p, "error starting: %s", strerror(errno)); - tal_free(p); } return sent; diff --git a/lightningd/plugin_control.c b/lightningd/plugin_control.c index 115d1b104..96d10daa1 100644 --- a/lightningd/plugin_control.c +++ b/lightningd/plugin_control.c @@ -97,12 +97,6 @@ plugin_dynamic_startdir(struct command *cmd, const char *dir_path) return command_still_pending(cmd); } -static void clear_plugin(struct plugin *p, const char *name) -{ - plugin_kill(p, "%s stopped by lightningd via RPC", name); - tal_free(p); -} - static struct command_result * plugin_dynamic_stop(struct command *cmd, const char *plugin_name) { @@ -116,7 +110,7 @@ plugin_dynamic_stop(struct command *cmd, const char *plugin_name) "%s cannot be managed when " "lightningd is up", plugin_name); - clear_plugin(p, plugin_name); + plugin_kill(p, "stopped by lightningd via RPC"); response = json_stream_success(cmd); if (deprecated_apis) json_add_string(response, "", diff --git a/tests/test_plugin.py b/tests/test_plugin.py index ceffc491b..0e70f9be4 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -229,7 +229,7 @@ def test_plugin_command(node_factory): # Make sure the plugin behaves normally after stop and restart assert("Successfully stopped helloworld.py." == n.rpc.plugin_stop(plugin="helloworld.py")["result"]) - n.daemon.wait_for_log(r"Killing plugin: helloworld.py") + n.daemon.wait_for_log(r"Killing plugin: stopped by lightningd via RPC") n.rpc.plugin_start(plugin=os.path.join(os.getcwd(), "contrib/plugins/helloworld.py")) n.daemon.wait_for_log(r"Plugin helloworld.py initialized") assert("Hello world" == n.rpc.call(method="hello")) @@ -237,7 +237,7 @@ def test_plugin_command(node_factory): # Now stop the helloworld plugin assert("Successfully stopped helloworld.py." == n.rpc.plugin_stop(plugin="helloworld.py")["result"]) - n.daemon.wait_for_log(r"Killing plugin: helloworld.py") + n.daemon.wait_for_log(r"Killing plugin: stopped by lightningd via RPC") # Make sure that the 'hello' command from the helloworld.py plugin # is not available anymore. cmd = [hlp for hlp in n.rpc.help()["help"] if "hello" in hlp["command"]]