airflow icon indicating copy to clipboard operation
airflow copied to clipboard

DAG.test() fails for tasks whose task_id contains non ASCII characters

Open per-andersen opened this issue 1 month ago • 5 comments

Under which category would you file this issue?

Airflow Core

Apache Airflow version

3.3.2, main

What happened and how to reproduce it?

Running a DAG with DAG.test() fails for any task whose task_id contains a non ASCII character, as soon as that task pushes a return value XCom. The task exhausts its retries and the DAG run ends up failed.

In our case the affected task ids are Danish: Købsmoms, ResultatOpgørelse, ResultatOpgørelseSammenligning.

To reproduce, save the following in your DAGs folder and run:

from airflow.sdk import DAG, task

with DAG(dag_id="non_ascii_task_id", schedule=None) as dag:

    @task(task_id="Købsmoms", retries=0)
    def koebsmoms() -> str:
        # Any non-None return triggers the return-value XCom push.
        return "value"

    koebsmoms()


if __name__ == "__main__":
    dag.test()

Expected: the run succeeds. Actual: UnicodeDecodeError and the DAG run fails. Without retries=0, the task retries on every attempt first.

The same DAG with an ASCII task_id (e.g. Kobsmoms) succeeds.

What you think should happen instead?

Here is a traceback:

Traceback (most recent call last):
  File ".../airflow/sdk/execution_time/task_runner.py", line 1598, in run
    _push_xcom_if_needed(result, ti, log)
  File ".../airflow/sdk/execution_time/task_runner.py", line 2188, in _push_xcom_if_needed
    _xcom_push(ti, BaseXCom.XCOM_RETURN_KEY, result, mapped_length=mapped_length)
  File ".../airflow/sdk/execution_time/task_runner.py", line 883, in _xcom_push
    XCom.set(
  File ".../airflow/sdk/bases/xcom.py", line 87, in set
    SUPERVISOR_COMMS.send(
  File ".../airflow/sdk/execution_time/supervisor.py", line 1949, in send
    self.supervisor._handle_request(msg, log, 0)
  File ".../airflow/sdk/execution_time/supervisor.py", line 1726, in _handle_request
    resp, dump_opts = handle_set_xcom(self.client, msg)
  File ".../airflow/sdk/execution_time/request_handlers.py", line 182, in handle_set_xcom
    client.xcoms.set(
  File ".../airflow/sdk/api/client.py", line 634, in set
    self.client.post(f"xcoms/{dag_id}/{run_id}/{task_id}/{key}", params=params, json=value)
  ...
  File ".../httpx/_transports/wsgi.py", line 136, in handle_request
    stream = WSGIByteStream(result)
  File ".../a2wsgi/asgi.py", line 207, in start_asgi_app
    self.app(build_scope(environ), self.asgi_receive, self.asgi_send),
  File ".../a2wsgi/asgi.py", line 93, in build_scope
    path = root_path + environ.get("PATH_INFO", "").encode("latin1").decode("utf8")
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf8 in position 62: invalid start byte

Byte 0xf8 is ø. In our run, position 62 lands exactly on the task_id inside xcoms/{dag_id}/{run_id}/{task_id}/{key}; the exact position depends on the length of the dag_id and run_id.

The task instance then goes to up_for_retry, repeats on every attempt, and the DAG run finishes as failed.

Task ids are validated by validate_key in airflow/sdk/definitions/_internal/node.py, which uses KEY_REGEX = r"^[\w.-]+$". In Python, \w matches Unicode letters, so Købsmoms is accepted as a valid task_id when the DAG is defined. A key that passes validation should not then fail at runtime. Either:

  • DAG.test() should handle non-ASCII task_id values, so the in-process test harness is not the one place they break, or
  • validation should reject non-ASCII ids at definition time, with a message that points to task_display_name / dag_display_name.

Operating System

Ubuntu Server 24.04.5 LTS

Deployment

Other Docker-based deployment

Apache Airflow Provider(s)

No response

Versions of Apache Airflow Providers

Not provider-specific. From the 3.3.2 / Python 3.11 constraints file: apache-airflow-providers-standard==1.19.0

Official Helm Chart version

No response

Kubernetes Version

Not Applicable

Helm Chart configuration

No response

Docker Image customizations

Not Applicable

Anything else?

Every time a non ASCII character is used in task_id of DAG.test().

Are you willing to submit PR?

  • [x] Yes I am willing to submit a PR!

Code of Conduct

per-andersen avatar Oct 08 '26 17:10 per-andersen

Thanks for opening your first issue here! Be sure to follow the issue template! If you are willing to raise PR to address this issue please do so, no need to wait for approval.

boring-cyborg[bot] avatar Oct 08 '26 17:10 boring-cyborg[bot]

Hi, I reproduced the reasoning in the traceback and checked task-sdk/src/airflow/sdk/api/client.py: the xcoms/... paths interpolate dag_id/run_id/task_id/key raw, while connections, store/ti, and dags in the same file use urllib.parse.quote(..., safe=''). That matches the latin1/utf8 failure in DAG.test()'s in-process WSGI transport for ids like Købsmoms.

If this is still unaddressed, I would like to work on it: quote the XCom path segments (and check task-instances/previous), with a DAG.test() non-ASCII regression test plus client URL tests. Does that direction sound right, or is there a reason these segments are intentionally unquoted?

Piyush00715 avatar Oct 13 '26 08:10 Piyush00715

Update on my comment above: I checked further and the quoting direction is wrong — please disregard it. httpx.URL.path returns the decoded path and WSGITransport passes it verbatim as WSGI PATH_INFO, so pre-quoting client URLs converges to the same value and cannot fix this crash (real HTTP deployments were never broken; httpx percent-encodes on the wire).

The actual fault is in the in-process harness used by DAG.test(): InProcessExecutionAPI.transport (airflow-core/src/airflow/api_fastapi/execution_api/in_process.py) chains httpx.WSGITransport → a2wsgi ASGIMiddleware → FastAPI. httpx supplies decoded Unicode as PATH_INFO, violating PEP 3333, and a2wsgi's encode("latin1").decode("utf8") then raises the exact UnicodeDecodeError: byte 0xf8 from the traceback.

I have a local fix ready: a small PEP 3333 normalization wrapper around the WSGI app in transport (UTF-8 → latin1 re-encode; identity for ASCII), plus regression tests in tests/unit/api_fastapi/execution_api/test_in_process.py. Verified the mechanism against real httpx 0.28.1 (raw transport reproduces the crash, wrapped serves 200 with the exact path). I could not run the repo suite here (no Breeze/Docker Linux env on this machine) — CI would need to confirm.

Does a transport-level fix in in_process.py sound like the right layer, or would you rather handle it elsewhere? Happy to open a PR once confirmed.

Piyush00715 avatar Oct 13 '26 08:10 Piyush00715

Noted #74472 — I missed that it was already open before my comments. It targets the same root cause and layer I arrived at independently (in-process WSGI PATH_INFO handling), with broader coverage (SCRIPT_NAME, non-latin1 chars) than my local draft, so I'm stepping back — no competing PR from me. Good luck with the merge.

Piyush00715 avatar Oct 13 '26 10:10 Piyush00715

Thanks for the attention to the issue - and yes, there is an open PR. If you have any comments feel free to add them there.

per-andersen avatar Oct 14 '26 05:10 per-andersen