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

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

Closed
gissuebot opened this issue Jul 7, 2014 · 9 comments
Closed

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

gissuebot opened this issue Jul 7, 2014 · 9 comments
Labels

Comments

@gissuebot
Copy link

From ron.gross on October 07, 2010 05:44:00

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.

Binary attachments: multi-threaded-circular-dependency.zip

Original issue: http://code.google.com/p/google-guice/issues/detail?id=550

@gissuebot
Copy link
Author

From ron.gross on October 07, 2010 02:47:36

(Using Guice version 2.0)

@gissuebot
Copy link
Author

From ron.gross on October 07, 2010 02:56:00

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.

@gissuebot
Copy link
Author

From ron.gross on October 07, 2010 04:51:41

Also - the problem does not reproduce if I don't bind the objects as singletons.

@gissuebot
Copy link
Author

From sberlin on October 07, 2010 05:21:12

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.

@gissuebot
Copy link
Author

From ron.gross on October 07, 2010 06:05:03

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.

@gissuebot
Copy link
Author

From sberlin on October 07, 2010 06:43:15

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.

@lchiriac
Copy link

This issue seems over 5 yrs old. Was there a solution, is there a new version of Guice that has adressed this ?

@lchiriac
Copy link

We get this issue at runtime not in compilation.

@sameb
Copy link
Member

sameb commented Apr 12, 2016

The exception is expected. If you have circular dependencies and use the circular dependencies before they're initialized, you will get an exception. You can use Binder.disableCircularProxies to disable the circular proxy feature of Guice. This was released in Guice 3.0, ~5 years ago.

@sameb sameb closed this as completed Apr 12, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants