diff --git a/src/generator.c b/src/generator.c index 33dd9fa0..01d4be7b 100644 --- a/src/generator.c +++ b/src/generator.c @@ -1166,7 +1166,9 @@ static void close_proxy_socket(proxy_instance_t *proxy, proxy_instance_t *subpro /* Remove the subproxy from the proxi list and put it on the dead list. * Further use of the subproxy pointer may point to a new proxy but will not * dereference. This will only disable subproxies so parent proxies need to - * have their disabled bool set manually. */ + * have their disabled bool set manually. This should only be called from the + * receiving threads *proxy_recv to avoid a race on the proxy or connsock data. + */ static void disable_subproxy(gdata_t *gdata, proxy_instance_t *proxi, proxy_instance_t *subproxy) { subproxy->alive = false; @@ -1241,7 +1243,7 @@ static bool parse_reconnect(proxy_instance_t *proxy, json_t *val) ret = true; parent = proxy->parent; - disable_subproxy(gdata, parent, proxy); + proxy->disabled = true; if (parent != proxy) { /* If this is a subproxy we only need to create a new one if * the url has changed. Otherwise automated recruiting will @@ -1606,7 +1608,7 @@ static void drop_proxy(gdata_t *gdata, const char *buf) return; } LOGNOTICE("Generator asked to drop proxy %d:%d", id, subid); - disable_subproxy(gdata, proxy, subproxy); + subproxy->disabled = true; } static void stratifier_reconnect_client(ckpool_t *ckp, const int64_t id) @@ -1844,7 +1846,7 @@ static void send_json_msgq(gdata_t *gdata, cs_msg_t **csmsgq) csmsg->len = 0; LOGNOTICE("Proxy %d:%d %s failed to send msg in send_json_msgq, dropping", proxy->id, proxy->subid, proxy->url); - disable_subproxy(gdata, proxy->parent, proxy); + proxy->disabled = true; } csmsg->ofs += ret; csmsg->len -= ret; @@ -2523,12 +2525,12 @@ static void *proxy_recv(void *arg) subproxy->id, subproxy->subid, cs->buf); } } + if (cs) + cksem_post(&cs->sem); /* Process hangup only after parsing messages */ - if (hup) + if (hup || subproxy->disabled) disable_subproxy(gdata, proxi, subproxy); - if (cs) - cksem_post(&cs->sem); } return NULL; @@ -2651,10 +2653,8 @@ static void *userproxy_recv(void *arg) cksem_post(&cs->sem); } - if (hup) { + if (hup || proxy->disabled) disable_subproxy(gdata, proxy->parent, proxy); - continue; - } } return NULL; } @@ -2980,7 +2980,6 @@ static void parse_ableproxy(gdata_t *gdata, const int sockd, const char *buf, bo if (disable) { /* Set disabled bool here in case this is a parent proxy */ proxy->disabled = true; - disable_subproxy(gdata, proxy, proxy); } else reconnect_proxy(proxy); out: