DroidFish: More robust handling of misbehaving engines.

This commit is contained in:
Peter Osterlund 2011-12-31 20:09:31 +00:00
parent 6f25af50d9
commit d8782830a9
2 changed files with 19 additions and 2 deletions

View File

@ -31,6 +31,8 @@ int main(int argc, char* argv[]);
static int fdFromChild = -1; static int fdFromChild = -1;
static int fdToChild = -1; static int fdToChild = -1;
static int childpid = -1;
/* /*
* Class: org_petero_droidfish_engine_NativePipedProcess * Class: org_petero_droidfish_engine_NativePipedProcess
* Method: startProcess * Method: startProcess
@ -39,13 +41,16 @@ static int fdToChild = -1;
extern "C" JNIEXPORT void JNICALL Java_org_petero_droidfish_engine_NativePipedProcess_startProcess extern "C" JNIEXPORT void JNICALL Java_org_petero_droidfish_engine_NativePipedProcess_startProcess
(JNIEnv* env, jobject obj) (JNIEnv* env, jobject obj)
{ {
if (childpid != -1)
kill(childpid, SIGKILL);
int fd1[2]; /* parent -> child */ int fd1[2]; /* parent -> child */
int fd2[2]; /* child -> parent */ int fd2[2]; /* child -> parent */
if (pipe(fd1) < 0) if (pipe(fd1) < 0)
exit(1); exit(1);
if (pipe(fd2) < 0) if (pipe(fd2) < 0)
exit(1); exit(1);
int childpid = fork(); childpid = fork();
if (childpid == -1) { if (childpid == -1) {
exit(1); exit(1);
} }

View File

@ -302,6 +302,7 @@ public class DroidComputerPlayer {
/** Start an engine, if not already started. /** Start an engine, if not already started.
* Will shut down old engine first, if needed. */ * Will shut down old engine first, if needed. */
public final synchronized void queueStartEngine(int id, String engine) { public final synchronized void queueStartEngine(int id, String engine) {
killOldEngine(engine);
stopSearch(); stopSearch();
searchRequest = SearchRequest.startRequest(id, engine); searchRequest = SearchRequest.startRequest(id, engine);
handleQueue(); handleQueue();
@ -314,6 +315,7 @@ public class DroidComputerPlayer {
* command, such as "draw" and "resign". * command, such as "draw" and "resign".
*/ */
public final synchronized void queueSearchRequest(SearchRequest sr) { public final synchronized void queueSearchRequest(SearchRequest sr) {
killOldEngine(sr.engine);
stopSearch(); stopSearch();
if (sr.ponderMove != null) if (sr.ponderMove != null)
@ -364,6 +366,7 @@ public class DroidComputerPlayer {
/** Start analyzing a position. */ /** Start analyzing a position. */
public final synchronized void queueAnalyzeRequest(SearchRequest sr) { public final synchronized void queueAnalyzeRequest(SearchRequest sr) {
killOldEngine(sr.engine);
stopSearch(); stopSearch();
// If no legal moves, there is nothing to analyze // If no legal moves, there is nothing to analyze
@ -385,6 +388,12 @@ public class DroidComputerPlayer {
handleIdleState(); handleIdleState();
} }
private void killOldEngine(String engine) {
if (engine.equals(engineState.engine))
return;
shutdownEngine();
}
/** Tell engine to stop searching. */ /** Tell engine to stop searching. */
public final synchronized boolean stopSearch() { public final synchronized boolean stopSearch() {
searchRequest = null; searchRequest = null;
@ -562,7 +571,10 @@ public class DroidComputerPlayer {
private final void monitorLoop() { private final void monitorLoop() {
while (true) { while (true) {
int timeout = getReadTimeout(); int timeout = getReadTimeout();
String s = uciEngine.readLineFromEngine(timeout); UCIEngine uci = uciEngine;
if (uci == null)
return;
String s = uci.readLineFromEngine(timeout);
if (Thread.currentThread().isInterrupted()) if (Thread.currentThread().isInterrupted())
return; return;
processEngineOutput(s); processEngineOutput(s);