From: Christian Brauner Date: Thu, 14 Dec 2017 19:57:15 +0000 (+0100) Subject: commands: fix race when open()/close() cmd socket X-Git-Tag: lxc-2.0.10~466 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=59beaa6f7b729d89f626e277334e8872fc455837;p=thirdparty%2Flxc.git commands: fix race when open()/close() cmd socket When we report STOPPED to a caller and then close the command socket it is technically possible - and I've seen this happen on the test builders - that a container start() right after a wait() will receive ECONNREFUSED because it called open() before we close(). So for all new state clients simply close the command socket. This will inform all state clients that the container is STOPPED and also prevents a race between a open()/close() on the command socket causing a new process to get ECONNREFUSED because we haven't yet closed the command socket. Signed-off-by: Christian Brauner --- diff --git a/src/lxc/start.c b/src/lxc/start.c index 711712059..7e3396490 100644 --- a/src/lxc/start.c +++ b/src/lxc/start.c @@ -754,12 +754,20 @@ void lxc_fini(const char *name, struct lxc_handler *handler) cgroup_destroy(handler); - lxc_set_state(name, handler, STOPPED); - - /* close command socket */ + /* For all new state clients simply close the command socket. + * This will inform all state clients that the container is + * STOPPED and also prevents a race between a open()/close() on + * the command socket causing a new process to get ECONNREFUSED + * because we haven't yet closed the command socket. + */ close(handler->conf->maincmd_fd); handler->conf->maincmd_fd = -1; + /* This function will try to connect to the legacy lxc-monitord state + * server and only exists for backwards compatibility. + */ + lxc_monitor_send_state(name, STOPPED, handler->lxcpath); + if (run_lxc_hooks(name, "post-stop", handler->conf, handler->lxcpath, NULL)) { ERROR("Failed to run lxc.hook.post-stop for container \"%s\".", name); if (handler->conf->reboot) {