Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix resource management in ModuleCache #3367

Merged
merged 3 commits into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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