Skip to content

Conversation

@iamapez
Copy link

@iamapez iamapez commented Nov 11, 2025

Summary

Fixes #58205

The try_adopt_task_instances method in the ECS Executor was calling ti.command_as_list() which doesn't exist in Airflow 3.x due to Task SDK changes. This caused the scheduler to crash with AttributeError when trying to adopt orphaned ECS tasks.

The Problem

  • In Airflow 3.x, the TaskInstance.command_as_list() method was removed as part of the Task SDK refactoring
  • The ECS Executor's try_adopt_task_instances method still calls this non-existent method at line 598
  • This causes immediate scheduler failure on startup when using ECS Executor with Airflow 3.x

The Solution

This PR adds version-aware code that:

  • Checks AIRFLOW_V_3_0_PLUS to determine the Airflow version
  • For Airflow 3.x: Reconstructs the command list manually from task instance attributes
  • For Airflow 2.x: Uses the existing command_as_list() method

Testing

  • Python syntax validated with py_compile
  • The fix maintains backward compatibility with Airflow 2.x
  • Scheduler starts successfully with this fix applied to Airflow 3.1.1

Checklist

  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • The fix is minimal and focused on the specific issue
  • Maintains backward compatibility with Airflow 2.x

…tances

The try_adopt_task_instances method was calling ti.command_as_list() which
doesn't exist in Airflow 3.x due to Task SDK changes. This caused scheduler
to crash with AttributeError when trying to adopt orphaned ECS tasks.

This fix:
- Checks AIRFLOW_V_3_0_PLUS to determine Airflow version
- For Airflow 3.x: Reconstructs the command list manually from task instance attributes
- For Airflow 2.x: Uses the existing command_as_list() method

Fixes apache#58205
@iamapez iamapez requested a review from o-nikolas as a code owner November 11, 2025 17:54
@boring-cyborg
Copy link

boring-cyborg bot commented Nov 11, 2025

Congratulations on your first Pull Request and welcome to the Apache Airflow community! If you have any issues or are unsure about any anything please check our Contributors' Guide (https://github.com/apache/airflow/blob/main/contributing-docs/README.rst)
Here are some useful points:

  • Pay attention to the quality of your code (ruff, mypy and type annotations). Our prek-hooks will help you with that.
  • In case of a new feature add useful documentation (in docstrings or in docs/ directory). Adding a new operator? Check this short guide Consider adding an example DAG that shows how users should use it.
  • Consider using Breeze environment for testing locally, it's a heavy docker but it ships with a working Airflow and a lot of integrations.
  • Be patient and persistent. It might take some time to get a review or get the final approval from Committers.
  • Please follow ASF Code of Conduct for all communication including (but not limited to) comments on Pull Requests, Mailing list and Slack.
  • Be sure to read the Airflow Coding style.
  • Always keep your Pull Requests rebased, otherwise your build might fail due to changes not related to your commits.
    Apache Airflow is a community-driven project and together we are making it better 🚀.
    In case of doubts contact the developers at:
    Mailing List: dev@airflow.apache.org
    Slack: https://s.apache.org/airflow-slack

@boring-cyborg boring-cyborg bot added area:providers provider:amazon AWS/Amazon - related issues labels Nov 11, 2025
In Airflow 3.x, the TaskInstance.execution_date attribute was renamed
to logical_date. This updates the fix to use the correct attribute name.
@iamapez
Copy link
Author

iamapez commented Nov 11, 2025

Updated: Also fixed execution_datelogical_date rename in Airflow 3.x.

The TaskInstance attribute execution_date was renamed to logical_date in Airflow 3.x, so the fix now uses the correct attribute name when constructing the command for adopted tasks.

Comment on lines 600 to 607
command = [
"airflow",
"tasks",
"run",
ti.dag_id,
ti.task_id,
ti.logical_date.isoformat(),
]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not how you want to generate a cmd for an Airflow 3.x task. The commands look different in AF3.

See here: https://github.com/apache/airflow/blob/main/providers/amazon/src/airflow/providers/amazon/aws/executors/ecs/ecs_executor.py#L509-L518

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could consider making this a helper function now as well, instead of copy/pasting the implementation here.

Match the command generation pattern used for newly queued tasks.
Creates ExecuteTask workload, serializes it as JSON, and constructs
the proper SDK command structure.
@iamapez
Copy link
Author

iamapez commented Nov 12, 2025

Updated to use proper Task SDK workload serialization pattern.

Now matches the command generation used for newly queued tasks (lines 509-518):

  • Creates ExecuteTask workload with ExecuteTask.make(ti)
  • Serializes with model_dump_json()
  • Constructs SDK command: python -m airflow.sdk.execution_time.execute_workload --json-string <serialized>

@iamapez iamapez requested a review from o-nikolas November 12, 2025 21:54
Copy link
Contributor

@o-nikolas o-nikolas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a unit tests we can add for this?

Comment on lines 600 to 607
command = [
"airflow",
"tasks",
"run",
ti.dag_id,
ti.task_id,
ti.logical_date.isoformat(),
]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could consider making this a helper function now as well, instead of copy/pasting the implementation here.

@o-nikolas o-nikolas self-requested a review November 26, 2025 20:06
@o-nikolas o-nikolas dismissed their stale review November 26, 2025 20:07

Removing request for change as the critical piece was addressed

@o-nikolas
Copy link
Contributor

@iamapez Are you planning to get back to this one?

I've removed my request for changes since you've addressed the main issue. But I still think tidying the command code into a helper since it's now copied in two places is worth doing.

Address code review feedback by consolidating duplicated command generation
into reusable helper methods and adding comprehensive test coverage.

Changes:
- Add _serialize_workload_to_command() static method for workload serialization
- Add _build_task_command() method for version-aware command generation
- Refactor execute_async() to use helper method (removed 9 lines of duplication)
- Refactor try_adopt_task_instances() to use helper method (removed 18 lines)
- Add comprehensive unit tests for both Airflow 2.x and 3.x compatibility
- Update test_try_adopt_task_instances and remove skip marker for Airflow 3.x
- Add Tradepost Markets Inc to INTHEWILD.md

Test coverage:
- test_serialize_workload_to_command: validates workload-to-command conversion
- test_build_task_command_airflow3: validates Airflow 3.x command generation
- test_build_task_command_airflow2: validates Airflow 2.x command generation
- test_try_adopt_task_instances: updated for Airflow 3.x support
@iamapez iamapez force-pushed the fix-ecs-executor-airflow3-compatibility branch from 3fd19b5 to ca4bdf8 Compare November 28, 2025 06:44
@iamapez
Copy link
Author

iamapez commented Nov 28, 2025

Consolidated the command generation logic into helper methods and added unit tests as requested. @o-nikolas

@iamapez
Copy link
Author

iamapez commented Dec 10, 2025

checking in on this pr @o-nikolas

@iamapez iamapez force-pushed the fix-ecs-executor-airflow3-compatibility branch from d1bc3ee to 59231e2 Compare December 11, 2025 00:10
@iamapez
Copy link
Author

iamapez commented Dec 11, 2025

sorry @o-nikolas accidental commit to this branch - i reverted the changes

@o-nikolas
Copy link
Contributor

sorry @o-nikolas accidental commit to this branch - i reverted the changes

@iamapez Looks like there are many test failures. Both static checks and unit test failures. Can you please have a look?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:providers provider:amazon AWS/Amazon - related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ECS Executor in apache-airflow-providers-amazon 9.16.0 incompatible with Airflow 3.x - calls removed method command_as_list()

2 participants