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 occasional failed imports due to race conditions #270

Merged
merged 2 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 11 additions & 2 deletions jvm/src/main/scala/io/kaitai/struct/formats/JavaClassSpecs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,24 @@ import java.io.{File, FileNotFoundException}
import scala.collection.mutable
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import scala.collection.concurrent
import java.util.concurrent.ConcurrentHashMap
import scala.jdk.CollectionConverters._

/**
* Java implementation of ClassSpec container, doing imports from local files.
*/
class JavaClassSpecs(relPath: String, absPaths: Seq[String], firstSpec: ClassSpec)
extends ClassSpecs(firstSpec) {

private val relFiles = mutable.Map[String, ClassSpec]()
private val absFiles = mutable.Map[String, ClassSpec]()
// We're using thread-safe `ConcurrentHashMap` for `relFiles` and `absFiles`,
// because these hash maps may be mutated concurrently by multiple threads in
// `JavaClassSpecs.cached()`. Using a non-thread-safe hash map here could
// occasionally cause `cacheMap.get(name)` in `JavaClassSpecs.cached()` to
// fail internally and throw an `ArrayIndexOutOfBoundsException`, see
// https://github.com/kaitai-io/kaitai_struct/issues/951
private val relFiles: concurrent.Map[String, ClassSpec] = new ConcurrentHashMap[String, ClassSpec]().asScala
private val absFiles: concurrent.Map[String, ClassSpec] = new ConcurrentHashMap[String, ClassSpec]().asScala

override def importRelative(name: String, path: List[String], inFile: Option[String]): Future[Option[ClassSpec]] = Future {
Log.importOps.info(() => s".. importing relative $name")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,23 @@ class LoadImports(specs: ClassSpecs) {
//
// In theory, duplicate imports shouldn't be returned at all by
// import* methods due to caching, but we won't rely on it here.
if (!specs.contains(specName)) {
specs(specName) = spec
//
// The `synchronized` block is necessary because this code is run
// concurrently by multiple threads (each resolving different imports)
// and `specs` is a shared non-thread-safe `HashMap`. Without this
// synchronization, a few imports were occasionally missing from
// `specs` due to a race condition, and even (though rarely) the
// implementation of `specs.contains()` could fail internally with an
// `ArrayIndexOutOfBoundsException`. For more details, see
// https://github.com/kaitai-io/kaitai_struct/issues/951
val isNewSpec = specs.synchronized {
val isNew = !specs.contains(specName)
if (isNew) {
specs(specName) = spec
}
isNew
}
if (isNewSpec) {
GreyCat marked this conversation as resolved.
Show resolved Hide resolved
processClass(spec, ImportPath.updateWorkDir(workDir, impPath))
} else {
Log.importOps.warn(() => s"... we have that already, ignoring")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class ResolveTypes(specs: ClassSpecs, topClass: ClassSpec, opaqueTypes: Boolean)
(Some(ClassSpec.opaquePlaceholder(typeName)), None)
} else {
// Opaque types are disabled => that is an error
Log.typeResolve.info(() => " => ??? (opaque type are disabled => error)")
(None, Some(TypeNotFoundErr(typeName, curClass, path)))
}
case Some(x) =>
Expand Down
Loading