Repository navigation
fix: close the root container after every shutdown handler - #54
Merged
Merged
Conversation
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 changed
setup_diregistered the wait-and-close (close_root) ondispatcher.shutdownwhen it was called. aiogram runs shutdown handlers on the dispatcher first and then on sub-routers, so adispatcher.shutdownhandler registered aftersetup_di, and every sub-routershutdownhandler, ran against a closed root and gotContainerClosedErrorafter theAPPfinalizers had already run.The #49 in-flight wait and the root close are now two handlers,
wait_in_flightandclose_root. Shutdown runs in this order:shutdown_timeoutand its warning),APPfinalizers run last.setup_diregisters both ondispatcher.shutdown, wait first, as a fallback foremit_shutdownwithoutemit_startup. The startup handler that opens the root then:wait_in_flightfromdispatcher.shutdownand puts it back at index 0, so it runs before every dispatcher handler and therefore before every router handler;close_rootfrom the observer it is on and appends it to theshutdownobserver of the last router indispatcher.chain_tail.Removing before re-adding is the restart guard for both.
wait_in_flightonly ever lives ondispatcher.shutdown, so it is there exactly once after any number of startups.close_rootis removed from wherever the previous startup (orsetup_di) put it, so if routers were included between runs it moves to the new last router and neither runs twice nor gets skipped. Re-appending each startup also puts it behind shutdown handlers registered on the last router between runs. Without a startup, both stay wheresetup_diput them and run once each, wait then close.manage_lifespan=Falseandauto_injectare unchanged.aiogram details this relies on (3.31.0, same in the 3.2 floor)
Router.emit_shutdowntriggers its ownshutdownobserver, then callsemit_shutdownon each ofsub_routersin order: a pre-order depth-first walk. So the first handler ofdispatcher.shutdownruns before every other shutdown handler, and the last router ofchain_tail(same pre-order walk) runs its handlers last.Dispatcher.__init__registersself.fsm.closeondispatcher.shutdown, so with the wait at index 0 the FSM storage now closes after the wait.EventObserver.triggeriteratesself.handlersat shutdown time. Replacingshutdown.handlersduring startup takes effect in the same cycle and does not touch a list being iterated. Startup and shutdown observers are plainEventObservers, not part ofrouter.observers, so_inject_routerdoes not wrap either handler.parent_routercan only be set once), so the router holding the close stays reachable.Limits
setup_dirun before the wait.SimpleRequestHandler.registerputs on aiohttp shutdown can still run before the wait. The README says so.Tests
test_shutdown_handlers_run_after_in_flight_updatesputs a shutdown handler on the dispatcher or a sub-router, registered before or aftersetup_di, and over two cycles checks theAPPresource log ishandled, hook, finalized. All four cases failed on the previous commit (hookcame first).test_shutdown_waits_once_per_cycleholds an update in flight withshutdown_timeout, includes routers between runs, and checks one timeout warning per shutdown, with and without startup. With the removal of the oldwait_in_flightentry deleted, it fails.test_shutdown_handlers_see_the_open_rootputs a shutdown handler on the dispatcher, on a sub-router, or on a sub-router that has its own child, registered before or aftersetup_di. Over two startup/shutdown cycles it checks that the handler resolves theAPPdependency before it is finalized, that the finalizer runs once per cycle, and thatclose_asyncruns once per cycle.test_router_included_between_runs_sees_the_open_rootandtest_shutdown_without_startup_closes_the_rootcover routers added between runs and the fallback.dispatcher.shutdownhandler to signal that shutdown had started. That handler now runs after the wait, so they signal from a wrapper arounddispatcher.emit_shutdowninstead.Closes #51