Skip to content

Commit c19743c

Browse files
arthurkehrwaldfreezy
authored andcommitted
Improve MPF start failure handling
1 parent d218b3b commit c19743c

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

Runtime/MpfWrangler.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,9 @@ public async Task StartMpf(MachineState initialState, CancellationToken ct)
222222
}
223223
catch (Exception ex)
224224
{
225-
_mpfProcess?.Kill();
225+
if (_mpfProcess != null && !_mpfProcess.HasExited)
226+
_mpfProcess.Kill();
227+
226228
_mpfProcess?.Dispose();
227229
_mpfProcess = null;
228230
MpfState = MpfState.NotConnected;
@@ -306,7 +308,7 @@ await Task.WhenAny(
306308
) != processExited.Task
307309
&& !_mpfProcess.HasExited
308310
)
309-
_mpfProcess?.Kill();
311+
_mpfProcess.Kill();
310312
}
311313

312314
_receiveMpfCommandsTask = null;
@@ -344,7 +346,8 @@ private Process StartMpfProcess()
344346
{
345347
// On Linux and macOS, start the process through the terminal so it has a window.
346348
#if UNITY_EDITOR_LINUX || UNITY_STANDALONE_LINUX
347-
process.StartInfo.Arguments = $"-e {process.StartInfo.FileName} {process.StartInfo.Arguments}";
349+
process.StartInfo.Arguments =
350+
$"-e {process.StartInfo.FileName} {process.StartInfo.Arguments}";
348351
process.StartInfo.FileName = "x-terminal-emulator";
349352
#elif UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
350353
// There is no way to pass arguments trough the macOS terminal,
@@ -353,7 +356,10 @@ private Process StartMpfProcess()
353356
// Very convoluted but there is no better way as far as Stackoverflow knows:
354357
// https://stackoverflow.com/questions/29510815/how-to-pass-command-line-arguments-to-a-program-run-with-the-open-command
355358
string tmpScriptPath = Path.Combine(Application.temporaryCachePath, "mpf.sh");
356-
File.WriteAllText(tmpScriptPath, $"#!/bin/bash\n{process.StartInfo.FileName} {process.StartInfo.Arguments}");
359+
File.WriteAllText(
360+
tmpScriptPath,
361+
$"#!/bin/bash\n{process.StartInfo.FileName} {process.StartInfo.Arguments}"
362+
);
357363
Process.Start("chmod", $"u+x {tmpScriptPath}");
358364
process.StartInfo.Arguments = $"-a Terminal {tmpScriptPath}";
359365
process.StartInfo.FileName = "open";

0 commit comments

Comments
 (0)