From 899bf3fde9ccbe716a0fb280872152763f74ade6 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 6 Dec 2017 10:05:46 +1030 Subject: [PATCH] subd: add transaction to subd exit corner case. As demonstrated in the test at the end of this series, openingd dying spontaneously causes the conn to be freed which causes the subd to be destroyed, which fails the peer, which hits the db. Signed-off-by: Rusty Russell --- lightningd/subd.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lightningd/subd.c b/lightningd/subd.c index 95d670335..317c8eb69 100644 --- a/lightningd/subd.c +++ b/lightningd/subd.c @@ -548,10 +548,20 @@ static void destroy_subd(struct subd *sd) if (sd->peer) { /* Don't loop back when we fail it. */ struct peer *peer = sd->peer; + struct db *db = sd->ld->wallet->db; + bool outer_transaction; + sd->peer = NULL; + + /* We can be freed both inside msg handling, or spontaneously. */ + outer_transaction = db->in_transaction; + if (!outer_transaction) + db_begin_transaction(db); peer_fail_transient(peer, "Owning subdaemon %s died (%i)", sd->name, status); + if (!outer_transaction) + db_commit_transaction(db); } if (sd->must_not_exit) {