From c1f5c8868921a496d74343817f77975aeb5af835 Mon Sep 17 00:00:00 2001
From: Matthew Hague <matthew.hague@rhul.ac.uk>
Date: Fri, 19 Nov 2021 17:42:19 +0000
Subject: [PATCH] Destroy process if did not exit

---
 .../uk/ac/rhul/cs/javatester/SubmissionWrapper.java  | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/src/main/java/uk/ac/rhul/cs/javatester/SubmissionWrapper.java b/src/main/java/uk/ac/rhul/cs/javatester/SubmissionWrapper.java
index eba0894..03a88bf 100644
--- a/src/main/java/uk/ac/rhul/cs/javatester/SubmissionWrapper.java
+++ b/src/main/java/uk/ac/rhul/cs/javatester/SubmissionWrapper.java
@@ -65,6 +65,7 @@ public class SubmissionWrapper implements AutoCloseable {
     }
 
     private static final int TIMEOUT = 2; // seconds
+    private static final int FLUSH_TIMEOUT = 5; // seconds
 
     // use getLine to get output, don't use these directly
     private Process process;
@@ -604,7 +605,9 @@ public class SubmissionWrapper implements AutoCloseable {
      */
     private void shutdownProcess() {
         try {
-            process.waitFor(TIMEOUT, TimeUnit.SECONDS);
+            boolean exited = process.waitFor(TIMEOUT, TimeUnit.SECONDS);
+            if (!exited)
+                process.destroyForcibly();
         } catch (InterruptedException e) {
             // make sure it's really shutdown
             process.destroyForcibly();
@@ -617,7 +620,7 @@ public class SubmissionWrapper implements AutoCloseable {
     private void flushOutputStreams() {
         getLinesFromStderr();
         getLinesFromStdout();
-    }
+     }
 
     /**
      * Get as many lines as possible from the submission stdout
@@ -627,8 +630,9 @@ public class SubmissionWrapper implements AutoCloseable {
     private void getLinesFromStdout() {
         try {
             String line;
-            while ((line = getLine()) != null)
+            while ((line = getLine()) != null) {
                 outputLines.add(line);
+            }
         } catch (TimeoutException e) {
             // do nothing
         }
@@ -665,6 +669,8 @@ public class SubmissionWrapper implements AutoCloseable {
             });
         } catch (TimeoutException e) {
             // do nothing
+        } catch (InterruptedException e) {
+            // do nothing
         } catch (Exception e) {
             System.err.println("Something failed badly awaiting response.");
             System.err.println(e);
-- 
GitLab