Skip to content

Commit 5628641

Browse files
committed
Better handling of ctrl-c while the compiler is running
Previously, the compiler tried its best to continue working after ctrl-c, which usually resulted a flood of error messages. This commit takes inspiration from scala/scala#6479 to handle ClosedByInterruptException and InterruptedException gracefully and avoid this. Note that the Scala 2 PR goes further: it checks `Thread.interrupted()` to set a global `cancelled` flag (Should we do the same with our `Run#isCancelled` ?) and it also catches InterruptedException at the top-level so that ctrl-c does not end up printing a stacktrace, but this is beyond what I have the time to look at currently.
1 parent fd18546 commit 5628641

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

compiler/src/dotty/tools/backend/jvm/BytecodeWriters.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package backend
33
package jvm
44

55
import java.io.{ DataOutputStream, FileOutputStream, IOException, OutputStream, File => JFile }
6+
import java.nio.channels.ClosedByInterruptException
7+
import java.nio.file.Files
68
import dotty.tools.io._
79
import dotty.tools.dotc.report
810

@@ -112,6 +114,15 @@ trait BytecodeWriters {
112114
val outstream = new DataOutputStream(outfile.bufferedOutput)
113115

114116
try outstream.write(jclassBytes, 0, jclassBytes.length)
117+
catch {
118+
case ex: ClosedByInterruptException =>
119+
try {
120+
outfile.delete() // don't leave a empty of half-written classfile around after an interrupt
121+
} catch {
122+
case _: Throwable =>
123+
}
124+
throw ex
125+
}
115126
finally outstream.close()
116127
report.informProgress("wrote '" + label + "' to " + outfile)
117128
}

compiler/src/dotty/tools/backend/jvm/GenBCode.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ class GenBCodePipeline(val int: DottyBackendInterface)(using Context) extends BC
190190
else {
191191
try { /*withCurrentUnit(item.cunit)*/(visit(item)) }
192192
catch {
193+
case ex: InterruptedException =>
194+
throw ex
193195
case ex: Throwable =>
194196
println(s"Error while emitting ${item.cunit.source.file.name}")
195197
throw ex
@@ -424,6 +426,8 @@ class GenBCodePipeline(val int: DottyBackendInterface)(using Context) extends BC
424426
addLambdaDeserialize(plainNode, serializableLambdas)
425427
addToQ3(item)
426428
} catch {
429+
case ex: InterruptedException =>
430+
throw ex
427431
case ex: Throwable =>
428432
println(s"Error while emitting ${item.plain.classNode.name}")
429433
throw ex

compiler/src/dotty/tools/dotc/core/SymbolLoaders.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package dotc
33
package core
44

55
import java.io.{IOException, File}
6+
import java.nio.channels.ClosedByInterruptException
67
import scala.compat.Platform.currentTime
78
import dotty.tools.io.{ ClassPath, ClassRepresentation, AbstractFile }
89
import config.Config
@@ -343,6 +344,10 @@ abstract class SymbolLoader extends LazyType { self =>
343344
report.informTime("loaded " + description, start)
344345
}
345346
catch {
347+
case ex: InterruptedException =>
348+
throw ex
349+
case ex: ClosedByInterruptException =>
350+
throw new InterruptedException
346351
case ex: IOException =>
347352
signalError(ex)
348353
case NonFatal(ex: TypeError) =>

0 commit comments

Comments
 (0)