lightningd: don't tell connectd to discard peer unless no subds left.

Otherwise it waits for subds to exit, but they don't.  Plus, the others
may still be talking!

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2022-03-23 09:31:33 +10:30
parent 75596b3e0f
commit 4e8239fcfe
7 changed files with 42 additions and 34 deletions

View file

@ -20,11 +20,6 @@
#include <wallet/txfilter.h>
#include <wire/wire_sync.h>
static bool connects_to_peer(struct subd *owner)
{
return owner && owner->talks_to_peer;
}
void channel_set_owner(struct channel *channel, struct subd *owner)
{
struct subd *old_owner = channel->owner;
@ -32,20 +27,10 @@ void channel_set_owner(struct channel *channel, struct subd *owner)
if (old_owner) {
subd_release_channel(old_owner, channel);
if (channel->connected && !connects_to_peer(owner)) {
/* If shutting down, connectd no longer exists */
if (channel->peer->ld->connectd) {
u8 *msg;
msg = towire_connectd_discard_peer(
NULL,
&channel->peer->id);
subd_send_msg(channel->peer->ld->connectd,
take(msg));
} else
channel->peer->is_connected = false;
}
if (channel->connected)
maybe_disconnect_peer(channel->peer->ld, channel->peer);
}
channel->connected = connects_to_peer(owner);
channel->connected = (owner && owner->talks_to_peer);
}
struct htlc_out *channel_has_htlc_out(struct channel *channel)

View file

@ -601,6 +601,31 @@ void connectd_activate(struct lightningd *ld)
io_loop(NULL, NULL);
}
void maybe_disconnect_peer(struct lightningd *ld, struct peer *peer)
{
struct channel *channel;
/* Any channels left which want to talk? */
if (peer->uncommitted_channel)
return;
list_for_each(&peer->channels, channel, list) {
if (!channel->owner)
continue;
if (channel->owner->talks_to_peer)
return;
}
/* If shutting down, connectd no longer exists */
if (!ld->connectd) {
peer->is_connected = false;
return;
}
subd_send_msg(ld->connectd,
take(towire_connectd_discard_peer(NULL, &peer->id)));
}
static struct command_result *json_sendcustommsg(struct command *cmd,
const char *buffer,
const jsmntok_t *obj UNNEEDED,

View file

@ -21,4 +21,7 @@ void connect_succeeded(struct lightningd *ld, const struct peer *peer,
bool incoming,
const struct wireaddr_internal *addr);
/* Disconnect a peer (if no subds want to talk any more) */
void maybe_disconnect_peer(struct lightningd *ld, struct peer *peer);
#endif /* LIGHTNING_LIGHTNINGD_CONNECT_CONTROL_H */

View file

@ -3,11 +3,11 @@
#include <common/json_command.h>
#include <common/type_to_string.h>
#include <common/wire_error.h>
#include <connectd/connectd_wiregen.h>
#include <errno.h>
#include <hsmd/hsmd_wiregen.h>
#include <lightningd/channel.h>
#include <lightningd/channel_control.h>
#include <lightningd/connect_control.h>
#include <lightningd/notification.h>
#include <lightningd/opening_common.h>
#include <lightningd/peer_control.h>
@ -31,6 +31,7 @@ static void destroy_uncommitted_channel(struct uncommitted_channel *uc)
uc->peer->uncommitted_channel = NULL;
maybe_disconnect_peer(uc->peer->ld, uc->peer);
maybe_delete_peer(uc->peer);
}
@ -104,11 +105,7 @@ void uncommitted_channel_disconnect(struct uncommitted_channel *uc,
enum log_level level,
const char *desc)
{
u8 *msg = towire_connectd_discard_peer(tmpctx, &uc->peer->id);
log_(uc->log, level, NULL, false, "%s", desc);
/* NULL when we're shutting down */
if (uc->peer->ld->connectd)
subd_send_msg(uc->peer->ld->connectd, msg);
if (uc->fc && uc->fc->cmd)
was_pending(command_fail(uc->fc->cmd, LIGHTNINGD, "%s", desc));
}

View file

@ -1962,11 +1962,9 @@ static struct command_result *json_disconnect(struct command *cmd,
disconnected = true;
}
if (!disconnected) {
/* It's just sitting in connectd. */
subd_send_msg(cmd->ld->connectd,
take(towire_connectd_discard_peer(NULL, id)));
}
/* It's just sitting in connectd? */
if (!disconnected)
maybe_disconnect_peer(cmd->ld, peer);
/* Connectd tells us when it's finally disconnected */
dc = tal(cmd, struct disconnect_command);

View file

@ -450,6 +450,9 @@ void log_(struct log *log UNNEEDED, enum log_level level UNNEEDED,
const char *fmt UNNEEDED, ...)
{ fprintf(stderr, "log_ called!\n"); abort(); }
/* Generated stub for maybe_disconnect_peer */
void maybe_disconnect_peer(struct lightningd *ld UNNEEDED, struct peer *peer UNNEEDED)
{ fprintf(stderr, "maybe_disconnect_peer called!\n"); abort(); }
/* Generated stub for merkle_tlv */
void merkle_tlv(const struct tlv_field *fields UNNEEDED, struct sha256 *merkle UNNEEDED)
{ fprintf(stderr, "merkle_tlv called!\n"); abort(); }
@ -654,9 +657,6 @@ u8 *towire_channeld_dev_memleak(const tal_t *ctx UNNEEDED)
/* Generated stub for towire_channeld_dev_reenable_commit */
u8 *towire_channeld_dev_reenable_commit(const tal_t *ctx UNNEEDED)
{ fprintf(stderr, "towire_channeld_dev_reenable_commit called!\n"); abort(); }
/* Generated stub for towire_connectd_discard_peer */
u8 *towire_connectd_discard_peer(const tal_t *ctx UNNEEDED, const struct node_id *id UNNEEDED)
{ fprintf(stderr, "towire_connectd_discard_peer called!\n"); abort(); }
/* Generated stub for towire_connectd_peer_final_msg */
u8 *towire_connectd_peer_final_msg(const tal_t *ctx UNNEEDED, const struct node_id *id UNNEEDED, const u8 *msg UNNEEDED)
{ fprintf(stderr, "towire_connectd_peer_final_msg called!\n"); abort(); }

View file

@ -448,6 +448,9 @@ bool json_tok_streq(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
void kill_uncommitted_channel(struct uncommitted_channel *uc UNNEEDED,
const char *why UNNEEDED)
{ fprintf(stderr, "kill_uncommitted_channel called!\n"); abort(); }
/* Generated stub for maybe_disconnect_peer */
void maybe_disconnect_peer(struct lightningd *ld UNNEEDED, struct peer *peer UNNEEDED)
{ fprintf(stderr, "maybe_disconnect_peer called!\n"); abort(); }
/* Generated stub for new_channel_mvt_invoice_hin */
struct channel_coin_mvt *new_channel_mvt_invoice_hin(const tal_t *ctx UNNEEDED,
struct htlc_in *hin UNNEEDED,
@ -733,9 +736,6 @@ u8 *towire_channeld_offer_htlc(const tal_t *ctx UNNEEDED, struct amount_msat amo
/* Generated stub for towire_channeld_sending_commitsig_reply */
u8 *towire_channeld_sending_commitsig_reply(const tal_t *ctx UNNEEDED)
{ fprintf(stderr, "towire_channeld_sending_commitsig_reply called!\n"); abort(); }
/* Generated stub for towire_connectd_discard_peer */
u8 *towire_connectd_discard_peer(const tal_t *ctx UNNEEDED, const struct node_id *id UNNEEDED)
{ fprintf(stderr, "towire_connectd_discard_peer called!\n"); abort(); }
/* Generated stub for towire_connectd_peer_final_msg */
u8 *towire_connectd_peer_final_msg(const tal_t *ctx UNNEEDED, const struct node_id *id UNNEEDED, const u8 *msg UNNEEDED)
{ fprintf(stderr, "towire_connectd_peer_final_msg called!\n"); abort(); }