From a944218e9c1d5ff1aca34609146389dc680335b7 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Sun, 12 Oct 2008 12:07:48 +0200 Subject: [PATCH] [BUG] ensure that listeners from disabled proxies are correctly unbound. There is a problem when an instance is marked "disabled". Its ports are still bound but will not be unbound upon termination. This causes processes to accumulate during soft restarts, and might even cause failures to restart new ones due to the inability to bind to the same port. The ideal solution would be to bind all ports at the end of the configuration parsing. An acceptable workaround is to unbind all listeners of disabled proxies. This is what the current patch does. --- include/proto/proxy.h | 1 + src/cfgparse.c | 2 ++ src/proxy.c | 32 +++++++++++++++++++++++--------- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/include/proto/proxy.h b/include/proto/proxy.h index 7268bfaad..72394788c 100644 --- a/include/proto/proxy.h +++ b/include/proto/proxy.h @@ -30,6 +30,7 @@ int start_proxies(int verbose); void maintain_proxies(struct timeval *next); void soft_stop(void); void pause_proxy(struct proxy *p); +void stop_proxy(struct proxy *p); void pause_proxies(void); void listen_proxies(void); diff --git a/src/cfgparse.c b/src/cfgparse.c index d34bfbe1c..b33800c73 100644 --- a/src/cfgparse.c +++ b/src/cfgparse.c @@ -2726,6 +2726,8 @@ int readcfgfile(const char *file) struct listener *listener; if (curproxy->state == PR_STSTOPPED) { + /* ensure we don't keep listeners uselessly bound */ + stop_proxy(curproxy); curproxy = curproxy->next; continue; } diff --git a/src/proxy.c b/src/proxy.c index 6fad76eae..358132995 100644 --- a/src/proxy.c +++ b/src/proxy.c @@ -351,15 +351,7 @@ void maintain_proxies(struct timeval *next) if (t == 0) { Warning("Proxy %s stopped.\n", p->id); send_log(p, LOG_WARNING, "Proxy %s stopped.\n", p->id); - - for (l = p->listen; l != NULL; l = l->next) { - unbind_listener(l); - if (l->state >= LI_ASSIGNED) { - delete_listener(l); - listeners--; - } - } - p->state = PR_STSTOPPED; + stop_proxy(p); /* try to free more memory */ pool_gc2(); } @@ -421,6 +413,28 @@ void pause_proxy(struct proxy *p) } } + +/* + * This function completely stops a proxy and releases its listeners. It has + * to be called when going down in order to release the ports so that another + * process may bind to them. It must also be called on disabled proxies at the + * end of start-up. When all listeners are closed, the proxy is set to the + * PR_STSTOPPED state. + */ +void stop_proxy(struct proxy *p) +{ + struct listener *l; + + for (l = p->listen; l != NULL; l = l->next) { + unbind_listener(l); + if (l->state >= LI_ASSIGNED) { + delete_listener(l); + listeners--; + } + } + p->state = PR_STSTOPPED; +} + /* * This function temporarily disables listening so that another new instance * can start listening. It is designed to be called upon reception of a