Skip to content

Commit

Permalink
Fixes UCI engine shutdown process
Browse files Browse the repository at this point in the history
  • Loading branch information
fathzer committed Feb 1, 2024
1 parent c28412f commit 313d4f5
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/fathzer/jchess/bot/uci/EngineLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public static Map<String, Engine> loadEngines(final Path path) throws IOExceptio
final List<String> commands = Files.readAllLines(path);
for (String command : commands) {
if (!command.isBlank() && !command.startsWith("#")) {
try (UCIEngine engine = new UCIEngine(command)) {
try {
UCIEngine engine = new UCIEngine(command);
engines.put(engine.getName(), engine);
log.info("engine {} is loaded",engine.getName());
} catch (IOException e) {
Expand Down
50 changes: 50 additions & 0 deletions src/main/java/com/fathzer/jchess/bot/uci/StdErrReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.fathzer.jchess.bot.uci;

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.EOFException;
import java.io.IOException;

class StdErrReader implements Closeable, Runnable {
private final BufferedReader errorReader;
private final Thread spyThread;
private boolean stopped;

StdErrReader(Process process) {
this.errorReader = process.errorReader();
this.spyThread = new Thread(this);
this.spyThread.setDaemon(true);
this.spyThread.start();
}

@Override
public void run() {
while (!stopped) {
try {
final String line = errorReader.readLine();
if (line!=null) {
System.err.println (line); //TODO
}
} catch (EOFException e) {
if (!stopped) {
log(e);
}
} catch (IOException e) {
log(e);
}
}
}

private void log(IOException e) {
synchronized(System.err) {
System.err.println("An error occured, stopped is "+ stopped);
e.printStackTrace(); //TODO
}
}

@Override
public void close() throws IOException {
this.stopped = true;
this.errorReader.close();
}
}
11 changes: 10 additions & 1 deletion src/main/java/com/fathzer/jchess/bot/uci/UCIEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import com.fathzer.jchess.bot.Engine;
import com.fathzer.jchess.bot.Move;
Expand All @@ -21,6 +22,7 @@ public class UCIEngine implements Closeable, Engine {
private final String name;
private final BufferedReader reader;
private final BufferedWriter writer;
private final StdErrReader errorReader;
private final List<Option<?>> options;

public UCIEngine(String path) throws IOException {
Expand All @@ -30,6 +32,7 @@ public UCIEngine(String path) throws IOException {
this.process = processBuilder.start();
this.reader = process.inputReader();
this.writer = process.outputWriter();
this.errorReader = new StdErrReader(process);
this.options = new ArrayList<>();
this.name = init();
}
Expand Down Expand Up @@ -107,8 +110,14 @@ public Move play(PlayParameters params) {

@Override
public void close() throws IOException {
this.write("quit");
this.reader.close();
this.writer.close();
this.process.destroy();
this.errorReader.close();
try {
this.process.waitFor(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
this.process.destroy();
}
}
}

0 comments on commit 313d4f5

Please sign in to comment.