Skip to content

feat: add manage_lifespan flag to setup_di - #40

Merged
lesnik512 merged 1 commit into
mainfrom
manage-lifespan-flag
Oct 17, 2026
Merged

lesnik512 merged 1 commit into
mainfrom
manage-lifespan-flag

Conversation

@lesnik512

@lesnik512 lesnik512 commented Oct 17, 2026 •

Copy link
Copy Markdown
Member

Closes #39

setup_di(broker, container) gains a keyword-only manage_lifespan: bool = True, matching modern-di-fastmcp's flag of the same name.

With the default, nothing changes: WORKER_STARTUP opens the root container and WORKER_SHUTDOWN closes it. With manage_lifespan=False, setup_di skips registering those two handlers. It still attaches the container to broker.state and registers taskiq_message_provider, and each task still builds and closes its own child container. The caller owns the root, for example with async with container: around the apps that share it.

Skipping the handler registration leaves the default path unchanged, and the flag adds no work per worker cycle.

Docs:

  • README: the flag in the API table and a short "Sharing a container" section.
  • CONTEXT.md: the "Root container" entry no longer says the worker events always open and close it.

Tests in tests/test_lifespan.py, all with manage_lifespan=False:

  • a full worker cycle with one task leaves the root open and its APP finalizer unrun
  • an APP- and a REQUEST-scoped dependency resolve, and the REQUEST finalizer runs when the task ends
  • with the caller owning the root in async with, the APP finalizer runs once, at the caller's exit

With the flag accepted but ignored, the three tests fail on their assertions. The existing lifespan tests cover the default and are unchanged.

@lesnik512
lesnik512 merged commit 8b7958f into main Oct 17, 2026
12 checks passed
@lesnik512
lesnik512 deleted the manage-lifespan-flag branch October 17, 2026 12:56
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

Successfully merging this pull request may close these issues.

Add manage_lifespan=False so the user can own a shared root container

1 participant