From 035194059353790d456a7f62833733372d20124a Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Fri, 29 Oct 2021 18:58:29 +0200 Subject: [PATCH] peer: Wrap the HTLC out destructor in a DB transaction Changelog-Fixed: peer: Fixed a crash when a connection is lost outside of a DB transaction --- lightningd/peer_htlcs.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lightningd/peer_htlcs.c b/lightningd/peer_htlcs.c index 9e9456595..2820cd28a 100644 --- a/lightningd/peer_htlcs.c +++ b/lightningd/peer_htlcs.c @@ -519,6 +519,12 @@ fail: */ static void destroy_hout_subd_died(struct htlc_out *hout) { + struct db *db = hout->key.channel->peer->ld->wallet->db; + /* Under some circumstances we may need to start a DB + * transaction and commit it here again. This is the case when + * we're getting called from the destructor chain. */ + bool have_tx = + db_in_transaction(db); log_debug(hout->key.channel->log, "Failing HTLC %"PRIu64" due to peer death", hout->key.id); @@ -531,8 +537,14 @@ static void destroy_hout_subd_died(struct htlc_out *hout) assert(hout->hstate == SENT_ADD_HTLC); hout->hstate = RCVD_REMOVE_HTLC; + if (!have_tx) + db_begin_transaction(db); + fail_out_htlc(hout, "Outgoing subdaemon died", take(towire_temporary_channel_failure(NULL, NULL))); + + if (!have_tx) + db_commit_transaction(db); } /* This is where channeld gives us the HTLC id, and also reports if it