Repository navigation
Description
We have four WatchedSubprocess subclasses that each implement _handle_request with large if/elif chains dispatching comms messages. Many of these handlers are duplicated inline across multiple supervisors:
- ActivitySubprocess (task-sdk, supervisor.py)
- CallbackSubprocess (task-sdk, callback_supervisor.py)
- TriggerRunnerSupervisor (airflow-core, triggerer_job_runner.py)
- DagFileProcessorProcess (airflow-core, processor.py)
A request_handlers.py module already exists in task-sdk/src/airflow/sdk/execution_time/ with shared handlers for GetConnection, GetVariable, GetAssetByName, GetAssetByUri, and MaskSecret. The Callback and Activity supervisors use these, but the Triggerer and DFP still have inline duplicates.
The Task:
Extract the remaining duplicated handler logic into shared functions in request_handlers.py and have all four supervisors call them.
Note: The implementations across supervisors are similar but not identical. Some include isinstance() guards on the response; some pass different parameter subsets; some set dump_opts while others don't. When extracting, don't just copy one supervisor's version verbatim. Compare all implementations of each handler and produce a single version that incorporates the best practices from each (e.g. proper response type checking, consistent dump_opts, full parameter forwarding).
Here is my suggestion, but you may come up with a better set of rules once you start working on it:
- Use the existing shared handlers as a guide, but don't be afraid to modify them if you come up with a new standard.
- Each new handler should be a standalone function with this signature:
def handle_<message_type>(client: Client, msg: <MessageType>) -> tuple[BaseModel | None, dict[str, bool]]:
- Always guard the response with
isinstancebefore converting it to a comms result model. The API client can returnErrorResponseon failure; when that happens, pass it through unchanged. - Always return dump_opts as the second tuple element. Use
{"exclude_unset": True}for result models that wrap API responses to avoid serializingNonefields. Add{"by_alias": True}when the result model uses field aliases (currently onlyConnectionResult). For simple pass-through responses or error responses, return{}. - Forward all message fields to the client call. Some supervisors currently pass fewer parameters than others for the same message type (e.g. the Triggerer's
GetXComomitsinclude_prior_dates). The shared handler should forward every field that the message carries and let the client and server handle defaults for optional fields. - Always mask secrets. If the response contains sensitive data (passwords, tokens, variable values), call
mask_secret()before returning. Look athandle_get_connectionandhandle_get_variablefor examples. - Handlers that don't return a response such as
PutVariableandDeleteXComcan use a simpler signature.return (None, {})or returnNonedirectly likehandle_mask_secretdoes. Pick whichever is cleaner for the specific case, but be consistent within a batch. - Keep handlers stateless. They should not touch subprocess internals (exit codes, terminal states, etc.). If a message type requires updating subprocess state, it belongs inline in that supervisor's
_handle_request, not in a shared handler.
Phase 1: High-overlap handlers (three or more supervisors share this logic)
- GetXCom - Activity, Triggerer, DFP
- PutVariable - Activity, Triggerer, DFP
- DeleteVariable - Activity, Triggerer, DFP
- GetTICount - Activity, Triggerer, DFP
- GetTaskStates - Activity, Triggerer, DFP
- GetPreviousTI - Activity, Triggerer, DFP
Phase 2: Medium-overlap handlers (two supervisors share these)
- SetXCom - Activity, Triggerer
- DeleteXCom - Activity, Triggerer
- GetDRCount - Activity, Triggerer
- GetDagRunState - Activity, Triggerer
- GetPreviousDagRun - Activity, DFP
- GetPrevSuccessfulDagRun - Activity, DFP
- GetXComCount - Activity, DFP
- GetXComSequenceItem - Activity, DFP
- GetXComSequenceSlice - Activity, DFP
Phase 2.5: Callback Supervisor
Comms channels were added to the Callback supervisor in #65269 and all of the comms channels it has as of the time I am writing this are already in shared helpers, but have a look and make sure that is still true while you are doing this.
Phase 3: Migrate DagFileProcessorProcess onto shared handlers
The DFP currently has fully inline versions of GetConnection, GetVariable, and MaskSecret even though shared handlers already exist. One thing to note: the DFP's inline GetConnection handler skips the mask_secret() calls on password and extra that the shared handler performs, which I personally feel is a bug but may require discussion. Maybe there's a reason I'm not aware of.
Out of Scope:
Supervisor-specific messages (e.g. TaskState, DeferTask, TriggerStateChanges, DagFileParsingResult) should either stay inline or move to a supervisor-specific helper/utils module (I vote leave alone, personally) since they interact with internal subprocess state.
Activity
leeyspaul commentedon Apr 20, 2026
Interesting, I would like to take this one on! I'll be working on it, and report back with further questions or a PR.
ferruzzi commentedon Apr 20, 2026
MOST of it should be copypasta to reduce redundant code into helpers, but it may take some discussion, feel free to ask for advice in the community slack server if anything looks odd.
leeyspaul commentedon Apr 20, 2026
Hey @ferruzzi , I'm currently ramping up on context of the mentioned code but I'm not seeing a
request_handlers.pyunder the mentionedtask-sdk/src/airflow/sdk/execution_time/. Is this something we want created here or is it already present on another branch/PR?ferruzzi commentedon Apr 20, 2026
Got ahead of myself. It will be added when #65269 merges. Sorry about that 😅
leeyspaul commentedon Apr 20, 2026
Thank you, that helps! I'll assume #65269 lands first and implement this on top of that structure after rebasing.
ferruzzi commentedon Apr 21, 2026
Yeah, #65269 should theoretically merge in the next day or three, so you can pull that down and use that as your upstream if you want, or just assume it'll be rebased in later. Whatever fits your workflow best.
leeyspaul commentedon Apr 24, 2026
@ferruzzi this PR: #65624 is now ready for review 👀
ferruzzi commentedon Apr 24, 2026
I just created a follow-up Issue for after this one is done. I'm going to be away on vacation, but if you or someone else wants to tackle it while I'm away, it's the next logical step after this gets merged.
leeyspaul commentedon Apr 24, 2026
Yeah, I'm happy to take it up as it builds on the previous. Have a good vacation!
Quantum0uasar commentedon May 21, 2026
Hi! I'd love to help with this refactoring effort.
My understanding of the task:
_handle_requesthandler logic fromActivitySubprocess,CallbackSubprocess,TriggerRunnerSupervisor, andDagFileProcessorProcessinto shared handlers inrequest_handlers.pytask-sdk/src/airflow/sdk/execution_time/request_handlers.pyI'll start by:
_handle_requestto identify common patternsWould this be best done incrementally (one supervisor at a time) or all at once? Happy to open a draft PR with the first supervisor extracted to get early feedback!
ferruzzi commentedon May 21, 2026
@leeyspaul - with your PR merged, this is completed, right?
leeyspaul commentedon May 21, 2026
Yes, I believe so!
ferruzzi commentedon May 21, 2026
Thanks for the work! Closing.