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
"Deadlock" in initialization with package objects and companion objects #7646
Comments
Imported From: https://issues.scala-lang.org/browse/SI-7646?orig=1
|
@retronym said (edited on Sep 4, 2013 9:20:36 PM UTC): A Scala module
Generates:
If objects form a cycle, those class initialization locks can deadlock:
Here's a stack overflow post about this topic [2]. Similar problems are possible with lazy vals; we have a bit more of an idea how to reduce those, which we've outlined in the Lazy Val SIP [3], which is planned for 2.12. But I don't know of any proposals to change the encoding of modules. A workaround is to eagerly force objects during single threaded application startup to break the cycle. Or avoid cycles between objects. I should mention that this is not specific to package objects; as far as the generated bytecode is concerned, they are no different from other Scala objects. So I have to close this one as not-a-bug. I wish we could do more. [1] http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-5.html |
@gkossakowski said:
See Scala Specification (5.4). One could wish we would just throw an exception at runtime in case of cyclic dependencies between objects or lazy vals. I believe we could do that but without invokedynamic cycle detection would be probably rather expensive. |
Richard Bradley (richard.bradley) said (edited on Dec 19, 2014 10:14:09 AM UTC): More on that here: http://stackoverflow.com/questions/27549671/how-to-diagnose-or-detect-deadlocks-in-java-static-initializers |
I seem to be able to create some kind of "deadlock" by running a test (declared in
Main.scala
). The deadlock is caused by separate threads which are accessinga.b.c.Oxbow$class.$init$
- the first because of a call to a method brought into scope via animport a.b.c.Oxbow._
and the second byimport a.b.c._
. If you run the test a few times, eventually you'll see the "deadlock" - I'm left with two threads which are doing this:I'm calling it a "deadlock" (i.e. in moderately ironic quotation marks) because, according to JConsole, there is no deadlock and according to the stacks both threads are in
RUNNABLE
state and are notWAITING
at all (could they have been woken up by a call to notify?) - but they don't appear to be doing anything. You can leave it for 10 minutes and the threads will be in the same stateIf I change the code so that both
Test1
andTest2
doimport a.b.c._
then the issue is not observedThe files are organized as follows:
You need to run
Main.scala
a few times to reveal the deadlock. I'm fairly confident that this must be a bug in scalac, rather than a trait initialization order problem on my side (although I could be wrong)The text was updated successfully, but these errors were encountered: