feat(etl-uvicorn): cooperative SIGTERM shutdown via GracefulServer + CancellationToken#70
Open
aballman wants to merge 9 commits into
Open
feat(etl-uvicorn): cooperative SIGTERM shutdown via GracefulServer + CancellationToken#70aballman wants to merge 9 commits into
aballman wants to merge 9 commits into
Conversation
…p on uvicorn 0.37)
…ite graceful timeout
Add pytest-asyncio to test deps (asyncio_mode=auto was declared but package was missing) and add test asserting that a sync run-func polling CancellationToken.raise_if_cancelled() between work units bails promptly via PluginShutdown when request_shutdown() fires mid-loop.
|
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
|
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.
Summary
Plugin uvicorn webservers now shut down cooperatively on SIGTERM instead of draining the in-flight request for an unbounded time. A cancelled job's plugin container can stop work and exit promptly rather than riding the pod termination grace cap.
Changes
GracefulServer(serve.py): subclassesuvicorn.Serverand overrideshandle_exitto set a process-global cancellation event before delegating to uvicorn's own shutdown. Theetl-uvicornCLI launches through it.CancellationToken(shutdown.py): a read-only token (no.set()) backed by that event.wrap_in_fastapiinjects it into any run-function that declares acancellation_tokenparameter — the same opt-in mechanism asusage/message_channels/filedata_meta. Sync run-loops pollraise_if_cancelled()between units of work; uvicorn cannot cancel a threadpool thread, so this is the only way a sync plugin stops promptly. Acancellation_dependency()FastAPI dependency is provided for plugins that build their own app.PluginShutdown→ HTTP 503: raised by a bailing run-loop, caught before the generic exception handler in both the non-streaming and streaming paths, and returned as a distinct shutdown-abort response rather than a plugin failure.timeout_graceful_shutdown: default 30s, overridable viaUVICORN_TIMEOUT_GRACEFUL_SHUTDOWN. uvicorn's default is unbounded, so an in-flight async request previously had no drain ceiling.cancellation_tokenis omitted from the/invokerequest model, the/schemaoutput, and the plugin-id hash, so it never appears as a caller-facing input.uvicorn.Server.install_signal_handlers, which the pinned uvicorn (0.37) removed in favor ofcapture_signals(), so it never took effect. Prompt shutdown now comes from the finite timeout plus the GracefulServer hook.check_precheck_func: validates parameter names against the injected set (and acceptscancellation_token), replacing logic that indexed a non-subscriptabledict_values.Testing
ruff checkclean. New coverage: token injection and schema non-leak (sync and async),PluginShutdown→ 503 (non-streaming and streaming), cooperative stop of a threadpool-bound sync run-loop,GracefulServer.handle_exit,serve(), andcheck_precheck_func.etl-uvicornserver triggers the cancellation hook and the process exits within ~1s, well under any pod grace.Compatibility
Behavior is unchanged for plugins that do not declare
cancellation_token. Version bumped to 0.0.45.Follow-ups (not in this PR)
cancellation_tokenand threading it through provider-call loops, plus a finite graceful timeout at every launch site.