Skip to content

Commit fc6bee8

Browse files
committed
connectd: close connection properly after dev-disconnect +.
We need to drain subds too, otherwise if timing is correct we never close. Signed-off-by: Rusty Russell <[email protected]>
1 parent af340a6 commit fc6bee8

File tree

1 file changed

+24
-27
lines changed

1 file changed

+24
-27
lines changed

connectd/multiplex.c

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ struct subd {
6767

6868
/* FIXME: reorder! */
6969
static void destroy_connected_subd(struct subd *subd);
70+
static struct io_plan *write_to_peer(struct io_conn *peer_conn,
71+
struct peer *peer);
7072

7173
static struct subd *find_subd(struct peer *peer,
7274
const struct channel_id *channel_id)
@@ -160,6 +162,21 @@ void disconnect_peer(struct peer *peer)
160162
}
161163
}
162164

165+
static void free_all_subds(struct peer *peer)
166+
{
167+
for (size_t i = 0; i < tal_count(peer->subds); i++) {
168+
/* Once conn exists, subd is a child of the conn. Free conn, free subd. */
169+
if (peer->subds[i]->conn) {
170+
tal_del_destructor(peer->subds[i], destroy_connected_subd);
171+
tal_free(peer->subds[i]->conn);
172+
} else {
173+
/* We told lightningd that peer spoke, but it hasn't returned yet. */
174+
tal_free(peer->subds[i]);
175+
}
176+
}
177+
tal_resize(&peer->subds, 0);
178+
}
179+
163180
/* Send warning, close connection to peer */
164181
static void send_warning(struct peer *peer, const char *fmt, ...)
165182
{
@@ -177,17 +194,7 @@ static void send_warning(struct peer *peer, const char *fmt, ...)
177194
inject_peer_msg(peer, take(msg));
178195

179196
/* Free all the subds immediately */
180-
for (size_t i = 0; i < tal_count(peer->subds); i++) {
181-
/* Once conn exists, subd is a child of the conn. Free conn, free subd. */
182-
if (peer->subds[i]->conn) {
183-
tal_del_destructor(peer->subds[i], destroy_connected_subd);
184-
tal_free(peer->subds[i]->conn);
185-
} else {
186-
/* We told lightningd that peer spoke, but it hasn't returned yet. */
187-
tal_free(peer->subds[i]);
188-
}
189-
}
190-
tal_resize(&peer->subds, 0);
197+
free_all_subds(peer);
191198
disconnect_peer(peer);
192199
}
193200

@@ -397,12 +404,6 @@ static bool is_urgent(enum peer_wire type)
397404
return false;
398405
}
399406

400-
/* io_sock_shutdown, but in format suitable for an io_plan callback */
401-
static struct io_plan *io_sock_shutdown_cb(struct io_conn *conn, struct peer *unused)
402-
{
403-
return io_sock_shutdown(conn);
404-
}
405-
406407
/* Process and eat protocol_batch_element messages, encrypt each element message
407408
* and return the encrypted messages as one long byte array. */
408409
static u8 *process_batch_elements(struct peer *peer, const u8 *msg TAKES)
@@ -466,11 +467,7 @@ static u8 *process_batch_elements(struct peer *peer, const u8 *msg TAKES)
466467
return ret;
467468
}
468469

469-
static struct io_plan *encrypt_and_send(struct peer *peer,
470-
const u8 *msg TAKES,
471-
struct io_plan *(*next)
472-
(struct io_conn *peer_conn,
473-
struct peer *peer))
470+
static struct io_plan *encrypt_and_send(struct peer *peer, const u8 *msg TAKES)
474471
{
475472
int type = fromwire_peektype(msg);
476473

@@ -482,8 +479,8 @@ static struct io_plan *encrypt_and_send(struct peer *peer,
482479
case DEV_DISCONNECT_OUT_AFTER:
483480
/* Disallow reads from now on */
484481
peer->dev_read_enabled = false;
485-
/* Using io_close here can lose the data we're about to send! */
486-
next = io_sock_shutdown_cb;
482+
free_all_subds(peer);
483+
drain_peer(peer);
487484
break;
488485
case DEV_DISCONNECT_OUT_BLACKHOLE:
489486
/* Disable both reads and writes from now on */
@@ -499,7 +496,7 @@ static struct io_plan *encrypt_and_send(struct peer *peer,
499496
/* Tell them to read again, */
500497
io_wake(&peer->subds);
501498
return msg_queue_wait(peer->to_peer, peer->peer_outq,
502-
next, peer);
499+
write_to_peer, peer);
503500
case DEV_DISCONNECT_OUT_DISABLE_AFTER:
504501
peer->dev_read_enabled = false;
505502
peer->dev_writes_enabled = tal(peer, u32);
@@ -523,7 +520,7 @@ static struct io_plan *encrypt_and_send(struct peer *peer,
523520
return io_write(peer->to_peer,
524521
peer->sent_to_peer,
525522
tal_bytelen(peer->sent_to_peer),
526-
next, peer);
523+
write_to_peer, peer);
527524
}
528525

529526
/* Kicks off write_to_peer() to look for more gossip to send from store */
@@ -1120,7 +1117,7 @@ static struct io_plan *write_to_peer(struct io_conn *peer_conn,
11201117
(*peer->dev_writes_enabled)--;
11211118
}
11221119

1123-
return encrypt_and_send(peer, take(msg), write_to_peer);
1120+
return encrypt_and_send(peer, take(msg));
11241121
}
11251122

11261123
static struct io_plan *read_from_subd(struct io_conn *subd_conn,

0 commit comments

Comments
 (0)