Skip to content

Commit

Permalink
Prevented C# bots from popping up exception dialogs if they threw
Browse files Browse the repository at this point in the history
unhandled exceptions.
  • Loading branch information
Matt Van Der Westhuizen committed Jun 1, 2015
1 parent fc03e1e commit 4040177
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 23 deletions.
1 change: 1 addition & 0 deletions ChallengeHarness/ChallengeHarness.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="BotMeta.cs" />
<Compile Include="ChangeErrorMode.cs" />
<Compile Include="Loggers\ConsoleLogger.cs" />
<Compile Include="Loggers\ConsoleScrollingLogger.cs" />
<Compile Include="Loggers\ILogger.cs" />
Expand Down
48 changes: 48 additions & 0 deletions ChallengeHarness/ChangeErrorMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace ChallengeHarness.Runners
{
/// <summary>
/// Set the window's error mode for this process. Be very careful with this --- this is a process wide
/// setting, so in a multi-threaded situation it can do crazy things!
/// Code pulled from: http://msdn.microsoft.com/en-us/magazine/cc163866.aspx
/// </summary>
public struct ChangeErrorMode : IDisposable
{
/// <summary>
/// Error modes that can be enabled or disabled.
/// </summary>
[Flags]
public enum ErrorModes
{
Default = 0x0,
FailCriticalErrors = 0x1,
NoGpFaultErrorBox = 0x2,
NoAlignmentFaultExcept = 0x4,
NoOpenFileErrorBox = 0x8000
}

private int _oldMode;

/// <summary>
/// Set the new error mode. When Dispose is called it will reset
/// to the original value (use the "using" statement).
/// </summary>
/// <param name="mode"></param>
public ChangeErrorMode(ErrorModes mode)
{
_oldMode = SetErrorMode((int)mode);
}

void IDisposable.Dispose() { SetErrorMode(_oldMode); }

[DllImport("kernel32.dll")]
private static extern int SetErrorMode(int newMode);
}
}
49 changes: 26 additions & 23 deletions ChallengeHarness/Runners/BotRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public string GetMove(MatchRender rendered)
var process = CreateProcess();
AddEventHandlersToProcess(process);
StartProcess(process);
var result = HandleProcessResponse(process);
string result = HandleProcessResponse(process);

AppendLogs();
ClearRoundFiles();
Expand Down Expand Up @@ -185,31 +185,34 @@ private void AddEventHandlersToProcess(Process p)

private void StartProcess(Process p)
{
p.Start();
p.BeginOutputReadLine();
p.BeginErrorReadLine();
using (ChangeErrorMode newErrorMode = new ChangeErrorMode(ChangeErrorMode.ErrorModes.FailCriticalErrors | ChangeErrorMode.ErrorModes.NoGpFaultErrorBox))
{
p.Start();
p.BeginOutputReadLine();
p.BeginErrorReadLine();

var didExit = p.WaitForExit(Settings.Default.MoveTimeoutSeconds*1000);
_botTimer.Stop();
var didExit = p.WaitForExit(Settings.Default.MoveTimeoutSeconds * 1000);
_botTimer.Stop();

if (!didExit)
{
if (!p.HasExited)
p.Kill();
OutputAppendLog(String.Format("[GAME]\tBot {0} timed out after {1} ms.", PlayerName,
_botTimer.ElapsedMilliseconds));
OutputAppendLog(String.Format("[GAME]\tKilled process {0}.", _processName));
}
else
{
OutputAppendLog(String.Format("[GAME]\tBot {0} finished in {1} ms.", PlayerName,
_botTimer.ElapsedMilliseconds));
}
if (!didExit)
{
if (!p.HasExited)
p.Kill();
OutputAppendLog(String.Format("[GAME]\tBot {0} timed out after {1} ms.", PlayerName,
_botTimer.ElapsedMilliseconds));
OutputAppendLog(String.Format("[GAME]\tKilled process {0}.", _processName));
}
else
{
OutputAppendLog(String.Format("[GAME]\tBot {0} finished in {1} ms.", PlayerName,
_botTimer.ElapsedMilliseconds));
}

if ((didExit) && (p.ExitCode != 0))
{
OutputAppendLog(String.Format("[GAME]\tProcess exited with non-zero code {0} from player {1}.",
p.ExitCode, PlayerName));
if ((didExit) && (p.ExitCode != 0))
{
OutputAppendLog(String.Format("[GAME]\tProcess exited with non-zero code {0} from player {1}.",
p.ExitCode, PlayerName));
}
}
}

Expand Down

0 comments on commit 4040177

Please sign in to comment.