@@ -140,6 +140,9 @@ struct dns_cache_entry
140140 struct addrinfo * addr ;
141141 retro_time_t timestamp ;
142142 bool valid ;
143+ #ifdef HAVE_THREADS
144+ sthread_t * thread ;
145+ #endif
143146 struct dns_cache_entry * next ;
144147};
145148
@@ -580,6 +583,13 @@ static void net_http_dns_cache_remove_expired(void)
580583 if ( (entry -> addr && (entry -> timestamp + dns_cache_timeout < cpu_features_get_time_usec ()))
581584 || (!entry -> addr && (entry -> timestamp + dns_cache_fail_timeout < cpu_features_get_time_usec ())))
582585 {
586+ #ifdef HAVE_THREADS
587+ if (entry -> thread )
588+ {
589+ sthread_join (entry -> thread );
590+ entry -> thread = NULL ;
591+ }
592+ #endif
583593 if (prev )
584594 prev -> next = entry -> next ;
585595 else
@@ -609,6 +619,13 @@ static struct dns_cache_entry *net_http_dns_cache_find(const char *domain, int p
609619 {
610620 if (port == entry -> port && string_is_equal (entry -> domain , domain ))
611621 {
622+ #ifdef HAVE_THREADS
623+ if (entry -> thread && entry -> valid )
624+ {
625+ sthread_join (entry -> thread );
626+ entry -> thread = NULL ;
627+ }
628+ #endif
612629 /* don't bump timeestamp for failures */
613630 if (entry -> addr )
614631 entry -> timestamp = cpu_features_get_time_usec ();
@@ -629,6 +646,9 @@ static struct dns_cache_entry *net_http_dns_cache_add(const char *domain, int po
629646 entry -> addr = addr ;
630647 entry -> timestamp = cpu_features_get_time_usec ();
631648 entry -> valid = (addr != NULL );
649+ #ifdef HAVE_THREADS
650+ entry -> thread = NULL ;
651+ #endif
632652 entry -> next = dns_cache ;
633653 dns_cache = entry ;
634654 return entry ;
@@ -688,7 +708,7 @@ static void net_http_conn_pool_remove_expired(void)
688708 entry = conn_pool ;
689709 while (entry )
690710 {
691- if (!entry -> in_use )
711+ if (!entry -> in_use && entry -> fd >= 0 && entry -> fd < FD_SETSIZE )
692712 {
693713 FD_SET (entry -> fd , & fds );
694714 if (entry -> fd >= max )
@@ -883,8 +903,6 @@ static bool net_http_new_socket(struct http_t *state)
883903 struct dns_cache_entry * entry ;
884904
885905#ifdef HAVE_THREADS
886- sthread_t * thread ;
887-
888906 if (!dns_cache_lock )
889907 dns_cache_lock = slock_new ();
890908 LOCK_DNS_CACHE ();
@@ -925,8 +943,7 @@ static bool net_http_new_socket(struct http_t *state)
925943 entry = net_http_dns_cache_add (state -> request .domain , state -> request .port , NULL );
926944#ifdef HAVE_THREADS
927945 /* create the entry for it as an indicator that the request is underway */
928- thread = sthread_create (net_http_resolve , entry );
929- sthread_detach (thread );
946+ entry -> thread = sthread_create (net_http_resolve , entry );
930947#else
931948 net_http_resolve (entry );
932949#endif
@@ -1444,6 +1461,7 @@ bool net_http_update(struct http_t *state, size_t* progress, size_t* total)
14441461 if (_len < 0 || state -> err )
14451462 {
14461463 net_http_conn_pool_remove (state -> conn );
1464+ state -> conn = NULL ;
14471465 state -> err = true;
14481466 response -> part = P_DONE ;
14491467 response -> status = -1 ;
@@ -1457,6 +1475,7 @@ bool net_http_update(struct http_t *state, size_t* progress, size_t* total)
14571475 if (!net_http_receive_body (state , _len ))
14581476 {
14591477 net_http_conn_pool_remove (state -> conn );
1478+ state -> conn = NULL ;
14601479 state -> err = true;
14611480 response -> part = P_DONE ;
14621481 response -> status = -1 ;
0 commit comments