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 <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2020-05-05 10:44:21 +09:30
parent 69b07cf5a6
commit 051cbf7cc4
3 changed files with 8 additions and 12 deletions

View file

@ -216,6 +216,7 @@ void plugin_kill(struct plugin *plugin, char *fmt, ...)
} }
check_plugins_resolved(plugin->plugins); 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) { if (err) {
plugin_kill(plugin, "%s", err); plugin_kill(plugin, "%s", err);
return io_close(plugin->stdout_conn); /* plugin_kill frees plugin */
return io_close(NULL);
} }
} while (success); } 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) static void plugin_manifest_timeout(struct plugin *plugin)
{ {
bool startup = plugin->plugins->startup;
plugin_kill(plugin, plugin_kill(plugin,
"failed to respond to \"%s\" in time, terminating.", "failed to respond to \"%s\" in time, terminating.",
plugin->plugin_state == AWAITING_GETMANIFEST_RESPONSE plugin->plugin_state == AWAITING_GETMANIFEST_RESPONSE
? "getmanifest" : "init"); ? "getmanifest" : "init");
if (plugin->plugins->startup) if (startup)
fatal("Can't recover from plugin failure, terminating."); 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, fatal("error starting plugin '%s': %s", p->cmd,
strerror(errno)); strerror(errno));
plugin_kill(p, "error starting: %s", strerror(errno)); plugin_kill(p, "error starting: %s", strerror(errno));
tal_free(p);
} }
return sent; return sent;

View file

@ -97,12 +97,6 @@ plugin_dynamic_startdir(struct command *cmd, const char *dir_path)
return command_still_pending(cmd); 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 * static struct command_result *
plugin_dynamic_stop(struct command *cmd, const char *plugin_name) 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 " "%s cannot be managed when "
"lightningd is up", "lightningd is up",
plugin_name); plugin_name);
clear_plugin(p, plugin_name); plugin_kill(p, "stopped by lightningd via RPC");
response = json_stream_success(cmd); response = json_stream_success(cmd);
if (deprecated_apis) if (deprecated_apis)
json_add_string(response, "", json_add_string(response, "",

View file

@ -229,7 +229,7 @@ def test_plugin_command(node_factory):
# Make sure the plugin behaves normally after stop and restart # Make sure the plugin behaves normally after stop and restart
assert("Successfully stopped helloworld.py." assert("Successfully stopped helloworld.py."
== n.rpc.plugin_stop(plugin="helloworld.py")["result"]) == 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.rpc.plugin_start(plugin=os.path.join(os.getcwd(), "contrib/plugins/helloworld.py"))
n.daemon.wait_for_log(r"Plugin helloworld.py initialized") n.daemon.wait_for_log(r"Plugin helloworld.py initialized")
assert("Hello world" == n.rpc.call(method="hello")) assert("Hello world" == n.rpc.call(method="hello"))
@ -237,7 +237,7 @@ def test_plugin_command(node_factory):
# Now stop the helloworld plugin # Now stop the helloworld plugin
assert("Successfully stopped helloworld.py." assert("Successfully stopped helloworld.py."
== n.rpc.plugin_stop(plugin="helloworld.py")["result"]) == 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 # Make sure that the 'hello' command from the helloworld.py plugin
# is not available anymore. # is not available anymore.
cmd = [hlp for hlp in n.rpc.help()["help"] if "hello" in hlp["command"]] cmd = [hlp for hlp in n.rpc.help()["help"] if "hello" in hlp["command"]]