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

Provider.instance throws a threading error #530

Open
MadsBogeskov opened this issue Mar 13, 2023 · 0 comments
Open

Provider.instance throws a threading error #530

MadsBogeskov opened this issue Mar 13, 2023 · 0 comments

Comments

@MadsBogeskov
Copy link
Contributor

MadsBogeskov commented Mar 13, 2023

In the app I am working on we often do registrations in Assemblies like so:

container.register(SomeType.self) {
    SomeType(
        dependencyOne: $0.resolve(Provider<DependencyOne>.self)!
    )
}

This is however not thread safe, so we sometimes experience that when we call instance on the Provider it will crash with a: If accessing container from multiple threads, make sure to use a synchronized resolver.. As I can read from the code this is because some other resolve might have occurred simultaneously with this, and because the Resolver isn't synchronised in the factory methods it isn't thread safe .

I have been able to fix this issue by replacing the above code with something like:

container.register(SomeType.self) {
    SomeType(
        dependencyOne: container.synchronize().resolve(Provider<DependencyOne>.self)!
    )
}

however that seems wrong, in that it feels like I am creating a cyclic dependency on the container, and also I am not sure that any registrations made after this would be part of the scope of the inlined container, which would result in invalid resolves.

Am I hitting a bug? If so I can try to see if I can fix it, or is there something I am misunderstanding, if so I can try and update the documentation so others won't hit the same issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant