From d2cf2a3f51330d0accee39b8f4998679c660ced4 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 23 Mar 2022 09:31:36 +1030 Subject: [PATCH] gossipd: don't use connectd daemon_conn after it shuts down. Simply exit, like we do when master daemon_conn exits. ``` Valgrind error file: valgrind-errors.2211908 ==2211908== Invalid read of size 8 ==2211908== at 0x12AC13: daemon_conn_send (daemon_conn.c:137) ==2211908== by 0x113CD9: queue_peer_msg (gossipd.c:118) ==2211908== by 0x11B806: query_channel_range (queries.c:1169) ==2211908== by 0x1250DD: peer_gossip_probe_scids (seeker.c:706) ==2211908== by 0x1253B1: check_firstpeer (seeker.c:788) ==2211908== by 0x1256CA: seeker_check (seeker.c:884) ==2211908== by 0x1366AC: timer_expired (timeout.c:62) ==2211908== by 0x1163D1: main (gossipd.c:1146) ==2211908== Address 0x4cafdf0 is 48 bytes inside a block of size 88 free'd ==2211908== at 0x48460C4: free (vg_replace_malloc.c:872) ==2211908== by 0x1805EA: del_tree (tal.c:421) ==2211908== by 0x1808BE: tal_free (tal.c:486) ==2211908== by 0x12AB25: destroy_dc_from_conn (daemon_conn.c:112) ==2211908== by 0x17FFDF: notify (tal.c:237) ==2211908== by 0x180519: del_tree (tal.c:402) ==2211908== by 0x1808BE: tal_free (tal.c:486) ==2211908== by 0x16EE9A: io_close (io.c:450) ==2211908== by 0x16ECA9: do_plan (io.c:401) ==2211908== by 0x16ED16: io_ready (io.c:417) ==2211908== by 0x1710B2: io_loop (poll.c:453) ==2211908== by 0x1163C5: main (gossipd.c:1144) ==2211908== Block was alloc'd at ==2211908== at 0x484384F: malloc (vg_replace_malloc.c:381) ==2211908== by 0x180064: allocate (tal.c:250) ==2211908== by 0x180634: tal_alloc_ (tal.c:428) ==2211908== by 0x12AB65: daemon_conn_new_ (daemon_conn.c:122) ==2211908== by 0x1155F4: gossip_init (gossipd.c:763) ==2211908== by 0x116014: recv_req (gossipd.c:999) ==2211908== by 0x12A828: handle_read (daemon_conn.c:31) ==2211908== by 0x16E09F: next_plan (io.c:59) ==2211908== by 0x16ECD4: do_plan (io.c:407) ==2211908== by 0x16ED16: io_ready (io.c:417) ==2211908== by 0x1710B2: io_loop (poll.c:453) ==2211908== by 0x1163C5: main (gossipd.c:1144) ==2211908== ``` --- gossipd/gossipd.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/gossipd/gossipd.c b/gossipd/gossipd.c index 674f646cb..45cbe4918 100644 --- a/gossipd/gossipd.c +++ b/gossipd/gossipd.c @@ -707,6 +707,15 @@ struct peer *random_peer(struct daemon *daemon, return best; } +/* This is called when lightningd or connectd closes its connection to + * us. We simply exit. */ +static void master_or_connectd_gone(struct daemon_conn *dc UNUSED) +{ + daemon_shutdown(); + /* Can't tell master, it's gone. */ + exit(2); +} + /*~ Parse init message from lightningd: starts the daemon properly. */ static void gossip_init(struct daemon *daemon, const u8 *msg) { @@ -763,6 +772,7 @@ static void gossip_init(struct daemon *daemon, const u8 *msg) daemon->connectd = daemon_conn_new(daemon, CONNECTD_FD, connectd_req, maybe_send_query_responses, daemon); + tal_add_destructor(daemon->connectd, master_or_connectd_gone); /* OK, we are ready. */ daemon_conn_send(daemon->master, @@ -1090,15 +1100,6 @@ done: return daemon_conn_read_next(conn, daemon->master); } -/* This is called when lightningd closes its connection to us. We simply - * exit. */ -static void master_gone(struct daemon_conn *master UNUSED) -{ - daemon_shutdown(); - /* Can't tell master, it's gone. */ - exit(2); -} - int main(int argc, char *argv[]) { setup_locale(); @@ -1131,7 +1132,7 @@ int main(int argc, char *argv[]) /* Our daemons always use STDIN for commands from lightningd. */ daemon->master = daemon_conn_new(daemon, STDIN_FILENO, recv_req, NULL, daemon); - tal_add_destructor(daemon->master, master_gone); + tal_add_destructor(daemon->master, master_or_connectd_gone); status_setup_async(daemon->master);