This is an automated email from the ASF dual-hosted git repository. rahulvats pushed a commit to branch fix/asset-watcher-trigger-hash-mismatch in repository https://gitbox.apache.org/repos/asf/airflow.git
commit 12d8ca29847489c2716fb045b44140b2a2f742d8 Author: vatsrahul1001 <[email protected]> AuthorDate: Thu Apr 2 18:26:25 2026 +0530 Fix asset watcher triggers deleted on every dag-parsing loop --- .../src/airflow/dag_processing/collection.py | 49 +++++++++++++++++----- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/airflow-core/src/airflow/dag_processing/collection.py b/airflow-core/src/airflow/dag_processing/collection.py index 1d60c32020f..aacf50d9710 100644 --- a/airflow-core/src/airflow/dag_processing/collection.py +++ b/airflow-core/src/airflow/dag_processing/collection.py @@ -727,11 +727,12 @@ def _find_all_asset_aliases(dags: Iterable[LazyDeserializedDAG]) -> Iterator[Ser def _find_active_assets(name_uri_assets: Iterable[tuple[str, str]], session: Session) -> set[tuple[str, str]]: + _name_uri_list = list(name_uri_assets) return { (str(row[0]), str(row[1])) for row in session.execute( select(AssetModel.name, AssetModel.uri).where( - tuple_(AssetModel.name, AssetModel.uri).in_(name_uri_assets), + tuple_(AssetModel.name, AssetModel.uri).in_(_name_uri_list), AssetModel.active.has(), AssetModel.scheduled_dags.any( DagScheduleAssetReference.dag.has(~DagModel.is_stale & ~DagModel.is_paused) @@ -1001,7 +1002,26 @@ class AssetModelOperation(NamedTuple): def add_asset_trigger_references( self, assets: dict[tuple[str, str], AssetModel], *, session: Session ) -> None: - from airflow.serialization.encoders import encode_trigger + def _get_raw_trigger_kwargs(trigger): + """ + Extract classpath and raw Python kwargs from a trigger. + + Unlike encode_trigger, this does NOT wrap values with BaseSerialization. + For dict triggers (from deserialized DAGs), any BaseSerialization-wrapped + values are unwrapped back to Python objects, ensuring hash consistency + with kwargs read back from the DB via Trigger._decrypt_kwargs. + """ + if isinstance(trigger, dict): + classpath = trigger["classpath"] + raw_kwargs = trigger["kwargs"] + unwrapped = {} + for k, v in raw_kwargs.items(): + if isinstance(v, dict) and Encoding.TYPE in v: + unwrapped[k] = BaseSerialization.deserialize(v) + else: + unwrapped[k] = v + return classpath, unwrapped + return trigger.serialize() # Update references from assets being used refs_to_add: dict[tuple[str, str], set[int]] = {} @@ -1013,14 +1033,15 @@ class AssetModelOperation(NamedTuple): for name_uri, asset in self.assets.items(): # If the asset belong to a DAG not active or paused, consider there is no watcher associated to it - asset_watcher_triggers = ( - [ - {**encode_trigger(watcher.trigger), "watcher_name": watcher.name} + if name_uri in active_assets: + asset_watcher_triggers = [ + {"classpath": cp, "kwargs": kw, "watcher_name": watcher.name} for watcher in asset.watchers + for cp, kw in [_get_raw_trigger_kwargs(watcher.trigger)] ] - if name_uri in active_assets - else [] - ) + else: + asset_watcher_triggers = [] + trigger_hash_to_trigger_dict: dict[int, dict] = { BaseEventTrigger.hash(trigger["classpath"], trigger["kwargs"]): trigger for trigger in asset_watcher_triggers @@ -1029,9 +1050,15 @@ class AssetModelOperation(NamedTuple): trigger_hash_from_asset: set[int] = set(trigger_hash_to_trigger_dict.keys()) asset_model = assets[name_uri] - trigger_hash_from_asset_model: set[int] = { - BaseEventTrigger.hash(trigger.classpath, trigger.kwargs) for trigger in asset_model.triggers - } + + trigger_hash_from_asset_model: set[int] = set() + for trigger in asset_model.triggers: + try: + trigger_hash_from_asset_model.add( + BaseEventTrigger.hash(trigger.classpath, trigger.kwargs) + ) + except (ImportError, KeyError, AttributeError, TypeError): + log.warning("Failed to hash trigger %s, skipping", trigger.id, exc_info=True) # Optimization: no diff between the DB and DAG definitions, no update needed if trigger_hash_from_asset == trigger_hash_from_asset_model:
