Skip to content

refactor(api): rebuild main.py lifespan on AsyncExitStack - #59

Merged
mariuspruvot merged 1 commit into
mainfrom
refactor/main-lifespan
Jul 30, 2026
Merged

refactor(api): rebuild main.py lifespan on AsyncExitStack#59
mariuspruvot merged 1 commit into
mainfrom
refactor/main-lifespan

Conversation

@mariuspruvot

Copy link
Copy Markdown
Owner

What

create_app() carried an 80-line inline lifespan whose shutdown was a hand-ordered finally block. This rebuilds it on AsyncExitStack:

  • Each resource registers its cleanup at acquisition (engine.dispose, clear_session_factory, container stop, replay drain, task cancellation), so the LIFO teardown order holds by construction — including when startup fails midway.
  • The teardown sequence is unchanged: cancel periodic loops → await in-flight replay tasks → stop running containers → clear session factory registry → dispose engine.
  • Startup steps extracted into named helpers; create_app() is now ~35 lines of wiring.
  • Function-level imports hoisted to module scope (no import cycles), narration comments dropped — including a leftover internal story-spec reference.

Verification

  • Full API suite: 282/282 passed (including both dispatcher tests that are flaky in CI)
  • ruff check + ruff format --check clean, mypy clean (37 files)
  • Behavior-preserving: no test changes needed

- Extract the 80-line inline lifespan out of create_app() into a module-level _lifespan built on AsyncExitStack: each resource registers its cleanup as it is acquired, so the LIFO teardown order (cancel loops -> drain replay tasks -> stop containers -> clear factory -> dispose engine) holds by construction, including when startup fails midway
- Extract named helpers (_reconcile_stale_sessions, _stop_running_containers, _drain_replay_tasks, _cancel_task) in place of the hand-ordered finally block
- Hoist function-level imports to module scope (no cycles) and drop narration comments, including a leftover story-spec reference
- Behavior preserved: same teardown order, same exception-swallowing semantics; 282/282 tests pass, ruff and mypy clean
@helprs-prod

helprs-prod Bot commented Jul 30, 2026

Copy link
Copy Markdown

helPRs session created for this PR.

Skill: challenge-me | Open session

@mariuspruvot
mariuspruvot merged commit 2a22f83 into main Jul 30, 2026
10 checks passed
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.

1 participant