Skip to content

Circular dependency deadlock possibility #4003

@justiny1023

Description

@justiny1023

Is it possible for Dagger to deadlock while providing for dependencies? For example, if there is a circular dependency between Class A and Class B because both inject each other like this:

class ClassA @Inject constructor(classB: Provider<ClassB>) {
   fun logSomeMetrics() {
      // ...
      classB.get().writeSomething();
      // ...
   }
}

class ClassB @Inject constructor(classA: Provider<ClassA>) {
   fun writeSomething() {
      // ...
      classA.get().logSomeMetrics();
      // ...
   }
}

The Provider was used in each class to try to break the circular dependency. Is there ever a race condition or scenario where two threads will deadlock because one is trying to resolve dependency (lazily through get()) for Class A, while the other is trying to resolve dependency (lazily through get()) for Class B?

Or is it guaranteed that both dependencies will be resolved properly without deadlock?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions