Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Consolidate stop/unload logic and threading across all places. This w…
…ill resolve most of the cases when torch does not stop or close correctly.
  • Loading branch information
zznty committed Feb 16, 2024
commit b883dede4d23705dffdf7bcb4fb33ac3887759bb
1 change: 1 addition & 0 deletions Torch.Server/TorchServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ void DoRestart(Task<GameSaveResult> task, object torch0)
{
config.TempAutostart = true;
torch.Stop();
torch.Destroy();
}

LogManager.Flush();
Expand Down
6 changes: 5 additions & 1 deletion Torch.Server/TorchService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ protected override void OnStart(string[] _)
protected override void OnStop()
{
var mre = new ManualResetEvent(false);
Task.Run(() => _initializer.Server.Stop());
Task.Run(() =>
{
_initializer.Server.Stop();
_initializer.Server.Destroy();
});
if (!mre.WaitOne(TimeSpan.FromMinutes(1)))
Process.GetCurrentProcess().Kill();
}
Expand Down
15 changes: 13 additions & 2 deletions Torch.Server/Views/TorchUI.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,16 @@ private void BtnStop_Click(object sender, RoutedEventArgs e)
{
var result = MessageBox.Show("Are you sure you want to stop the server?", "Stop Server", MessageBoxButton.YesNo);

if (result == MessageBoxResult.Yes)
_server.Invoke(() => _server.Stop());
if (result != MessageBoxResult.Yes)
{
return;
}

Task.Run(() =>
{
_server.Stop();
_server.Destroy();
});
}

protected override void OnClosing(CancelEventArgs e)
Expand All @@ -180,7 +188,10 @@ protected override void OnClosing(CancelEventArgs e)
//_config.Save(); //you idiot

if (_server?.State == ServerState.Running)
{
_server.Stop();
_server.Destroy();
}

_scrollTimer.Stop();

Expand Down
6 changes: 4 additions & 2 deletions Torch/Commands/TorchCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,12 +341,14 @@ private IEnumerable StopCountdown(int countdown, bool save)
ITorchBase torch = (mod as CommandModule)?.Context?.Torch;
Debug.Assert(torch != null);
torch.Stop();
torch.Destroy();
}, this, TaskContinuationOptions.RunContinuationsAsynchronously);
}
else
{
Log.Info("Stopping server.");
Context.Torch.Invoke(() => Context.Torch.Stop());
Context.Torch.Stop();
Context.Torch.Destroy();
}


Expand Down Expand Up @@ -407,7 +409,7 @@ private IEnumerable RestartCountdown(int countdown, bool save)
}

Log.Warn("Initiating server restart.");
Context.Torch.Invoke(() => Context.Torch.Restart(save));
Context.Torch.Restart(save);
yield break;
}

Expand Down