Browse Source

connectd: properly cleanup 'competing' outgoing connections

Coauthored-By: Rusty Russell @rustyrussell
ppa-prep
niftynei 4 years ago
committed by neil saitug
parent
commit
abad494fcf
  1. 29
      connectd/connectd.c

29
connectd/connectd.c

@ -173,6 +173,8 @@ struct connecting {
struct daemon *daemon; struct daemon *daemon;
struct io_conn *conn;
/* The ID of the peer (not necessarily unique, in transit!) */ /* The ID of the peer (not necessarily unique, in transit!) */
struct node_id id; struct node_id id;
@ -276,16 +278,26 @@ static void connected_to_peer(struct daemon *daemon,
struct io_conn *conn, struct io_conn *conn,
const struct node_id *id) const struct node_id *id)
{ {
/* Don't call destroy_io_conn */ struct connecting *outgoing;
io_set_finish(conn, NULL, NULL);
/* We allocate 'conn' as a child of 'connect': we don't want to free /* We allocate 'conn' as a child of 'connect': we don't want to free
* it just yet though. tal_steal() it onto the permanent 'daemon' * it just yet though. tal_steal() it onto the permanent 'daemon'
* struct. */ * struct. */
tal_steal(daemon, conn); tal_steal(daemon, conn);
/* Now free the 'connecting' struct. */ /* This is either us (if conn is an outgoing connection), or
tal_free(find_connecting(daemon, id)); * NULL or a separate attempt (if we're an incoming): in
* that case, kill the outgoing in favor of our successful
* incoming connection. */
outgoing = find_connecting(daemon, id);
if (outgoing) {
/* Don't call destroy_io_conn, since we're done. */
if (outgoing->conn)
io_set_finish(outgoing->conn, NULL, NULL);
/* Now free the 'connecting' struct. */
tal_free(outgoing);
}
} }
/*~ Every per-peer daemon needs a connection to the gossip daemon; this allows /*~ 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)
"%s. ", error); "%s. ", error);
} }
/*~ This is the destructor for the (unsuccessful) connection. We accumulate /*~ This is the destructor for the (unsuccessful) outgoing connection. We accumulate
* the errors which occurred, so we can report to lightningd properly in case * the errors which occurred, so we can report to lightningd properly in case
* they all fail, and try the next address. * they all fail, and try the next address.
* *
@ -782,6 +794,9 @@ static void try_connect_one_addr(struct connecting *connect)
bool use_proxy = connect->daemon->use_proxy_always; bool use_proxy = connect->daemon->use_proxy_always;
const struct wireaddr_internal *addr = &connect->addrs[connect->addrnum]; const struct wireaddr_internal *addr = &connect->addrs[connect->addrnum];
/* In case we fail without a connection, make destroy_io_conn happy */
connect->conn = NULL;
/* Out of addresses? */ /* Out of addresses? */
if (connect->addrnum == tal_count(connect->addrs)) { if (connect->addrnum == tal_count(connect->addrs)) {
connect_failed(connect->daemon, &connect->id, connect_failed(connect->daemon, &connect->id,
@ -860,9 +875,9 @@ static void try_connect_one_addr(struct connecting *connect)
/* This creates the new connection using our fd, with the initialization /* This creates the new connection using our fd, with the initialization
* function one of the above. */ * function one of the above. */
if (use_proxy) if (use_proxy)
notleak(io_new_conn(connect, fd, conn_proxy_init, connect)); connect->conn = io_new_conn(connect, fd, conn_proxy_init, connect);
else else
notleak(io_new_conn(connect, fd, conn_init, connect)); connect->conn = io_new_conn(connect, fd, conn_init, connect);
} }
/*~ connectd is responsible for incoming connections, but it's the process of /*~ connectd is responsible for incoming connections, but it's the process of

Loading…
Cancel
Save