From 4d751562f69852cf30239066326874864d781d64 Mon Sep 17 00:00:00 2001 From: Davyd McColl Date: Wed, 11 Oct 2023 18:07:03 +0200 Subject: [PATCH] :alembic: abort the watcher thread if stuck & only wait 1 second to rejoin --- .../PeanutButter.TempRedis/TempRedis.cs | 38 ++++++++++++++++--- 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/source/TempDb/PeanutButter.TempRedis/TempRedis.cs b/source/TempDb/PeanutButter.TempRedis/TempRedis.cs index 62b546b7b7..f554a6874c 100644 --- a/source/TempDb/PeanutButter.TempRedis/TempRedis.cs +++ b/source/TempDb/PeanutButter.TempRedis/TempRedis.cs @@ -213,6 +213,7 @@ private bool ReadProcessIsRunning() private string _executable; private bool _stopped = false; + private bool _watching = false; private Thread _watcherThread; private AutoTempFile _configFile; private AutoTempFile _saveFile; @@ -416,19 +417,36 @@ private void WatchServerProcess() var t = new Thread( () => { - while (!_stopped) + try { - RestartServerIfRequired(); - Thread.Sleep(100); + while (_watching) + { + RestartServerIfRequired(); + Thread.Sleep(100); + } + } + catch (ThreadAbortException) + { + // suppress + } + catch (Exception ex) + { + _logger($"{nameof(WatchServerProcess)} died: {ex}"); } } ); - t.Start(); + _watching = false; var existingWatcher = Interlocked.Exchange(ref _watcherThread, t); if (existingWatcher is not null) { - existingWatcher.Join(); + if (!existingWatcher.Join(1000)) + { + existingWatcher.Abort(); + } } + + _watching = true; + t.Start(); } private void RestartServerIfRequired() @@ -485,10 +503,18 @@ public void Stop() { try { + _watching = false; var watcher = Interlocked.Exchange(ref _watcherThread, null); + if (watcher is not null) + { + if (!watcher.Join(1000)) + { + watcher.Abort(); + } + } + _stopped = true; _logger("stopping redis-server watcher thread"); - watcher?.Join(); _logger("killing redis-server"); var serverProcess = Interlocked.Exchange(ref _serverProcess, null); serverProcess?.Kill();