Skip to content

Commit

Permalink
lightningd: in second shutdown_plugin, trigger EOF in plugin stdin
Browse files Browse the repository at this point in the history
So a db_write plugin can still self-terminate when its ready for it.

Credit to ZmnSCPxj for the EOF idea, see issue ElementsProject#4785
  • Loading branch information
SimonVrouwe committed Jan 4, 2023
1 parent f454710 commit 952936c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
15 changes: 11 additions & 4 deletions lightningd/plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -2167,8 +2167,9 @@ void shutdown_plugins(struct lightningd *ld, bool first_call)
continue;
}

/* First call: Send shutdown notification while jsonrpc still exists */
/* First call: */
if (first_call) {
/* Send shutdown notifications to all while jsonrpc still exists */
notified = notify_plugin_shutdown(ld, p);

/* Always keep db_write plugins alive in first call */
Expand All @@ -2181,10 +2182,16 @@ void shutdown_plugins(struct lightningd *ld, bool first_call)
else
tal_free(p);

/* Second call: Only wait if plugin got notified in first call */
/* Second call: */
} else {
p->plugin_state = SHUTDOWN;
if (!plugin_subscriptions_contains(p, "shutdown"))
/* Only wait if the plugin got notified in first call */
if (plugin_subscriptions_contains(p, "shutdown")) {
p->plugin_state = SHUTDOWN;

/* io_close triggers EOF at plugin's stdin read-loop, so it can
* still self-terminate when its ready (within 30s). */
io_close(p->stdin_conn);
} else
tal_free(p);
}
}
Expand Down
13 changes: 9 additions & 4 deletions lightningd/plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,17 @@ void plugin_kill(struct plugin *plugin, enum log_level loglevel,
const char *fmt, ...);

/**
* Tell plugins we're shutting down, and free them.
* Tell plugins we're shutting down, maybe wait for them to self-terminate and
* free them.
*
* @skip_db_write_plugins - keep db_write plugins alive, i.e. the ones that
* registered the db_write hook
* @first_call=true: keeps db_write plugins alive, i.e. the ones that
* registered the db_write hook, but they still receive a shutdown notification.
*
* @first_call=false: shutdown remaining plugins: if it subscribed to shutdown
* then trigger EOF on stdin and wait for it to self-terminate, otherwise kill
* it immediately.
*/
void shutdown_plugins(struct lightningd *ld, bool first);
void shutdown_plugins(struct lightningd *ld, bool first_call);

/**
* Returns the plugin which registers the command with name {cmd_name}
Expand Down

0 comments on commit 952936c

Please sign in to comment.