Skip to content

Commit

Permalink
Fix resource management in ModuleCache (#3367)
Browse files Browse the repository at this point in the history
PR fixes an issue when compiler crashes while trying to load an incompatible `IR` cache.
  • Loading branch information
4e6 committed Mar 29, 2022
1 parent 7152e0d commit 435d7bb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,14 @@
- [Upgraded to Graal VM 21.3.0][3258]
- [Added the ability to decorate values with warnings.][3248]
- [Fixed issues related to constructors' default arguments][3330]
- [Fixed compiler issue related to module cache.][3367]

[3227]: https://github.com/enso-org/enso/pull/3227
[3248]: https://github.com/enso-org/enso/pull/3248
[3258]: https://github.com/enso-org/enso/pull/3258
[3330]: https://github.com/enso-org/enso/pull/3330
[3360]: https://github.com/enso-org/enso/pull/3360
[3367]: https://github.com/enso-org/enso/pull/3367

# Enso 2.0.0-alpha.18 (2021-10-12)

Expand Down
40 changes: 24 additions & 16 deletions engine/runtime/src/main/scala/org/enso/compiler/ModuleCache.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package org.enso.compiler

import java.io._
import java.nio.charset.{Charset, StandardCharsets}
import java.nio.file._
import java.util.logging.Level

import buildinfo.Info
import com.oracle.truffle.api.source.Source
import com.oracle.truffle.api.{TruffleFile, TruffleLogger}
Expand All @@ -14,12 +19,8 @@ import org.enso.interpreter.runtime.builtin.Builtins
import org.enso.interpreter.runtime.{Context, Module}
import org.enso.logger.masking.MaskedPath

import java.io._
import java.nio.charset.{Charset, StandardCharsets}
import java.nio.file._
import java.util.logging.Level
import scala.jdk.OptionConverters._
import scala.util.Using
import scala.util.{Failure, Success, Using}

// TODO Once #1971 is fixed, the logging statements should go back to using our
// normal templating syntax.
Expand Down Expand Up @@ -136,10 +137,11 @@ class ModuleCache(private val module: Module) {
)(implicit logger: TruffleLogger): Boolean = {
if (ensureRoot(cacheRoot)) {
val byteStream: ByteArrayOutputStream = new ByteArrayOutputStream()
val stream: ObjectOutputStream = new ObjectOutputStream(byteStream)
stream.writeObject(module.module)
val bytesToWrite = byteStream.toByteArray
stream.close()
val bytesToWrite =
Using.resource(new ObjectOutputStream(byteStream)) { stream =>
stream.writeObject(module.module)
byteStream.toByteArray
}

val blobDigest = computeDigest(bytesToWrite)
val sourceDigest = computeSourceDigest(module.source)
Expand Down Expand Up @@ -224,27 +226,33 @@ class ModuleCache(private val module: Module) {
Module.CompilationStage.valueOf(meta.compilationStage)

if (sourceDigestValid && blobDigestValid) {
val bais: ByteArrayInputStream = new ByteArrayInputStream(blobBytes)
val ois: ObjectInputStream = new ObjectInputStream(bais)

val readObject = ois.readObject()
ois.close()
val readObject =
Using(new ObjectInputStream(new ByteArrayInputStream(blobBytes))) {
_.readObject()
}

readObject match {
case mod: IR.Module =>
case Success(mod: IR.Module) =>
Some(
ModuleCache.CachedModule(
mod,
compilationStage,
module.getSource
)
)
case _ =>
case Success(_) =>
logger.log(
ModuleCache.logLevel,
s"Module `${module.getName.toString}` was corrupt on disk."
)
None
case Failure(ex) =>
logger.log(
ModuleCache.logLevel,
s"Module `${module.getName.toString}` failed to load " +
s"(caused by: ${ex.getMessage})."
)
None
}
} else {
logger.log(
Expand Down

0 comments on commit 435d7bb

Please sign in to comment.