Skip to content

Commit 4249c61

Browse files
rustyrussellniftynei
authored andcommitted
libplugin: forget pending requests if associated command freed.
This is usually fine, but without this, commando (another branch!) has a race: 1. A command has multiple parts. 2. We start sending them out. 3. We get a response, which completes the cmd. 4. We go to send the next one out, but it's been freed. Signed-off-by: Rusty Russell <[email protected]>
1 parent 1781fad commit 4249c61

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

plugins/libplugin.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,11 @@ static void disable_request_cb(struct command *cmd, struct out_req *out)
153153
out->cmd = NULL;
154154
}
155155

156+
static void destroy_out_req(struct out_req *out_req, struct plugin *plugin)
157+
{
158+
uintmap_del(&plugin->out_reqs, out_req->id);
159+
}
160+
156161
/* FIXME: Move lightningd/jsonrpc to common/ ? */
157162

158163
struct out_req *
@@ -170,13 +175,14 @@ jsonrpc_request_start_(struct plugin *plugin, struct command *cmd,
170175
{
171176
struct out_req *out;
172177

173-
out = tal(plugin, struct out_req);
178+
out = tal(cmd, struct out_req);
174179
out->id = plugin->next_outreq_id++;
175180
out->cmd = cmd;
176181
out->cb = cb;
177182
out->errcb = errcb;
178183
out->arg = arg;
179184
uintmap_add(&plugin->out_reqs, out->id, out);
185+
tal_add_destructor2(out, destroy_out_req, plugin);
180186

181187
/* If command goes away, don't call callbacks! */
182188
if (out->cmd)
@@ -674,7 +680,6 @@ static void handle_rpc_reply(struct plugin *plugin, const jsmntok_t *toks)
674680

675681
/* We want to free this if callback doesn't. */
676682
tal_steal(tmpctx, out);
677-
uintmap_del(&plugin->out_reqs, out->id);
678683

679684
contenttok = json_get_member(plugin->rpc_buffer, toks, "error");
680685
if (contenttok) {

0 commit comments

Comments
 (0)