Skip to content

Commit fa513c9

Browse files
committed
lightningd: clean up subds before freeing HTLCs.
Otherwise we get weird effects, as htlcs are being freed: ``` 2022-01-26T05:07:37.8774610Z lightningd-1: 2022-01-26T04:47:48.770Z DEBUG 030eeb52087b9dbb27b7aec79ca5249369f6ce7b20a5684ce38d9f4595a21c2fda-chan#8: Failing HTLC 18446744073709551615 due to peer death 2022-01-26T05:07:37.8775287Z lightningd-1: 2022-01-26T04:47:48.770Z **BROKEN** 030eeb52087b9dbb27b7aec79ca5249369f6ce7b20a5684ce38d9f4595a21c2fda-chan#8: Neither origin nor in? ``` Signed-off-by: Rusty Russell <[email protected]>
1 parent f9b6fd6 commit fa513c9

File tree

5 files changed

+42
-5
lines changed

5 files changed

+42
-5
lines changed

lightningd/lightningd.c

+5
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,11 @@ static void shutdown_subdaemons(struct lightningd *ld)
529529
ld->gossip = subd_shutdown(ld->gossip, 10);
530530
ld->hsm = subd_shutdown(ld->hsm, 10);
531531

532+
/*~ Closing the hsmd means all other subdaemons should be exiting;
533+
* deal with that cleanly before we start freeing internal
534+
* structures. */
535+
subd_shutdown_remaining(ld);
536+
532537
/* Now we free all the HTLCs */
533538
free_htlcs(ld, NULL);
534539

lightningd/onchain_control.c

+6-1
Original file line numberDiff line numberDiff line change
@@ -567,10 +567,15 @@ static void onchain_error(struct channel *channel,
567567
bool warning UNUSED,
568568
const u8 *err_for_them UNUSED)
569569
{
570+
channel_set_owner(channel, NULL);
571+
572+
/* This happens on shutdown: fine */
573+
if (channel->peer->ld->state == LD_STATE_SHUTDOWN)
574+
return;
575+
570576
/* FIXME: re-launch? */
571577
log_broken(channel->log, "%s", desc);
572578
channel_set_billboard(channel, true, desc);
573-
channel_set_owner(channel, NULL);
574579
}
575580

576581
/* With a reorg, this can get called multiple times; each time we'll kill

lightningd/opening_common.c

+8-4
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ void uncommitted_channel_disconnect(struct uncommitted_channel *uc,
105105
{
106106
u8 *msg = towire_connectd_peer_disconnected(tmpctx, &uc->peer->id);
107107
log_(uc->log, level, NULL, false, "%s", desc);
108-
subd_send_msg(uc->peer->ld->connectd, msg);
108+
/* NULL when we're shutting down */
109+
if (uc->peer->ld->connectd)
110+
subd_send_msg(uc->peer->ld->connectd, msg);
109111
if (uc->fc && uc->fc->cmd)
110112
was_pending(command_fail(uc->fc->cmd, LIGHTNINGD, "%s", desc));
111113
notify_disconnect(uc->peer->ld, &uc->peer->id);
@@ -191,9 +193,11 @@ void handle_reestablish(struct lightningd *ld,
191193
"Unknown channel for reestablish");
192194
log_debug(ld->log, "Reestablish on UNKNOWN channel %s",
193195
type_to_string(tmpctx, struct channel_id, channel_id));
194-
subd_send_msg(ld->connectd,
195-
take(towire_connectd_peer_final_msg(NULL, peer_id,
196-
err)));
196+
/* Unless we're shutting down */
197+
if (ld->connectd)
198+
subd_send_msg(ld->connectd,
199+
take(towire_connectd_peer_final_msg(NULL, peer_id,
200+
err)));
197201
tal_free(peer_fd);
198202
}
199203
}

lightningd/subd.c

+14
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,20 @@ struct subd *subd_shutdown(struct subd *sd, unsigned int seconds)
895895
return tal_free(sd);
896896
}
897897

898+
void subd_shutdown_remaining(struct lightningd *ld)
899+
{
900+
struct subd *subd;
901+
902+
/* We give them a second to finish exiting, before we kill
903+
* them in destroy_subd() */
904+
sleep(1);
905+
906+
while ((subd = list_top(&ld->subds, struct subd, list)) != NULL) {
907+
/* Destructor removes from list */
908+
io_close(subd->conn);
909+
}
910+
}
911+
898912
void subd_release_channel(struct subd *owner, const void *channel)
899913
{
900914
/* If owner is a per-peer-daemon, and not already freeing itself... */

lightningd/subd.h

+9
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,15 @@ void subd_release_channel(struct subd *owner, const void *channel);
222222
*/
223223
struct subd *subd_shutdown(struct subd *subd, unsigned int seconds);
224224

225+
/**
226+
* subd_shutdown_remaining - kill all remaining (per-peer) subds
227+
* @ld: lightningd
228+
*
229+
* They should already be exiting (since we shutdown hsmd), but
230+
* make sure they have.
231+
*/
232+
void subd_shutdown_remaining(struct lightningd *ld);
233+
225234
/* Ugly helper to get full pathname of the current binary. */
226235
const char *find_my_abspath(const tal_t *ctx, const char *argv0);
227236

0 commit comments

Comments
 (0)