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 race condition in new LazyVals #16975

Merged
merged 5 commits into from
Feb 27, 2023
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
3 changes: 2 additions & 1 deletion library/src/scala/runtime/LazyVals.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ object LazyVals {

/* ------------- Start of public API ------------- */

sealed trait LazyValControlState
// This trait extends Serializable to fix #16806 that caused a race condition
sealed trait LazyValControlState extends Serializable

/**
* Used to indicate the state of a lazy val that is being
Expand Down
5 changes: 5 additions & 0 deletions project/MiMaFilters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ object MiMaFilters {
ProblemFilters.exclude[MissingFieldProblem]("scala.runtime.stdLibPatches.language#experimental.into"),
ProblemFilters.exclude[MissingClassProblem]("scala.runtime.stdLibPatches.language$experimental$into$"),
// end of New experimental features in 3.3.X

// Added java.io.Serializable as LazyValControlState supertype
ProblemFilters.exclude[MissingTypesProblem]("scala.runtime.LazyVals$LazyValControlState"),
ProblemFilters.exclude[MissingTypesProblem]("scala.runtime.LazyVals$Waiting"),

)
val TastyCore: Seq[ProblemFilter] = Seq(
ProblemFilters.exclude[DirectMissingMethodProblem]("dotty.tools.tasty.TastyBuffer.reset"),
Expand Down
2 changes: 2 additions & 0 deletions tests/run/i16806.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Success
Success
42 changes: 42 additions & 0 deletions tests/run/i16806.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//scalajs: --skip
import java.util.concurrent.Semaphore

object Repro {

case object DFBit
final class DFError extends Exception("")
final class DFType[+T](val value: T | DFError) extends AnyVal

def asIR(dfType: DFType[DFBit.type]): DFBit.type = dfType.value match
case dfTypeIR: DFBit.type => dfTypeIR
case err: DFError => throw new DFError

object Holder {
val s = new Semaphore(1, false)
final lazy val Bit = {
s.release()
new DFType[DFBit.type](DFBit)
}
}

@main
def Test =
val a = new Thread() {
override def run(): Unit =
Holder.s.acquire()
val x = Holder.Bit.value
assert(x.isInstanceOf[DFBit.type])
println("Success")
}
val b = new Thread() {
override def run(): Unit =
Holder.s.acquire()
val x = Holder.Bit.value
assert(x.isInstanceOf[DFBit.type])
println("Success")
}
a.start()
b.start()
a.join(300)
b.join(300)
}