Skip to content

feat: add manage_lifespan flag to setup_di - #46

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

lesnik512 merged 1 commit into
mainfrom
feat/manage-lifespan

Conversation

@lesnik512

Copy link
Copy Markdown
Member

Closes #45

setup_di(app, container) takes a keyword-only manage_lifespan: bool = True, with the same name and default as modern-di-fastmcp. The default changes nothing.

With manage_lifespan=False the four worker signal handlers skip open() and close_sync(). Everything else still runs: the worker_process_init handler still binds the container to a spawn child's app, each handler still acts only for the app that owns the container, and @inject still builds, opens and closes a child container per task call. The user owns the root container, for example with async with container: around running the apps.

The check sits in two small helpers, _open_for and _close_for, so the binding call in the process-init handler runs before the flag is consulted.

README: the flag is in the API table, and a new "Sharing a container" section shows the user owning the root. It also says that a prefork child inherits the open container and nothing closes it there with the flag off, so the user closes it from their own worker_process_shutdown handler if the child's APP finalizers must run. CONTEXT.md: the root container entry notes that the flag hands opening and closing to the user.

Tests, each written to fail before the change:

  • a full worker lifecycle (both signal pairs and one task) leaves the root open with its APP finalizer unrun, while the task's REQUEST-scoped dependency resolves and its finalizer runs
  • the init signals do not reopen a closed root
  • with the user owning the root in async with, the APP finalizer runs once, at the user's exit
  • a spawn child still binds its container to the current app and leaves it closed

Existing tests for the default are unchanged.

@lesnik512
lesnik512 merged commit 2a578d1 into main Oct 17, 2026
12 checks passed
@lesnik512
lesnik512 deleted the feat/manage-lifespan 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