Guice causing deadlock

456 views
Skip to first unread message

med...@gmail.com

unread,
Jul 15, 2008, 5:45:54 PM7/15/08
to google-guice
OK, so it's my use of Guice that causes the deadlock, but still this
should be fixed.

Scopes.java has a synchronized(Injector.class) block which is causing
trouble. This is because on another thread,
Class.initAnnotationsIfNecessary, a synchronized method, is getting
called on the Injector class object. A more private monitor should be
used for Guice's synchronization.

limpb...@gmail.com

unread,
Jul 15, 2008, 8:00:02 PM7/15/08
to google-guice
On Jul 15, 2:45 pm, "medo...@gmail.com" <medo...@gmail.com> wrote:
> Scopes.java has a synchronized(Injector.class) block which is causing
> trouble. This is because on another thread,
> Class.initAnnotationsIfNecessary, a synchronized method, is getting
> called on the Injector class object. A more private monitor should be
> used for Guice's synchronization.

I'm confused -- if both threads are using the
same lock, how are they deadlocking?

One thing you should avoid is starting threads
within @Inject methods.

med...@gmail.com

unread,
Jul 15, 2008, 8:07:27 PM7/15/08
to google-guice
Because the monitor owner, running downstream from the sync block in
Scopes.java, is waiting on the other thread to finish. And the other
thread cannot finish because it cannot enter the sync method in Class.

You're right, though, I only found this issue because of bad design in
my code. It's not a problem for me anymore but others may run into
it, wasting their time too.

Dhanji R. Prasanna

unread,
Jul 16, 2008, 12:17:29 AM7/16/08
to google...@googlegroups.com
I have also wondered why the singleton lock is so coarse grained. It
would seem to preclude multiple independent injectors creating
singletons concurrently?
Or is that the intention?

Bob Lee

unread,
Jul 16, 2008, 4:34:27 PM7/16/08
to google...@googlegroups.com
On 7/15/08, Dhanji R. Prasanna <dha...@gmail.com> wrote:
I have also wondered why the singleton lock is so coarse grained. It
would seem to preclude multiple independent injectors creating
singletons concurrently?
Or is that the intention?

The lock is coarse so as to prevent deadlocks (imagine two different threads requesting two different but circularly dependent singletons at the same time).

If I remember correctly, it synchronizes on Injector.class instead of the Injector because:

  a) Scopes.SINGLETON doesn't have access to the Injector.

and

  b) The singleton provider can actually be shared between Injectors if you use hierarchical injectors or take a Binding from one injector and rebind it in another.

Bob

med...@gmail.com

unread,
Jul 17, 2008, 2:12:36 AM7/17/08
to google-guice
Fair enough, but I'd use a private monitor instead of
Injector.class's.

Bob Lee

unread,
Jul 17, 2008, 3:20:09 AM7/17/08
to google...@googlegroups.com
On Wed, Jul 16, 2008 at 11:12 PM, med...@gmail.com <med...@gmail.com> wrote:
Fair enough, but I'd use a private monitor instead of
Injector.class's.

Sure.

Reply all
Reply to author
Forward
0 new messages