diff --git a/src/ssb.c b/src/ssb.c index 0ba05cb7..c34df246 100644 --- a/src/ssb.c +++ b/src/ssb.c @@ -2722,22 +2722,30 @@ typedef struct _connect_t static void _tf_on_connect_getaddrinfo(uv_getaddrinfo_t* addrinfo, int result, struct addrinfo* info) { connect_t* connect = addrinfo->data; - if (result == 0 && info) + if (!connect->ssb->shutting_down) { - struct sockaddr_in addr = *(struct sockaddr_in*)info->ai_addr; - addr.sin_port = htons(connect->port); - tf_ssb_connection_create(connect->ssb, connect->host, &addr, connect->key); + if (result == 0 && info) + { + struct sockaddr_in addr = *(struct sockaddr_in*)info->ai_addr; + addr.sin_port = htons(connect->port); + tf_ssb_connection_create(connect->ssb, connect->host, &addr, connect->key); + } + else + { + tf_printf("getaddrinfo(%s) => %s\n", connect->host, uv_strerror(result)); + } } - else - { - tf_printf("getaddrinfo(%s) => %s\n", connect->host, uv_strerror(result)); - } - tf_free(connect); uv_freeaddrinfo(info); + tf_ssb_unref(connect->ssb); + tf_free(connect); } void tf_ssb_connect(tf_ssb_t* ssb, const char* host, int port, const uint8_t* key) { + if (ssb->shutting_down) + { + return; + } connect_t* connect = tf_malloc(sizeof(connect_t)); *connect = (connect_t) { .ssb = ssb, @@ -2749,11 +2757,13 @@ void tf_ssb_connect(tf_ssb_t* ssb, const char* host, int port, const uint8_t* ke tf_ssb_connections_store(ssb->connections_tracker, host, port, id); snprintf(connect->host, sizeof(connect->host), "%s", host); memcpy(connect->key, key, k_id_bin_len); + tf_ssb_ref(ssb); int r = uv_getaddrinfo(ssb->loop, &connect->req, _tf_on_connect_getaddrinfo, host, NULL, &(struct addrinfo) { .ai_family = AF_INET }); if (r < 0) { tf_printf("uv_getaddrinfo: %s\n", uv_strerror(r)); tf_free(connect); + tf_ssb_unref(ssb); } }