Skip to content

Commit

Permalink
Fixes #41, Blocking: Process can leak in Self Play
Browse files Browse the repository at this point in the history
Two issues addressed in this change.  1) while waiting for the engine
response (which can be on the order of seconds/minutes worst case)
we also wait on the exit event now, in case it comes during that time.
2) The Dispose path for the UCIChessEngine class was just wrong.  At
some point the code diverged and I forgot to clean it up.  Thanks to
stockfish being so damned resilient, I missed it until now.  It's why
testing is important.
  • Loading branch information
Matthew Baker committed Sep 27, 2017
1 parent 1e6227f commit 3f509c6
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions AllPawnsMustDie/core/UCIChessEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ public void Dispose()
{
// Stop listening to stdout of engine process
engineProcess.OutputDataReceived -= OnDataReceived;
// Send the UCI quit if we haven't
((IChessEngine)this).Quit();

// Dispose of the process
engineProcess.Dispose();
disposed = true;
Expand Down Expand Up @@ -357,8 +360,13 @@ public void CommandQueueExecutionMethod()
engineProcess.StandardInput.WriteLine(IsReady);
}

// Wait for the response
uciCommandFinished.WaitOne();
// Wait on the exit event as well, so we can bail as fast as needed
AutoResetEvent[] commandEvents = { uciCommandFinished, shutdownCommandQueue };
int commandWait = WaitHandle.WaitAny(commandEvents);
if (commandWait == 1)
{
exitThread = true;
}
}
}
}
Expand All @@ -373,10 +381,7 @@ void IChessEngine.Quit()
// engine will exit ASAP, so assume it's gone after this
if (engineProcess != null && !(Disposed))
{
// Unsubscribe from the handler as this will close the process
//engineProcess.OutputDataReceived -= OnDataReceived;
engineProcess.StandardInput.WriteLine(UciQuit);
Dispose();
}
}
#endregion
Expand Down

0 comments on commit 3f509c6

Please sign in to comment.