Skip to content

Commit 43e5ef3

Browse files
rustyrussellniftynei
authored andcommitted
libplugin: don't call callbacks if cmd completed before response.
This can particularly happen with commando: ``` commando: FATAL SIGNAL 11 (version 06b36d3) 0x55609e953d51 send_backtrace common/daemon.c:33 0x55609e953dfb crashdump common/daemon.c:46 0x7f665e3b908f ??? /build/glibc-SzIz7B/glibc-2.31/signal/../sysdeps/unix/sysv/linux/x86_64/sigaction.c:0 0x55609e9387a3 send_more_cmd plugins/commando.c:632 0x55609e93b270 handle_rpc_reply plugins/libplugin.c:669 0x55609e93bd50 rpc_read_response_one plugins/libplugin.c:842 0x55609e93be86 rpc_conn_read_response plugins/libplugin.c:862 0x55609e9f4f68 next_plan ccan/ccan/io/io.c:59 0x55609e9f5b70 do_plan ccan/ccan/io/io.c:407 0x55609e9f5bb2 io_ready ccan/ccan/io/io.c:417 0x55609e9f7ea5 io_loop ccan/ccan/io/poll.c:453 0x55609e93eb20 plugin_main plugins/libplugin.c:1676 0x55609e9397ab main plugins/commando.c:922 0x7f665e39a082 __libc_start_main ../csu/libc-start.c:308 0x55609e93677d ??? ???:0 0xffffffffffffffff ??? ???:0 ``` Reported-by: @adi2011 Signed-off-by: Rusty Russell <[email protected]>
1 parent b3fde87 commit 43e5ef3

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

plugins/libplugin.c

+24
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,22 @@ static void ld_rpc_send(struct plugin *plugin, struct json_stream *stream)
135135
io_wake(plugin->io_rpc_conn);
136136
}
137137

138+
139+
/* When cmd for request is gone, we use this as noop callback */
140+
static struct command_result *ignore_cb(struct command *command,
141+
const char *buf,
142+
const jsmntok_t *result,
143+
void *arg)
144+
{
145+
return command_done();
146+
}
147+
148+
static void disable_request_cb(struct command *cmd, struct out_req *out)
149+
{
150+
out->errcb = NULL;
151+
out->cb = ignore_cb;
152+
}
153+
138154
/* FIXME: Move lightningd/jsonrpc to common/ ? */
139155

140156
struct out_req *
@@ -160,6 +176,10 @@ jsonrpc_request_start_(struct plugin *plugin, struct command *cmd,
160176
out->arg = arg;
161177
uintmap_add(&plugin->out_reqs, out->id, out);
162178

179+
/* If command goes away, don't call callbacks! */
180+
if (out->cmd)
181+
tal_add_destructor2(out->cmd, disable_request_cb, out);
182+
163183
out->js = new_json_stream(NULL, cmd, NULL);
164184
json_object_start(out->js, NULL);
165185
json_add_string(out->js, "jsonrpc", "2.0");
@@ -646,6 +666,10 @@ static void handle_rpc_reply(struct plugin *plugin, const jsmntok_t *toks)
646666
json_tok_full_len(toks),
647667
json_tok_full(plugin->rpc_buffer, toks), id);
648668

669+
/* Remove destructor if one existed */
670+
if (out->cmd)
671+
tal_del_destructor2(out->cmd, disable_request_cb, out);
672+
649673
/* We want to free this if callback doesn't. */
650674
tal_steal(tmpctx, out);
651675
uintmap_del(&plugin->out_reqs, out->id);

0 commit comments

Comments
 (0)