Skip to content

Commit

Permalink
Dump sibblings when a file cannot be executed
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslavTulach committed Jan 24, 2025
1 parent 83312e1 commit 2a91bb9
Showing 1 changed file with 73 additions and 44 deletions.
117 changes: 73 additions & 44 deletions project/DistributionPackage.scala
Original file line number Diff line number Diff line change
Expand Up @@ -241,59 +241,88 @@ object DistributionPackage {
) { diff =>
if (diff.modified.nonEmpty) {
log.info(s"Generating index for $libName ")
val pathToExecute: String =
Platform.executableFile(ensoExecutable.getAbsoluteFile)

def assertExecutable(when: String) = {
val fileToExecute: File = new File(pathToExecute)
if (!fileToExecute.canExecute()) {
log.warn(s"Not an executable file ${fileToExecute} $when")
var dir = fileToExecute
while (dir != null && !dir.exists()) {
dir = dir.getParentFile
}
var count = 0
if (dir != null) {
log.warn(s"Content of ${dir}")
Option(dir.listFiles).map(_.map { file =>
log.warn(s" ${file}")
count += 1
})
}
log.warn(s"Found ${count} files.")
}
}
assertExecutable("before launching")
val command = Seq(
Platform.executableFile(ensoExecutable.getAbsoluteFile),
pathToExecute,
"--no-compile-dependencies",
"--no-global-cache",
"--compile",
path.getAbsolutePath
)
log.debug(command.mkString(" "))
val runningProcess = Process(
command,
Some(path.getAbsoluteFile.getParentFile),
"JAVA_OPTS" -> "-Dorg.jline.terminal.dumb=true"
).run
// Poor man's solution to stuck index generation
val GENERATING_INDEX_TIMEOUT = 60 * 2 // 2 minutes
var current = 0
var timeout = false
while (runningProcess.isAlive() && !timeout) {
if (current > GENERATING_INDEX_TIMEOUT) {
java.lang.System.err
.println("Reached timeout when generating index. Terminating...")
try {
val pidOfProcess = pid(runningProcess)
val javaHome = System.getProperty("java.home")
val jstack =
if (javaHome == null) "jstack"
else
Paths.get(javaHome, "bin", "jstack").toAbsolutePath.toString
val in = java.lang.Runtime.getRuntime
.exec(Array(jstack, pidOfProcess.toString))
.getInputStream

System.err.println(IOUtils.toString(in, "UTF-8"))
} catch {
case e: Throwable =>
java.lang.System.err
.println("Failed to get threaddump of a stuck process", e);
} finally {
timeout = true
runningProcess.destroy()
try {
val runningProcess = Process(
command,
Some(path.getAbsoluteFile.getParentFile),
"JAVA_OPTS" -> "-Dorg.jline.terminal.dumb=true"
).run
// Poor man's solution to stuck index generation
val GENERATING_INDEX_TIMEOUT = 60 * 2 // 2 minutes
var current = 0
var timeout = false
while (runningProcess.isAlive() && !timeout) {
if (current > GENERATING_INDEX_TIMEOUT) {
java.lang.System.err
.println(
"Reached timeout when generating index. Terminating..."
)
try {
val pidOfProcess = pid(runningProcess)
val javaHome = System.getProperty("java.home")
val jstack =
if (javaHome == null) "jstack"
else
Paths.get(javaHome, "bin", "jstack").toAbsolutePath.toString
val in = java.lang.Runtime.getRuntime
.exec(Array(jstack, pidOfProcess.toString))
.getInputStream

System.err.println(IOUtils.toString(in, "UTF-8"))
} catch {
case e: Throwable =>
java.lang.System.err
.println("Failed to get threaddump of a stuck process", e);
} finally {
timeout = true
runningProcess.destroy()
}
} else {
Thread.sleep(1000)
current += 1
}
} else {
Thread.sleep(1000)
current += 1
}
}
if (timeout) {
throw new RuntimeException(
s"TIMEOUT: Failed to compile $libName in $GENERATING_INDEX_TIMEOUT seconds"
)
}
if (runningProcess.exitValue() != 0) {
throw new RuntimeException(s"Cannot compile $libName.")
if (timeout) {
throw new RuntimeException(
s"TIMEOUT: Failed to compile $libName in $GENERATING_INDEX_TIMEOUT seconds"
)
}
if (runningProcess.exitValue() != 0) {
throw new RuntimeException(s"Cannot compile $libName.")
}
} finally {
assertExecutable("after execution")
}
} else {
log.debug(s"No modified files. Not generating index for $libName.")
Expand Down

0 comments on commit 2a91bb9

Please sign in to comment.