@@ -173,6 +173,8 @@ struct connecting {
173173
174174 struct daemon * daemon ;
175175
176+ struct io_conn * conn ;
177+
176178 /* The ID of the peer (not necessarily unique, in transit!) */
177179 struct node_id id ;
178180
@@ -276,16 +278,26 @@ static void connected_to_peer(struct daemon *daemon,
276278 struct io_conn * conn ,
277279 const struct node_id * id )
278280{
279- /* Don't call destroy_io_conn */
280- io_set_finish (conn , NULL , NULL );
281+ struct connecting * outgoing ;
281282
282283 /* We allocate 'conn' as a child of 'connect': we don't want to free
283284 * it just yet though. tal_steal() it onto the permanent 'daemon'
284285 * struct. */
285286 tal_steal (daemon , conn );
286287
287- /* Now free the 'connecting' struct. */
288- tal_free (find_connecting (daemon , id ));
288+ /* This is either us (if conn is an outgoing connection), or
289+ * NULL or a separate attempt (if we're an incoming): in
290+ * that case, kill the outgoing in favor of our successful
291+ * incoming connection. */
292+ outgoing = find_connecting (daemon , id );
293+ if (outgoing ) {
294+ /* Don't call destroy_io_conn, since we're done. */
295+ if (outgoing -> conn )
296+ io_set_finish (outgoing -> conn , NULL , NULL );
297+
298+ /* Now free the 'connecting' struct. */
299+ tal_free (outgoing );
300+ }
289301}
290302
291303/*~ Every per-peer daemon needs a connection to the gossip daemon; this allows
@@ -669,7 +681,7 @@ void add_errors_to_error_list(struct connecting *connect, const char *error)
669681 "%s. " , error );
670682}
671683
672- /*~ This is the destructor for the (unsuccessful) connection. We accumulate
684+ /*~ This is the destructor for the (unsuccessful) outgoing connection. We accumulate
673685 * the errors which occurred, so we can report to lightningd properly in case
674686 * they all fail, and try the next address.
675687 *
@@ -782,6 +794,9 @@ static void try_connect_one_addr(struct connecting *connect)
782794 bool use_proxy = connect -> daemon -> use_proxy_always ;
783795 const struct wireaddr_internal * addr = & connect -> addrs [connect -> addrnum ];
784796
797+ /* In case we fail without a connection, make destroy_io_conn happy */
798+ connect -> conn = NULL ;
799+
785800 /* Out of addresses? */
786801 if (connect -> addrnum == tal_count (connect -> addrs )) {
787802 connect_failed (connect -> daemon , & connect -> id ,
@@ -860,9 +875,9 @@ static void try_connect_one_addr(struct connecting *connect)
860875 /* This creates the new connection using our fd, with the initialization
861876 * function one of the above. */
862877 if (use_proxy )
863- notleak ( io_new_conn (connect , fd , conn_proxy_init , connect ) );
878+ connect -> conn = io_new_conn (connect , fd , conn_proxy_init , connect );
864879 else
865- notleak ( io_new_conn (connect , fd , conn_init , connect ) );
880+ connect -> conn = io_new_conn (connect , fd , conn_init , connect );
866881}
867882
868883/*~ connectd is responsible for incoming connections, but it's the process of
0 commit comments