Fix shutdown issues with in-flight SSB connection attempts.

This commit is contained in:
Cory McWilliams 2024-05-15 12:37:13 -04:00
parent f79d7b35a4
commit 74bb2151c1

View File

@ -2722,6 +2722,8 @@ typedef struct _connect_t
static void _tf_on_connect_getaddrinfo(uv_getaddrinfo_t* addrinfo, int result, struct addrinfo* info) static void _tf_on_connect_getaddrinfo(uv_getaddrinfo_t* addrinfo, int result, struct addrinfo* info)
{ {
connect_t* connect = addrinfo->data; connect_t* connect = addrinfo->data;
if (!connect->ssb->shutting_down)
{
if (result == 0 && info) if (result == 0 && info)
{ {
struct sockaddr_in addr = *(struct sockaddr_in*)info->ai_addr; struct sockaddr_in addr = *(struct sockaddr_in*)info->ai_addr;
@ -2732,12 +2734,18 @@ static void _tf_on_connect_getaddrinfo(uv_getaddrinfo_t* addrinfo, int result, s
{ {
tf_printf("getaddrinfo(%s) => %s\n", connect->host, uv_strerror(result)); tf_printf("getaddrinfo(%s) => %s\n", connect->host, uv_strerror(result));
} }
tf_free(connect); }
uv_freeaddrinfo(info); 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) 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_t* connect = tf_malloc(sizeof(connect_t));
*connect = (connect_t) { *connect = (connect_t) {
.ssb = ssb, .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); tf_ssb_connections_store(ssb->connections_tracker, host, port, id);
snprintf(connect->host, sizeof(connect->host), "%s", host); snprintf(connect->host, sizeof(connect->host), "%s", host);
memcpy(connect->key, key, k_id_bin_len); 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 }); int r = uv_getaddrinfo(ssb->loop, &connect->req, _tf_on_connect_getaddrinfo, host, NULL, &(struct addrinfo) { .ai_family = AF_INET });
if (r < 0) if (r < 0)
{ {
tf_printf("uv_getaddrinfo: %s\n", uv_strerror(r)); tf_printf("uv_getaddrinfo: %s\n", uv_strerror(r));
tf_free(connect); tf_free(connect);
tf_ssb_unref(ssb);
} }
} }