From 7127855741faaf9544339db4faa7d25b0158fdfc Mon Sep 17 00:00:00 2001
From: Waylon Cude <waylon531@gmail.com>
Date: Mon, 23 Nov 2020 07:20:49 -0800
Subject: [PATCH] Fix synctl and duplicate worker spawning (#8798)

Synctl did not check if a worker thread was already running when using
`synctl start` and would naively start a fresh copy. This would
sometimes lead to cases where many duplicate copies of a single worker
would run.

This fix adds a pid check when starting worker threads and synctl will
now refuse to start individual workers if they're already running.
---
 changelog.d/8798.bugfix | 1 +
 synctl                  | 7 +++++++
 2 files changed, 8 insertions(+)
 create mode 100644 changelog.d/8798.bugfix

diff --git a/changelog.d/8798.bugfix b/changelog.d/8798.bugfix
new file mode 100644
index 0000000000..9bdb2b51ea
--- /dev/null
+++ b/changelog.d/8798.bugfix
@@ -0,0 +1 @@
+Fix a bug where synctl could spawn duplicate copies of a worker. Contributed by Waylon Cude.
diff --git a/synctl b/synctl
index 9395ebd048..cfa9cec0c4 100755
--- a/synctl
+++ b/synctl
@@ -358,6 +358,13 @@ def main():
         for worker in workers:
             env = os.environ.copy()
 
+            # Skip starting a worker if its already running
+            if os.path.exists(worker.pidfile) and pid_running(
+                int(open(worker.pidfile).read())
+            ):
+                print(worker.app + " already running")
+                continue
+
             if worker.cache_factor:
                 os.environ["SYNAPSE_CACHE_FACTOR"] = str(worker.cache_factor)
 
-- 
GitLab