Skip to content

Clearing a dag run with a dynamically mapped task causes a 409 error in the api server that fails the dag run #48538

Closed

Description

@TJaniF

Apache Airflow version

3.0.0

If "Other Airflow 2 version" selected, which one?

current main

What happened?

Clearing a previous (successful) run of the following dag from the UI causes a 409 error in the api-server:

from airflow.decorators import dag, task

@dag
def simple_dynamic_task_mapping():

    @task   
    def get_nums():
        
        return [1,2,3]    #A

    @task
    def times_2(num):    #B
        return num * 2

    _get_nums = get_nums()
    _times_2 = times_2.expand(num=_get_nums)    #D


simple_dynamic_task_mapping()    

The error is a 409 in the API server, then the scheduler, I think responding to that:

Image

The scheduler and API server end up fine but the dag run fails without an error log, the logs stop at filling up dagbag:

[2025-03-30, 00:41:25] INFO - DAG bundles loaded: dags-folder: source="airflow.dag_processing.bundles.manager.DagBundlesManager"
[2025-0[3](http://localhost:28080/dags/simple_dynamic_task_mapping/runs/manual__2025-03-30T00:41:15.805919+00:00_U2AisQsa/tasks/get_nums?try_number=2#3)-30, 00:41:25] INFO - Filling up the DagBag from /files/dags/dtm.py: source="airflow.models.dagbag.DagBag"

This does not happen with a dag that does not contain a dynamic task.

What you think should happen instead?

The dag should clear and rerun successfully.

How to reproduce

Run the dag above. Then clear it from the UI and see the error in the api-server and scheduler.

Operating System

MacOS

Versions of Apache Airflow Providers

None

Deployment

Other

Deployment details

breeze

Anything else?

No response

Are you willing to submit PR?

  • Yes I am willing to submit a PR!

Code of Conduct

Activity

  1. added
    kind:bug
    needs-triage
    on Mar 30, 2025
  2. added
    area:API
    on Mar 30, 2025
  3. added
    priority:high
    and removed
    needs-triage
    on Mar 30, 2025
  4. vatsrahul1001 commented on Mar 30, 2025

    @vatsrahul1001
    Contributor

    I see error in celery worker logs

    2025-03-30 05:59:38.325308 [warning  ] Server error                   [airflow.sdk.api.client] detail={'detail': {'reason': 'Unique constraint violation', 'statement': 'INSERT INTO task_map (dag_id, task_id, run_id, map_index, length, keys) VALUES (%(dag_id)s, %(task_id)s, %(run_id)s, %(map_index)s, %(length)s, %(keys)s)', 'orig_error': 'duplicate key value violates unique constraint "task_map_pkey"\nDETAIL:  Key (dag_id, task_id, run_id, map_index)=(simple_dynamic_task_mapping_new, get_nums, manual__2025-03-30T05:57:54.788032+00:00_5CocwY5o, -1) already exists.\n'}}
    [2025-03-30T05:59:38.340+0000] {trace.py:267} ERROR - Task execute_workload[c3a1a240-ecb4-447a-a7d5-1c90d9c8531e] raised unexpected: ServerResponseError('Server returned error')
    Traceback (most recent call last):
      File "/usr/local/lib/python3.9/site-packages/celery/app/trace.py", line 453, in trace_task
        R = retval = fun(*args, **kwargs)
      File "/usr/local/lib/python3.9/site-packages/celery/app/trace.py", line 736, in __protected_call__
        return self.run(*args, **kwargs)
      File "/opt/airflow/providers/celery/src/airflow/providers/celery/executors/celery_executor_utils.py", line 161, in execute_workload
        supervise(
      File "/opt/airflow/task-sdk/src/airflow/sdk/execution_time/supervisor.py", line 1153, in supervise
        exit_code = process.wait()
      File "/opt/airflow/task-sdk/src/airflow/sdk/execution_time/supervisor.py", line 730, in wait
        self._monitor_subprocess()
      File "/opt/airflow/task-sdk/src/airflow/sdk/execution_time/supervisor.py", line 788, in _monitor_subprocess
        alive = self._service_subprocess(max_wait_time=max_wait_time) is None
      File "/opt/airflow/task-sdk/src/airflow/sdk/execution_time/supervisor.py", line 625, in _service_subprocess
        need_more = socket_handler(key.fileobj)
      File "/opt/airflow/task-sdk/src/airflow/sdk/execution_time/supervisor.py", line 1010, in cb
        gen.send(line)
      File "/opt/airflow/task-sdk/src/airflow/sdk/execution_time/supervisor.py", line 514, in handle_requests
        self._handle_request(msg, log)
      File "/opt/airflow/task-sdk/src/airflow/sdk/execution_time/supervisor.py", line 916, in _handle_request
        self.client.xcoms.set(
      File "/opt/airflow/task-sdk/src/airflow/sdk/api/client.py", line 332, in set
        self.client.post(f"xcoms/{dag_id}/{run_id}/{task_id}/{key}", params=params, json=value)
      File "/usr/local/lib/python3.9/site-packages/httpx/_client.py", line 1145, in post
        return self.request(
      File "/usr/local/lib/python3.9/site-packages/tenacity/__init__.py", line 336, in wrapped_f
        return copy(f, *args, **kw)
      File "/usr/local/lib/python3.9/site-packages/tenacity/__init__.py", line 475, in __call__
        do = self.iter(retry_state=retry_state)
      File "/usr/local/lib/python3.9/site-packages/tenacity/__init__.py", line 376, in iter
        result = action(retry_state)
      File "/usr/local/lib/python3.9/site-packages/tenacity/__init__.py", line 398, in <lambda>
        self._add_action_func(lambda rs: rs.outcome.result())
      File "/usr/local/lib/python3.9/concurrent/futures/_base.py", line 439, in result
        return self.__get_result()
      File "/usr/local/lib/python3.9/concurrent/futures/_base.py", line 391, in __get_result
        raise self._exception
      File "/usr/local/lib/python3.9/site-packages/tenacity/__init__.py", line 478, in __call__
        result = fn(*args, **kwargs)
      File "/opt/airflow/task-sdk/src/airflow/sdk/api/client.py", line 538, in request
        return super().request(*args, **kwargs)
      File "/usr/local/lib/python3.9/site-packages/httpx/_client.py", line 827, in request
        return self.send(request, auth=auth, follow_redirects=follow_redirects)
      File "/usr/local/lib/python3.9/site-packages/httpx/_client.py", line 914, in send
        response = self._send_handling_auth(
      File "/usr/local/lib/python3.9/site-packages/httpx/_client.py", line 942, in _send_handling_auth
        response = self._send_handling_redirects(
      File "/usr/local/lib/python3.9/site-packages/httpx/_client.py", line 999, in _send_handling_redirects
        raise exc
      File "/usr/local/lib/python3.9/site-packages/httpx/_client.py", line 982, in _send_handling_redirects
        hook(response)
      File "/opt/airflow/task-sdk/src/airflow/sdk/api/client.py", line 107, in raise_on_4xx_5xx
        return get_json_error(response) or response.raise_for_status()
      File "/opt/airflow/task-sdk/src/airflow/sdk/api/client.py", line 103, in get_json_error
        raise err
    airflow.sdk.api.client.ServerResponseError: Server returned error
    [2025-03-30 05:59:41 +0000] [171] [INFO] Handling signal: winch
    [2025-03-30 06:01:39 +0000] [171] [INFO] Handling signal: winch
    
  5. vatsrahul1001 commented on Mar 30, 2025

    @vatsrahul1001
    Contributor

    It failed due to a unique constraint violation while non-mapped task was being inserted into the task_map table.

  6. vatsrahul1001 commented on Mar 30, 2025

    @vatsrahul1001
    Contributor

    I tried replicating in AF2 and noticed for same DAG we were not insterting records in task_map table.

  7. vikramkoka commented on Mar 31, 2025

    @vikramkoka
    Contributor

    Daniel Standish,
    Assigning it to you, since this looks similar to the other mapped task issue. Please feel free to reassign if not

  8. added this to the Airflow 3.0.0 milestone on Apr 15, 2025
  9. Zifeng-Ma commented on Aug 5, 2025

    @Zifeng-Ma

    Hi, I am experiencing the same error but not in mapped task. (in regular "@task").

    [2025-08-05T09:36:41.244+0000] {_client.py:1026} INFO - HTTP Request: PATCH http://localhost:8080/execution/task-instances/01987997-1396-7340-a
    f22-7f69d9c86c28/run "HTTP/1.1 409 Conflict"
    2025-08-05 09:36:41 [warning ] Server error
    [airflow.sdk.api.client] detail={'detail': {'reason': 'invalid_state', 'message$
    : 'TI was not in a state where it could be marked as running', 'previous_state': 'scheduled'}}

    Can you help me have a look? Thanks!

  10. BruAPAHE commented on Oct 28, 2025

    @BruAPAHE

    bump, have some for task mapper 409

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

Metadata

Metadata

Assignees

Labels

area:APIarea:corearea:dynamic-task-mappingkind:bugpriority:high

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions