Skip to content

Commit d1a679d

Browse files
matmat
authored andcommitted
add timeout for compilation. javac freezes sometimes.
1 parent 76eb399 commit d1a679d

13 files changed

Lines changed: 98 additions & 44 deletions

File tree

‎koans/app/config/config.properties‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
compile_timeout_in_ms=2000
12
debug=false
23
exit_character=Q
34
enable_encouragement=false

‎koans/app/config/file_hashes.dat‎

0 Bytes
Binary file not shown.

‎koans/app/lib/koans.jar‎

-477 KB
Binary file not shown.

‎koans/lib/koans.jar‎

-566 KB
Binary file not shown.

‎koans/run.sh‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
DIR="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
2-
mkdir "$DIR"/bin
2+
mkdir "$DIR"/app
3+
mkdir "$DIR"/app/bin
34
function exitOnError()
45
{
56
rc=$?
@@ -12,4 +13,4 @@ exitOnError
1213
java -version
1314
exitOnError
1415
clear
15-
java -classpath "$DIR"/bin:"$DIR"/lib/koans.jar com.sandwich.koan.runner.AppLauncher "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9"
16+
java -classpath "$DIR"/app/bin:"$DIR"/app/lib/koans.jar com.sandwich.koan.runner.AppLauncher "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9"

‎koans/src/beginner/AboutKoans.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class AboutKoans {
88

99
@Koan
1010
public void findAboutKoansFile(){
11-
fail("delete this line");
11+
fail("delete this line");
1212
}
1313

1414
@Koan

‎lib/file-compiler/src/com/sandwich/util/io/DynamicClassLoader.java‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class DynamicClassLoader extends ClassLoader {
1515
private static Map<URL, Class<?>> classesByLocation = new HashMap<URL, Class<?>>();
1616
private static Map<Class<?>, URL> locationByClass = new HashMap<Class<?>, URL>();
1717

18+
private final long timeout;
1819
private final FileMonitor fileMonitor;
1920
private final String binDir, sourceDir;
2021
private final String[] classPath;
@@ -25,12 +26,19 @@ public DynamicClassLoader(String binDir, String sourceDir, String[] classPath, F
2526

2627
public DynamicClassLoader(String binDir, String sourceDir, String[] classPath,
2728
FileMonitor fileMonitor, ClassLoader parent) {
28-
super(parent);
29+
this(binDir, sourceDir, classPath, fileMonitor, parent, 5000l);
30+
}
31+
32+
public DynamicClassLoader(String binDir, String sourceDir,
33+
String[] classPath, FileMonitor fileMonitor, ClassLoader parent,
34+
long timeout) {
35+
super(parent);
2936
this.fileMonitor = fileMonitor;
3037
this.binDir = binDir;
3138
this.sourceDir = sourceDir;
3239
this.classPath = classPath;
33-
}
40+
this.timeout = timeout;
41+
}
3442

3543
public static void remove(URL url){
3644
String urlToString = url.toString().replace(FileCompiler.CLASS_SUFFIX, "").replace(FileCompiler.JAVA_SUFFIX, "");
@@ -69,26 +77,26 @@ public Class<?> loadClass(String className, CompilationListener listener){
6977
boolean isAnonymous = absolutePath.contains("$");
7078
if(fileMonitor.isFileModifiedSinceLastPoll(sourceFile.getAbsolutePath(), sourceFile.lastModified())){
7179
if(!isAnonymous){
72-
compile(className, fileName, sourceFile, listener);
80+
compile(className, fileName, sourceFile, timeout, listener);
7381
}
7482
}
7583
return loadClass(classFile.toURI().toURL(), className);
7684
}
7785
try{
7886
return super.loadClass(className);
7987
}catch(ClassNotFoundException x){
80-
compile(className, fileName, sourceFile, listener);
88+
compile(className, fileName, sourceFile, timeout, listener);
8189
classFile = new File(fileName);
8290
return loadClass(classFile.toURI().toURL(), className);
8391
}
8492
} catch (Exception e) {
8593
throw new RuntimeException(e);
8694
}
8795
}
88-
89-
private void compile(String className, String fileName, File sourceFile, CompilationListener listener)
96+
97+
private void compile(String className, String fileName, File sourceFile, long timeout, CompilationListener listener)
9098
throws IOException {
91-
FileCompiler.compile(sourceFile, new File(binDir), listener, classPath);
99+
FileCompiler.compile(sourceFile, new File(binDir), listener, timeout, classPath);
92100
fileMonitor.updateFileSaveTime(sourceFile);
93101
}
94102

@@ -120,6 +128,7 @@ public Class<?> loadClass(URL url, String className){
120128

121129
@Override
122130
public DynamicClassLoader clone(){
123-
return new DynamicClassLoader(binDir, sourceDir, classPath, fileMonitor);
131+
return new DynamicClassLoader(binDir, sourceDir, classPath, fileMonitor,
132+
getClass().getClassLoader(), timeout);
124133
}
125134
}

‎lib/file-compiler/src/com/sandwich/util/io/FileCompiler.java‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,22 @@ public static void compile(ErrorPresenter errorPresenter, File src, File bin,
2626
compile(src, bin, new CompilationFailureLogger(errorPresenter), classpath);
2727
}
2828

29-
public static void compile(File src, final File bin, CompilationListener listener, String[] classpath) throws IOException {
29+
public static void compile(File src, final File bin,
30+
CompilationListener listener, String[] classpath) throws IOException {
31+
compile(src, bin, listener, 5000l, classpath);
32+
}
33+
34+
public static void compile(File src, File bin,
35+
CompilationListener listener, long timeout, String[] classpath) throws IOException {
3036
if(!bin.exists()){
3137
if(!bin.mkdir()){
3238
System.err.println("Was unable to create: "+bin);
3339
System.exit(-231);
3440
}
3541
}
36-
FileUtils.forEachFile(src, bin, new FileCompilerAction(bin, listener, classpath));
42+
FileUtils.forEachFile(src, bin, new FileCompilerAction(bin, listener, timeout, classpath));
3743
}
38-
44+
3945
public static String getContentsOfJavaFile(String sourceDir, String className) {
4046
return FileUtils.readFileAsString(getSourceFileFromClass(sourceDir, className));
4147
}

‎lib/file-compiler/src/com/sandwich/util/io/FileCompilerAction.java‎

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,35 @@
22

33
import java.io.File;
44
import java.io.IOException;
5+
import java.util.concurrent.Callable;
6+
import java.util.concurrent.ExecutorService;
7+
import java.util.concurrent.Executors;
8+
import java.util.concurrent.Future;
9+
import java.util.concurrent.TimeUnit;
10+
11+
import com.sandwich.util.ExceptionUtils;
512

613
class FileCompilerAction implements FileAction {
714

815
private final String destinationPath;
916
private final String[] classPaths;
10-
private CompilationListener compilationListner;
17+
private CompilationListener compilationListener;
18+
private final long timeout;
1119
static final CompilationListener LOGGING_HANDLER = new CompilationFailureLogger();
1220

1321
public FileCompilerAction(File destinationPath, CompilationListener errorHandler, String...classPaths){
22+
this(destinationPath, errorHandler, 1000, classPaths);
23+
}
24+
25+
public FileCompilerAction(File destinationPath,
26+
CompilationListener errorHandler, long timeout, String[] classPaths) {
1427
if(destinationPath == null){
1528
throw new IllegalArgumentException("the destination path is required");
1629
}
1730
this.destinationPath = destinationPath.getAbsolutePath();
1831
this.classPaths = classPaths;
19-
this.compilationListner = errorHandler == null ? LOGGING_HANDLER : errorHandler;
32+
this.compilationListener = errorHandler == null ? LOGGING_HANDLER : errorHandler;
33+
this.timeout = timeout;
2034
}
2135

2236
public void sourceToDestination(File src, File bin) throws IOException {
@@ -25,19 +39,49 @@ public void sourceToDestination(File src, File bin) throws IOException {
2539
String[] command = constructJavaCompilationCommand(src);
2640
Process p = Runtime.getRuntime().exec(command);
2741
try {
28-
if (p.waitFor() != 0) {
29-
compilationListner.compilationFailed(src, command, p.exitValue(), StreamUtils.convertStreamToString(p.getErrorStream()), null);
30-
}else{
31-
compilationListner.compilationSucceeded(src, command, StreamUtils.convertStreamToString(p.getInputStream()), null);
32-
}
42+
executeWithTimeout(src, command, p, timeout);
43+
} catch (IllegalThreadStateException x) {
44+
compilationListener.compilationFailed(src, command,
45+
255, "Compilation took longer than "+timeout+" ms.\n" +
46+
"It is likely that the compiler has locked up compiling this file.\n" +
47+
"Please revert your last change and try something different.", x);
3348
} catch (Exception x) {
3449
x.printStackTrace();
35-
compilationListner.compilationFailed(src, command,
50+
compilationListener.compilationFailed(src, command,
3651
p.exitValue(), StreamUtils.convertStreamToString(p.getErrorStream()), x);
3752
}
3853
}
3954
}
4055

56+
private void executeWithTimeout(final File src, final String[] command,
57+
final Process p, final long timeout) throws InterruptedException {
58+
ExecutorService executor = Executors.newCachedThreadPool();
59+
Callable<Object> task = new Callable<Object>() {
60+
public Object call() {
61+
try {
62+
if (p.waitFor() != 0) {
63+
compilationListener.compilationFailed(src, command, p
64+
.exitValue(), StreamUtils.convertStreamToString(p.getErrorStream()), null);
65+
} else {
66+
compilationListener.compilationSucceeded(src, command,
67+
StreamUtils.convertStreamToString(p.getInputStream()), null);
68+
}
69+
} catch (InterruptedException e) {
70+
compilationListener.compilationFailed(src, command, p.exitValue(),
71+
ExceptionUtils.convertToPopulatedStackTraceString(e), null);
72+
}
73+
return null;
74+
}
75+
};
76+
Future<Object> future = executor.submit(task);
77+
try {
78+
future.get(timeout, TimeUnit.MILLISECONDS);
79+
} catch (Exception e) {
80+
compilationListener.compilationFailed(src, command, p.exitValue(),
81+
ExceptionUtils.convertToPopulatedStackTraceString(e), null);
82+
}
83+
}
84+
4185
private String[] constructJavaCompilationCommand(File src) {
4286
return copy(new String[]{"javac", "-d", destinationPath}, getClasspath(), new String[]{src.getAbsolutePath()});
4387
}

‎lib/koans-lib/.classpath‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
<classpath>
33
<classpathentry kind="src" path="src"/>
44
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
5+
<classpathentry combineaccessrules="false" kind="src" path="/util"/>
56
<classpathentry combineaccessrules="false" kind="src" path="/file-compiler"/>
67
<classpathentry combineaccessrules="false" kind="src" path="/file-monitor"/>
7-
<classpathentry combineaccessrules="false" kind="src" path="/util"/>
88
<classpathentry kind="output" path="bin"/>
99
</classpath>

0 commit comments

Comments
 (0)