Skip to content

Commit 62a97a9

Browse files
committed
Improved error catching and handling.
1 parent 2a0bca3 commit 62a97a9

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ minecraft_version = 1.12.2
33
forge_version = 14.23.5.2847
44

55
mod_name = OpenProlog
6-
mod_version=0.3.0
6+
mod_version=0.3.1
77
opencomputers_version=1.7.5.192
88

99
org.gradle.jvmargs = -Xmx2048m

src/main/java/com/syntheticentropy/ocpro/PrologArchitecture.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public class PrologArchitecture implements Architecture {
2929
private FutureTask<Object> vmQueryResponseFutureTask;
3030
private int vmQuerySleepTicks;
3131

32+
private ExecutionResult forceExecutionResult;
33+
3234
enum VMResponse {
3335
RunSynchronous,
3436
Wait,
@@ -65,6 +67,9 @@ public void runSynchronized() {
6567

6668
@Override
6769
public ExecutionResult runThreaded(boolean isSynchronizedReturn) {
70+
if (forceExecutionResult != null)
71+
return forceExecutionResult;
72+
6873
//Called for: synchronized return, init, or signal
6974
vmQueryResponse = VMResponse.None;
7075
vmQueryResponseFutureTask = new FutureTask<>(() -> "");
@@ -109,13 +114,19 @@ public ExecutionResult runThreaded(boolean isSynchronizedReturn) {
109114
}
110115
// (magical time where prolog engine resolves part of the query)
111116
try {
117+
if (vm.exitException != null) {
118+
return new ExecutionResult.Error(vm.exitException.getMessage());
119+
}
120+
if (vm.getState() == Thread.State.TERMINATED) {
121+
return new ExecutionResult.Error("Terminated early");
122+
}
112123
vmQueryResponseFutureTask.get(30, TimeUnit.SECONDS);
113124
// vmQueryResponseFutureTask.get(3, TimeUnit.SECONDS);
114125
} catch (InterruptedException | ExecutionException | TimeoutException e) {
115126
if (vm.exitException != null) {
116127
return new ExecutionResult.Error(vm.exitException.getMessage());
117128
}
118-
return new ExecutionResult.Error("Some problem eh?");
129+
return new ExecutionResult.Error("Some problem eh? (1)");
119130
}
120131

121132
// a query has sent us something (or the thread exited)
@@ -137,7 +148,7 @@ public ExecutionResult runThreaded(boolean isSynchronizedReturn) {
137148
if (vm.exitException != null) {
138149
return new ExecutionResult.Error(vm.exitException.getMessage());
139150
}
140-
return new ExecutionResult.Error("Some problem eh?");
151+
return new ExecutionResult.Error("Some problem eh? (2)");
141152
}
142153

143154
// a query calls this
@@ -179,6 +190,18 @@ public void waitingCall(int ticks) {
179190
}
180191
}
181192

193+
public void crash(String e) {
194+
forceExecutionResult = new ExecutionResult.Error(e);
195+
196+
if (!vmQueryResponseFutureTask.isDone()) {
197+
vmQueryResponseFutureTask.cancel(true);
198+
}
199+
if (!synchronizedFutureTask.isDone()) {
200+
synchronizedFutureTask.cancel(true);
201+
}
202+
// machine.crash(e);
203+
}
204+
182205
@Override
183206
public boolean isInitialized() {
184207
return vm != null && vm.getState() != Thread.State.NEW;

src/main/java/com/syntheticentropy/ocpro/PrologVM.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class PrologVM extends Thread {
1111

1212
JIPEngine jip;
1313
PrologArchitecture owner;
14-
State state = State.Created;
14+
public State state = State.Created;
1515
Exception exitException;
1616

1717
@Override
@@ -27,12 +27,14 @@ public void run() {
2727
queryTerm = jipQuery.nextSolution();
2828
}
2929
// TODO: in signal query mode, wait for and run signal queries until shutdown is requested
30+
owner.crash("Query resolved early");
3031
} else {
3132
// run main query
3233
}
3334
} catch (Exception e) {
35+
state = State.Terminated;
3436
exitException = e;
35-
owner.machine.crash(e.getMessage());
37+
owner.crash(e.getMessage());
3638
}
3739
}
3840

0 commit comments

Comments
 (0)