Skip to content

Application hangs when separate thread tries to read field of main application #17197

Open
@myazinn

Description

@myazinn

Compiler version

3.2.2, 3.3.0-RC3

Minimized code

https://scastie.scala-lang.org/YXhWrechQJGiJbcu9DoOkg

object ThreadStartApp extends App {
  def method(arg: Any) = {
    println(s"start $arg")
    while (true) {
      println(s"loop $arg")
      Thread.sleep(1000)
    }
  }

  val arg = "arg"
  val withArg = new Thread(() => {
    println("init arg")
    method(arg)
  })
  withArg.start()

  Thread.sleep(10000)
  withArg.interrupt()
}

Output

init arg
start arg
loop arg

Expectation

Expected output:

init arg
start arg
loop arg
loop arg
loop arg
loop arg
loop arg
loop arg
loop arg
loop arg
loop arg
loop arg

Works fine if I just inline variable
https://scastie.scala-lang.org/eLQ91hqkTtipAz8gVyGYHA
Or make it a local variable
https://scastie.scala-lang.org/MgYjQzRQTUSbHxljDewSdg
Also works fine in Scala 2.13.10
https://scastie.scala-lang.org/SlPxWnelT4uUioUr0l9gDQ

Looks like Thread.sleep(10000) somehow is locking an arg field, since once the main thread is awake, the app manages to start execution of method method but it get interrupted immediately.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions