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

State of parent container is not preserved in the child #203

Open
michael-rubel opened this issue Jan 15, 2025 · 3 comments
Open

State of parent container is not preserved in the child #203

michael-rubel opened this issue Jan 15, 2025 · 3 comments
Labels
bug Something isn't working

Comments

@michael-rubel
Copy link

In "Scoped Container" feature, the state of previously registered objects is not preserved in the child.

Boot time (Uvicorn server start time):

class CoreServiceProvider(ServiceProvider):
    def register(self):
        self.container.register(PipeRegistry, scope=Scope.singleton)

class PipelineServiceProvider(ServiceProvider):
    def __init__(
        self,
        container: ServiceContainer,
        pipe_registry: PipeRegistry,
    ):
        super().__init__(container)

        self.pipe_registry = pipeline_registry

    def register(self):
        self.pipe_registry.register(MyFirstActionPipe)
        self.pipe_registry.register(MySecondActionPipe)

Request:

@routes.post("/stream")
async def stream(request: Request) -> EventSourceResponse:
    container: ServiceContainer = request.app.container.child() # <-- created a child here

    print(container)

    await container.resolve(RequestBootstrapper).bootstrap(request)

    ...
class RequestBootstrapper:
    def __init__(
        self,
        container: ServiceContainer,
        pipe_registry: PipeRegistry,
    ):
        self.container = container
        print(self.container)

        self.pipe_registry = pipe_registry
        print("Pipes: ", self.pipe_registry.pipes())

        ....

Object IDs are the same (result of prints):
<core.dependency_injection.container.ServiceContainer object at 0x75671915cf50>
<core.dependency_injection.container.ServiceContainer object at 0x75671915cf50>

Result of self.pipe_registry.pipes() is [] empty. Pipes are not registered, parent container's state is lost in the child.

@bobthemighty
Copy link
Owner

I don't know what any of these objects are, so it's a little hard for me to understand the problem here. You mean that the singleton dependency isn't shared between the child and parent?

@michael-rubel
Copy link
Author

Yes, singletons are completely new, so the state of the parent's objects is lost. I think sharing state once (when creating a child) makes sense because you want to have access to shared registries from the parent (from the server bootstrapping stage) while using an isolated child container inside the request.

@bobthemighty
Copy link
Owner

There's a cache for singletons, held in the container, my guess is that because the dep isn't in that cache in the child, it ends up delegating back to the parent. Clear bug, which I was semi-thinking about anyway.

@bobthemighty bobthemighty added the bug Something isn't working label Jan 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants