Skip to content

Commit

Permalink
⚗️ abort the watcher thread if stuck & only wait 1 second to rejoin
Browse files Browse the repository at this point in the history
  • Loading branch information
fluffynuts committed Oct 11, 2023
1 parent 75f22c0 commit 4d75156
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions source/TempDb/PeanutButter.TempRedis/TempRedis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 4d75156

Please sign in to comment.