diff --git a/lightningd/pay.c b/lightningd/pay.c index 0f845d6a7..3c4039794 100644 --- a/lightningd/pay.c +++ b/lightningd/pay.c @@ -333,6 +333,11 @@ static void json_sendpay(struct command *cmd, /* Wait until we get response. */ tal_add_destructor2(cmd, remove_cmd_from_pc, pc); + /* They're both children of ld, but on shutdown make sure we + * destroy the command before the pc, otherwise the + * remove_cmd_from_pc destructor causes a use-after-free */ + tal_steal(pc, cmd); + failcode = send_htlc_out(peer, amount, first_hop_data.outgoing_cltv, &rhash, onion, NULL, pc, &pc->out); if (failcode) {