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 #20856: Serialize Waiting and Evaluating as if null. #21243

Merged
merged 1 commit into from
Jul 31, 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
20 changes: 18 additions & 2 deletions library/src/scala/runtime/LazyVals.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,29 @@ object LazyVals {
* Used to indicate the state of a lazy val that is being
* evaluated and of which other threads await the result.
*/
final class Waiting extends CountDownLatch(1) with LazyValControlState
final class Waiting extends CountDownLatch(1) with LazyValControlState {
/* #20856 If not fully evaluated yet, serialize as if not-evaluat*ing* yet.
* This strategy ensures the "serializability" condition of parallel
* programs--not to be confused with the data being `java.io.Serializable`.
* Indeed, if thread A is evaluating the lazy val while thread B attempts
* to serialize its owner object, there is also an alternative schedule
* where thread B serializes the owner object *before* A starts evaluating
* the lazy val. Therefore, forcing B to see the non-evaluating state is
* correct.
*/
private def writeReplace(): Any = null
}

/**
* Used to indicate the state of a lazy val that is currently being
* evaluated with no other thread awaiting its result.
*/
object Evaluating extends LazyValControlState
object Evaluating extends LazyValControlState {
/* #20856 If not fully evaluated yet, serialize as if not-evaluat*ing* yet.
* See longer comment in `Waiting.writeReplace()`.
*/
private def writeReplace(): Any = null
}

/**
* Used to indicate the state of a lazy val that has been evaluated to
Expand Down
1 change: 1 addition & 0 deletions tests/run/i20856.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
succeeded: BOMB: test
70 changes: 70 additions & 0 deletions tests/run/i20856.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// scalajs: --skip

import java.io.*

class Message(content: String) extends Serializable:
//@transient
lazy val bomb: String =
Thread.sleep(200)
"BOMB: " + content
end Message

object Test:
def serialize(obj: Message): Array[Byte] =
val byteStream = ByteArrayOutputStream()
val objectStream = ObjectOutputStream(byteStream)
try
objectStream.writeObject(obj)
byteStream.toByteArray
finally
objectStream.close()
byteStream.close()
end serialize

def deserialize(bytes: Array[Byte]): Message =
val byteStream = ByteArrayInputStream(bytes)
val objectStream = ObjectInputStream(byteStream)
try
objectStream.readObject().asInstanceOf[Message]
finally
objectStream.close()
byteStream.close()
end deserialize

def main(args: Array[String]): Unit =
val bytes =
val msg = Message("test")

val touch = Thread(() => {
msg.bomb // start evaluation before serialization
()
})
touch.start()

Thread.sleep(50) // give some time for the fork to start lazy val rhs eval

serialize(msg) // serialize in the meantime so that we capture Waiting state
end bytes

val deserializedMsg = deserialize(bytes)

@volatile var msg = ""
@volatile var started = false
val read = Thread(() => {
started = true
msg = deserializedMsg.bomb
()
})
read.start()

Thread.sleep(1000)
if !started then
throw Exception("ouch, the thread has not started yet after 1s")

if !msg.isEmpty() then
println(s"succeeded: $msg")
else
read.interrupt()
throw new AssertionError("failed to read bomb in 1s!")
end main
end Test
Loading