refactor(api): rebuild main.py lifespan on AsyncExitStack - #59
Merged
Conversation
- 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 session created for this PR. Skill: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
create_app()carried an 80-line inline lifespan whose shutdown was a hand-orderedfinallyblock. This rebuilds it onAsyncExitStack:engine.dispose,clear_session_factory, container stop, replay drain, task cancellation), so the LIFO teardown order holds by construction — including when startup fails midway.create_app()is now ~35 lines of wiring.Verification
ruff check+ruff format --checkclean,mypyclean (37 files)