fix: move file IO outside mutex in load_proxy_list

This commit is contained in:
frosty
2026-03-15 15:33:47 -04:00
parent b092bae97b
commit 916c815b08

View File

@@ -102,28 +102,19 @@ int load_proxy_list(const char *filename) {
return 0; return 0;
} }
pthread_mutex_lock(&proxy_mutex);
if (proxy_list) {
free(proxy_list);
proxy_list = NULL;
}
proxy_count = 0;
FILE *file = fopen(filename, "r"); FILE *file = fopen(filename, "r");
if (!file) { if (!file) {
pthread_mutex_unlock(&proxy_mutex);
fprintf(stderr, "[WARN] Could not open proxy list file: %s\n", filename); fprintf(stderr, "[WARN] Could not open proxy list file: %s\n", filename);
return -1; return -1;
} }
int capacity = 16; int temp_capacity = 16;
proxy_list = (Proxy *)malloc(capacity * sizeof(Proxy)); Proxy *temp_list = (Proxy *)malloc(temp_capacity * sizeof(Proxy));
if (!proxy_list) { if (!temp_list) {
fclose(file); fclose(file);
return -1; return -1;
} }
proxy_count = 0; int temp_count = 0;
char line[512]; char line[512];
while (fgets(line, sizeof(line), file)) { while (fgets(line, sizeof(line), file)) {
@@ -151,24 +142,30 @@ int load_proxy_list(const char *filename) {
continue; continue;
} }
if (proxy_count >= capacity) { if (temp_count >= temp_capacity) {
capacity *= 2; temp_capacity *= 2;
Proxy *new_list = (Proxy *)realloc(proxy_list, capacity * sizeof(Proxy)); Proxy *new_list = (Proxy *)realloc(temp_list, temp_capacity * sizeof(Proxy));
if (!new_list) { if (!new_list) {
free(proxy_list); free(temp_list);
proxy_list = NULL;
proxy_count = 0;
fclose(file); fclose(file);
pthread_mutex_unlock(&proxy_mutex);
return -1; return -1;
} }
proxy_list = new_list; temp_list = new_list;
} }
proxy_list[proxy_count++] = proxy; temp_list[temp_count++] = proxy;
} }
fclose(file); fclose(file);
pthread_mutex_lock(&proxy_mutex);
if (proxy_list) {
free(proxy_list);
}
proxy_list = temp_list;
proxy_count = temp_count;
fprintf(stderr, "[INFO] Loaded %d proxies from %s\n", proxy_count, filename); fprintf(stderr, "[INFO] Loaded %d proxies from %s\n", proxy_count, filename);
pthread_mutex_unlock(&proxy_mutex); pthread_mutex_unlock(&proxy_mutex);
return proxy_count; return proxy_count;