mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-19 23:24:27 +01:00
plugins/libplugin.c: Allow freeing notification struct command *.
We always allocate a new `struct command` when we get a full JSON object from stdin:b2df01dc73/plugins/libplugin.c (L1229-L1233)If it happens to be a notification, we pass the `struct command` to the handler, and not free it ourselves:b2df01dc73/plugins/libplugin.c (L1270-L1275)There are only nine points in `plugins/libplugin.c` where we `tal_free` anything, and only one of them frees a `struct command`:b2df01dc73/plugins/libplugin.c (L224-L234)The above function `command_complete` is not appropriate for notification handlers; the above function sends out a response to our stdout, which a notification handler should not do. However, as-is, it does mean that notification handling leaks `struct command` objects, which can be problematic if we ever have future built-in plugins which are significantly more dependent on notifications. This commit changes notification handlers to return `struct command_result *`, because possibly in the future notification handlers may want to perform `send_outreq`, so we might as well use our standard convention for callbacks, and to encourage future developers to check how to properly terminate notification handlers (and free up the `struct command`). We also now provide a `notification_handled` function which a notification handler must eventually call, as well as a `notification_handler_pending` which is just a snowclone of `command_still_pending`.
This commit is contained in:
committed by
Rusty Russell
parent
e393791d4b
commit
d4690358d9
@@ -708,7 +708,7 @@ json_rbf_channel_call(struct command *cmd,
|
|||||||
return send_outreq(cmd->plugin, req);
|
return send_outreq(cmd->plugin, req);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void json_disconnect(struct command *cmd,
|
static struct command_result *json_disconnect(struct command *cmd,
|
||||||
const char *buf,
|
const char *buf,
|
||||||
const jsmntok_t *params)
|
const jsmntok_t *params)
|
||||||
{
|
{
|
||||||
@@ -730,9 +730,11 @@ static void json_disconnect(struct command *cmd,
|
|||||||
type_to_string(tmpctx, struct node_id, &id));
|
type_to_string(tmpctx, struct node_id, &id));
|
||||||
|
|
||||||
cleanup_peer_pending_opens(&id);
|
cleanup_peer_pending_opens(&id);
|
||||||
|
|
||||||
|
return notification_handled(cmd);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void json_channel_open_failed(struct command *cmd,
|
static struct command_result *json_channel_open_failed(struct command *cmd,
|
||||||
const char *buf,
|
const char *buf,
|
||||||
const jsmntok_t *params)
|
const jsmntok_t *params)
|
||||||
{
|
{
|
||||||
@@ -758,6 +760,8 @@ static void json_channel_open_failed(struct command *cmd,
|
|||||||
open = cleanup_channel_pending_open(&cid);
|
open = cleanup_channel_pending_open(&cid);
|
||||||
if (open)
|
if (open)
|
||||||
unreserve_psbt(open);
|
unreserve_psbt(open);
|
||||||
|
|
||||||
|
return notification_handled(cmd);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void json_add_policy(struct json_stream *stream,
|
static void json_add_policy(struct json_stream *stream,
|
||||||
|
|||||||
@@ -1759,3 +1759,10 @@ command_hook_success(struct command *cmd)
|
|||||||
json_add_string(response, "result", "continue");
|
json_add_string(response, "result", "continue");
|
||||||
return command_finished(cmd, response);
|
return command_finished(cmd, response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct command_result *WARN_UNUSED_RESULT
|
||||||
|
notification_handled(struct command *cmd)
|
||||||
|
{
|
||||||
|
tal_free(cmd);
|
||||||
|
return &complete;
|
||||||
|
}
|
||||||
|
|||||||
@@ -84,7 +84,9 @@ struct plugin_option {
|
|||||||
/* Create an array of these, one for each notification you subscribe to. */
|
/* Create an array of these, one for each notification you subscribe to. */
|
||||||
struct plugin_notification {
|
struct plugin_notification {
|
||||||
const char *name;
|
const char *name;
|
||||||
void (*handle)(struct command *cmd,
|
/* The handler must eventually trigger a `notification_handled`
|
||||||
|
* call. */
|
||||||
|
struct command_result* (*handle)(struct command *cmd,
|
||||||
const char *buf,
|
const char *buf,
|
||||||
const jsmntok_t *params);
|
const jsmntok_t *params);
|
||||||
};
|
};
|
||||||
@@ -190,6 +192,13 @@ command_success(struct command *cmd, const struct json_out *result);
|
|||||||
struct command_result *WARN_UNUSED_RESULT
|
struct command_result *WARN_UNUSED_RESULT
|
||||||
command_hook_success(struct command *cmd);
|
command_hook_success(struct command *cmd);
|
||||||
|
|
||||||
|
/* End a notification handler. */
|
||||||
|
struct command_result *WARN_UNUSED_RESULT
|
||||||
|
notification_handled(struct command *cmd);
|
||||||
|
|
||||||
|
/* Helper for notification handler that will be finished in a callback. */
|
||||||
|
#define notification_handler_pending(cmd) command_still_pending(cmd)
|
||||||
|
|
||||||
/* Synchronous helper to send command and extract fields from
|
/* Synchronous helper to send command and extract fields from
|
||||||
* response; can only be used in init callback. */
|
* response; can only be used in init callback. */
|
||||||
void rpc_scan(struct plugin *plugin,
|
void rpc_scan(struct plugin *plugin,
|
||||||
|
|||||||
@@ -524,7 +524,7 @@ check_sigs_ready(struct multifundchannel_command *mfc)
|
|||||||
return command_still_pending(mfc->cmd);
|
return command_still_pending(mfc->cmd);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void json_peer_sigs(struct command *cmd,
|
static struct command_result *json_peer_sigs(struct command *cmd,
|
||||||
const char *buf,
|
const char *buf,
|
||||||
const jsmntok_t *params)
|
const jsmntok_t *params)
|
||||||
{
|
{
|
||||||
@@ -552,7 +552,7 @@ static void json_peer_sigs(struct command *cmd,
|
|||||||
"mfc ??: `openchannel_peer_sigs` no "
|
"mfc ??: `openchannel_peer_sigs` no "
|
||||||
"pending dest found for channel_id %s",
|
"pending dest found for channel_id %s",
|
||||||
type_to_string(tmpctx, struct channel_id, &cid));
|
type_to_string(tmpctx, struct channel_id, &cid));
|
||||||
return;
|
return notification_handled(cmd);
|
||||||
}
|
}
|
||||||
|
|
||||||
plugin_log(cmd->plugin, LOG_DBG,
|
plugin_log(cmd->plugin, LOG_DBG,
|
||||||
@@ -587,7 +587,12 @@ static void json_peer_sigs(struct command *cmd,
|
|||||||
dest->state = MULTIFUNDCHANNEL_SIGNED;
|
dest->state = MULTIFUNDCHANNEL_SIGNED;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Possibly free up the struct command for the mfc that
|
||||||
|
* we found. */
|
||||||
check_sigs_ready(dest->mfc);
|
check_sigs_ready(dest->mfc);
|
||||||
|
|
||||||
|
/* Free up the struct command for *this* call. */
|
||||||
|
return notification_handled(cmd);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ json_peer_connected(struct command *cmd,
|
|||||||
return command_finished(cmd, response);
|
return command_finished(cmd, response);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void json_connected(struct command *cmd,
|
static struct command_result *json_connected(struct command *cmd,
|
||||||
const char *buf,
|
const char *buf,
|
||||||
const jsmntok_t *params)
|
const jsmntok_t *params)
|
||||||
{
|
{
|
||||||
@@ -56,16 +56,17 @@ static void json_connected(struct command *cmd,
|
|||||||
assert(idtok);
|
assert(idtok);
|
||||||
plugin_log(cmd->plugin, LOG_INFORM, "%s connected",
|
plugin_log(cmd->plugin, LOG_INFORM, "%s connected",
|
||||||
json_strdup(tmpctx, buf, idtok));
|
json_strdup(tmpctx, buf, idtok));
|
||||||
|
return notification_handled(cmd);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void json_shutdown(struct command *cmd,
|
static struct command_result *json_shutdown(struct command *cmd,
|
||||||
const char *buf,
|
const char *buf,
|
||||||
const jsmntok_t *params)
|
const jsmntok_t *params)
|
||||||
{
|
{
|
||||||
plugin_log(cmd->plugin, LOG_DBG, "shutdown called");
|
plugin_log(cmd->plugin, LOG_DBG, "shutdown called");
|
||||||
|
|
||||||
if (dont_shutdown)
|
if (dont_shutdown)
|
||||||
return;
|
return notification_handled(cmd);
|
||||||
|
|
||||||
plugin_exit(cmd->plugin, 0);
|
plugin_exit(cmd->plugin, 0);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user