Repository navigation
fix: close the root container after every after_shutdown hook - #83
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_diused to registercontainer.close_asyncas anafter_shutdownhook when it was called. FastStream runsafter_shutdownhooks in registration order, so a hook registered aftersetup_diran against a closed root and gotContainerClosedError, after theAPPfinalizers had already run.With
manage_lifespan=True,setup_dinow registers oneon_startuphook that opens the root and, on the first startup only, appendsclose_asyncto theafter_shutdownhooks. By then the user has registered their hooks, so the close runs after all of them. A flag in the closure stops a restarted app from appending the close a second time. This follows the startup install of the middleware in ADR 0002.manage_lifespan=Falseis unchanged: neither open nor close is registered.FastStream details this relies on (0.7.7)
on_startupandafter_shutdownappend anapply_typeswrapper to the lists_on_startup_callingand_after_shutdown_calling. Because of the wrapper, a membership check on the original callable does not work, so the guard is a flag.start()iterates_on_startup_callingandstop()iterates_after_shutdown_callingonly at shutdown, so appending to the shutdown list during startup takes effect in the same cycle.TestAppcalls the samestart()andstop().AsgiFastStreaminherits both methods.as_asgi()(AsgiFastStream.from_app) shares the four hook lists with the original app, so the close lands in the right list whethersetup_digets aFastStream, anAsgiFastStream, or aFastStreamthat is converted later.Limits
FastStream(lifespan=...)context manager exits after all hooks, so its teardown still runs after the close. FastStream has no public hook after it. The README now says so and points tomanage_lifespan=Falsefor that case.after_shutdownhook appended during startup by anon_startuphook registered aftersetup_diruns after the close. The same goes for a hook registered between two runs of the same app, since the close keeps its position from the first startup.Tests
test_after_shutdown_hook_registered_after_setup_di_sees_the_open_rootregisters anafter_shutdownhook aftersetup_di, runs twoTestAppcycles, and checks that the hook resolves theAPPdependency before it is finalized, that the finalizer runs once per cycle, and thatclose_asyncis called once per cycle. It runs on aFastStream, anAsgiFastStream, and aFastStreamconverted withas_asgi()aftersetup_di. All three failed on main withContainerClosedError. With the guard removed the close count check fails. The existingmanage_lifespan=Falsetests still pass. The repro from the issue now prints['user:live', 'finalized']for every hook case.Closes #82