From: Martin Willi Date: Wed, 11 Jun 2014 13:04:15 +0000 (+0200) Subject: watcher: Prevent race condition spawning multiple watcher threads X-Git-Tag: 5.2.0rc1~51 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=bc1139480f42859a4b26fe8518ef9dd7c2849a49;p=thirdparty%2Fstrongswan.git watcher: Prevent race condition spawning multiple watcher threads If file descriptors get added and removed in rapid succession, the active watcher thread might not take notice of it and continues running. However, add() spawns a watcher thread whenever a file descriptor is added to an empty set. This could result in multiple watcher threads, which is fixed by a proper check for running watchers. --- diff --git a/src/libstrongswan/processing/watcher.c b/src/libstrongswan/processing/watcher.c index 560e47299a..3518dfdaef 100644 --- a/src/libstrongswan/processing/watcher.c +++ b/src/libstrongswan/processing/watcher.c @@ -260,8 +260,10 @@ static job_requeue_t watch(private_watcher_t *this) FD_ZERO(&ex); this->mutex->lock(this->mutex); + if (this->fds->get_count(this->fds) == 0) { + this->running = FALSE; this->mutex->unlock(this->mutex); return JOB_REQUEUE_NONE; } @@ -405,7 +407,7 @@ METHOD(watcher_t, add, void, this->mutex->lock(this->mutex); this->fds->insert_last(this->fds, entry); - if (this->fds->get_count(this->fds) == 1) + if (!this->running) { this->running = TRUE; lib->processor->queue_job(lib->processor,