Skipped Airflow tasks lost task.duration: someone else fixed it first, so I added the test
Skipped tasks never sent task.duration. I opened a fix in June; in October a different PR made the same one-line change for a different reason. What it didn't check was the metric, so my PR shrank to the regression test, and review made that test check the number, not just that a call happened.
The bug
task.duration is the timer for how long a task ran. In Airflow 3 the Task SDK sends it
from finalize(), at the end of every task, behind one condition:
# Record task duration metrics for all terminal states
if ti.start_date and ti.end_date:
duration_ms = (ti.end_date - ti.start_date).total_seconds() * 1000
stats.timing("task.duration", duration_ms, tags=ti.stats_tags)
Success and failure set ti.end_date before they get here. The skip path didn’t. When a
task raised AirflowSkipException, the runner put an end date into the TaskState
message it sent to the supervisor, and left the local task instance without one. So
finalize() saw no end date and quietly sent nothing.
#61849 narrowed it down to the operators
people actually hit it with: ShortCircuitOperator, BranchPythonOperator, and
BashOperator with skip_on_exit_code.
Skipped tasks are a normal part of branching Dags, so a duration dashboard was silently leaving out every one of them. The same kind of gap as my first Airflow PR: nothing errors, a number just isn’t there.
Almost six months, mostly waiting
- 2026-06-03Opened the PR: set ti.end_date on the skip path, plus a testashb retitled it the same day; labelled ready for maintainer review
- 2026-08-13Triage removed the label over failing CIone failure was my test missing the run_type tag; the rest came from a packaging change on main
- 2026-08 – 10CI stuck at action_required, waiting for approvalrebased in September to pick up the fix from main, in October to clear a conflict
- 2026-10-27#75685 merged: sets ti.end_date for listenersfixes the metric as a side effect, with no test for it
- 2026-10-28PR cut down to a task.duration regression test, plus closes #61849
- 2026-11-26kaxil's review: three comments, all about what the test would catch
- 2026-12-01Approved and merged
As a first-time contributor, every push needs a maintainer to approve the CI run, so most of the summer was waiting: fix my own test, rebase, ask for approval again.
Someone else’s PR, the same line
On October 26 another contributor opened
#75685. It had nothing to do with metrics:
listeners are handed the task instance after it finishes, and for a skipped task they had no
way to read its end date. The fix was the same one I had proposed: set ti.end_date before
building the TaskState message. ashb merged it less than a day later.
That fixed the metric as a side effect. But #75685’s tests checked ti.end_date and what
the listener saw. Nothing checked task.duration itself. If someone later changed the gate
in finalize(), or moved the skip path again, the metric could vanish exactly the way it
had, and every test would still pass.
So instead of closing my PR, I cut it down to what was still missing: a regression test for
the metric, and closes #61849. I checked it the way I check any regression test: revert
the skip-path ti.end_date line from #75685 and the [skipped] case fails, while
[success] and [failed] still pass.
Review: check the number, not the call
After another month and two pings, kaxil reviewed it. Three comments, all about what the test would and wouldn’t catch.
- Assert the state you think you’re testing.
expected_stateonly appeared in the failure message. If a skip ever ended up on a failure path,[skipped]would still pass, and the skip path is the one this test exists for. Nowassert state == expected_stateruns right afterrun(). - Share the cases. The
ti.finishtest next to it had a fourth case,1 / 0, which reaches a different failure handler that setsend_dateon its own path. Both tests now read one list,TERMINAL_STATE_CASES, so they can’t drift apart. - Check the value. My test counted the calls and checked the tags, so dropping the
* 1000or flipping the subtraction infinalize()would still pass. He marked this one non-blocking, since the bug was a missing metric, not a wrong one. I did it anyway: time is frozen atstart_date + 5s, and both the tagged and the legacy name must report exactly5000.0.
time_machine.move_to(ti.start_date + timedelta(seconds=5), tick=False)
...
state, _, error = run(ti, context=context, log=mock.MagicMock())
assert state == expected_state
finalize(ti, state=state, context=context, log=mock.MagicMock(), error=error)
...
assert task_duration_calls[0].args[1] == 5000.0
backend.timing.assert_any_call(
f"dag.{ti.dag_id}.{ti.task_id}.duration", 5000.0, tags={"run_type": "manual"}
)
He approved the next day, and it was merged on December 1, almost six months after I opened it, with no production code left in it.
What I’ll remember next time
Look at what the other PR checks, not just what it changes. The same line fixed for a different reason usually comes with tests for that reason only.
Revert the fix and watch it fail. Then ask what else could break the same output and still pass: here, a wrong unit or a flipped subtraction.
Assert that each case reached the path its name claims. Otherwise a case can pass for a reason that has nothing to do with it.
I wrote this PR with Claude Code (Opus 5.5) and disclosed it with Generated-by: in the
PR, as Airflow’s guidelines ask.