xiaodududu / google-guice

Automatically exported from code.google.com/p/google-guice
Apache License 2.0
0 stars 0 forks source link

Exceptions when using circular dependency in multi-threaded code #550

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I am using circular dependency in two object's constructors. The two objects 
are defined as singletons, and are being created in multi-threaded code. I'm 
getting the following exception:

java.lang.IllegalStateException: This is a proxy used to support circular 
references involving constructors. The object we're proxying is not constructed 
yet. Please wait until after injection has completed to use this object.
    at com.google.inject.internal.ConstructionContext$DelegatingInvocationHandler.invoke(ConstructionContext.java:100)
    at $Proxy6.whoAmI(Unknown Source)
    at com.delver.guice.GuiceTest.testIt(GuiceTest.java:65)
    at com.delver.guice.GuiceTest.access$000(GuiceTest.java:21)
    at com.delver.guice.GuiceTest$2.call(GuiceTest.java:44)
    at com.delver.guice.GuiceTest$2.call(GuiceTest.java:39)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:619)

Attached is a test that reproduces the problem.

Original issue reported on code.google.com by ron.gr...@gmail.com on 7 Oct 2010 at 9:44

Attachments:

GoogleCodeExporter commented 9 years ago
(Using Guice version 2.0)

Original comment by ron.gr...@gmail.com on 7 Oct 2010 at 9:47

GoogleCodeExporter commented 9 years ago
A few workarounds that I've found:

1. Using a Provider instead of Guice's Proxy seems to resolve the issue.
2. When using asEagerSingleton the problem does not reproduce.

Is there a way to disable the proxy generation for circular dependencies (make 
it thorw an exception)? I would rather always use a Provider instead of a proxy 
until this is fixed, and would like an easy way to catch all these cases where 
my code has circular dependencies.

Original comment by ron.gr...@gmail.com on 7 Oct 2010 at 9:56

GoogleCodeExporter commented 9 years ago
Also - the problem does not reproduce if I don't bind the objects as singletons.

Original comment by ron.gr...@gmail.com on 7 Oct 2010 at 11:51

GoogleCodeExporter commented 9 years ago
Re: Is there a way to disable the proxy generation for circular dependencies 
(make it throw an exception)?

Yes, but it requires Guice 3.0 (or more specifically, the code in SVN since 3.0 
is not released yet).  Use InjectorBuilder.disableCircularProxies.

Original comment by sberlin on 7 Oct 2010 at 12:21

GoogleCodeExporter commented 9 years ago
I actually tried that, but I see that some tests are failing in trunk, and also 
had problems compiling my code against Guice 3.0. For now I resorted to making 
the classes non-singletons.

Original comment by ron.gr...@gmail.com on 7 Oct 2010 at 1:05

GoogleCodeExporter commented 9 years ago
The AllTests target suppresses some tests that are known to fail (they're 
written against expectations that Guice doesn't fulfill yet).  

What issues are you seeing compiling against 3.0?  It should be a trivial swap.

Original comment by sberlin on 7 Oct 2010 at 1:43