From 1acc1aa27ef5d8dfd5742ab89b78e8403d1b87ba Mon Sep 17 00:00:00 2001 From: niftynei Date: Tue, 9 Feb 2021 15:24:23 -0600 Subject: [PATCH] rbf: clean up channel on error Make sure we clean up unsaved channels appropriately on failure. We forget the peer/channel if it's unsaved! --- lightningd/peer_control.c | 37 +++++++++++++++------ lightningd/test/run-invoice-select-inchan.c | 3 ++ 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 5a1943b26..4b6fed0e0 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -380,8 +380,35 @@ void channel_errmsg(struct channel *channel, { notify_disconnect(channel->peer->ld, &channel->peer->id); + /* Clean up any in-progress open attempts */ + if (channel->open_attempt) { + struct open_attempt *oa = channel->open_attempt; + if (oa->cmd) { + was_pending(command_fail(oa->cmd, LIGHTNINGD, + "%s", desc)); + oa->cmd = NULL; + } + notify_channel_open_failed(channel->peer->ld, &channel->cid); + channel->open_attempt = tal_free(channel->open_attempt); + } + + if (channel->openchannel_signed_cmd) { + was_pending(command_fail(channel->openchannel_signed_cmd, + LIGHTNINGD, "%s", desc)); + channel->openchannel_signed_cmd = NULL; + } + /* No per_peer_state means a subd crash or disconnection. */ if (!pps) { + /* If the channel is unsaved, we forget it */ + if (channel_unsaved(channel)) { + log_unusual(channel->log, "%s", + "Unsaved peer failed." + " Disconnecting and deleting channel."); + delete_channel(channel); + return; + } + channel_fail_reconnect(channel, "%s: %s", channel->owner->name, desc); return; @@ -391,16 +418,6 @@ void channel_errmsg(struct channel *channel, if (err_for_them && !channel->error && !warning) channel->error = tal_dup_talarr(channel, u8, err_for_them); - /* Clean up any in-progress open attempts */ - if (channel->open_attempt) { - struct open_attempt *oa = channel->open_attempt; - if (oa->cmd) - was_pending(command_fail(oa->cmd, LIGHTNINGD, - "%s", desc)); - notify_channel_open_failed(channel->peer->ld, &channel->cid); - channel->open_attempt = tal_free(channel->open_attempt); - } - /* Other implementations chose to ignore errors early on. Not * surprisingly, they now spew out spurious errors frequently, * and we would close the channel on them. We now support warnings diff --git a/lightningd/test/run-invoice-select-inchan.c b/lightningd/test/run-invoice-select-inchan.c index 41438d089..115d8d212 100644 --- a/lightningd/test/run-invoice-select-inchan.c +++ b/lightningd/test/run-invoice-select-inchan.c @@ -606,6 +606,9 @@ u8 *towire_channeld_specific_feerates(const tal_t *ctx UNNEEDED, u32 feerate_bas /* Generated stub for towire_connectd_connect_to_peer */ u8 *towire_connectd_connect_to_peer(const tal_t *ctx UNNEEDED, const struct node_id *id UNNEEDED, u32 seconds_waited UNNEEDED, const struct wireaddr_internal *addrhint UNNEEDED) { fprintf(stderr, "towire_connectd_connect_to_peer called!\n"); abort(); } +/* Generated stub for towire_connectd_peer_disconnected */ +u8 *towire_connectd_peer_disconnected(const tal_t *ctx UNNEEDED, const struct node_id *id UNNEEDED) +{ fprintf(stderr, "towire_connectd_peer_disconnected called!\n"); abort(); } /* Generated stub for towire_dualopend_send_shutdown */ u8 *towire_dualopend_send_shutdown(const tal_t *ctx UNNEEDED, const u8 *shutdown_scriptpubkey UNNEEDED) { fprintf(stderr, "towire_dualopend_send_shutdown called!\n"); abort(); }