devnaldo opened a new pull request, #77207: URL: https://github.com/apache/airflow/pull/77207
## What This PR improves error handling for task and Dag run clearing APIs by returning HTTP 503 (Service Unavailable) for recognized transient database contention errors instead of HTTP 500 (Internal Server Error). The change applies to four public endpoints: 1. `clearTaskInstances` 2. `clear_dag_run` 3. `clear_dag_runs` 4. `clear_dag_run_partitions` Recognized database errors include: * PostgreSQL `55P03` (`lock_not_available`), supporting both psycopg2 (`pgcode`) and psycopg3 (`sqlstate`). * PostgreSQL `57014` (`query_canceled`), including statement timeouts. * PostgreSQL `40P01` (`deadlock_detected`). * MySQL `1205`, `3572`, and `1213`. * SQLite `database is locked`. The API returns a stable error message without exposing database internals. The underlying exception is logged at warning level for debugging. The 503 responses are also documented in the generated OpenAPI specification and UI API client. ## Why Database contention can cause task clearing requests to fail even when the underlying operation may succeed if retried later. Returning HTTP 500 does not communicate that this is a potentially transient condition. Returning HTTP 503 provides clients with a more appropriate status code. ## Implementation details * Added `is_db_contention_error()` to identify supported database contention errors. * Added `translate_db_contention_errors()` to handle these errors at the individual route level. * Kept the existing global database exception handling unchanged. * Unrelated `OperationalError` exceptions continue to propagate through the existing error handling. * The execution API behavior remains unchanged. ## Scope Related to #77201. This PR improves error handling for the existing synchronous task clearing endpoints. It does not implement asynchronous clearing, durable operation tracking, background processing, or progress reporting. ## Testing * Targeted tests: 21 passed. * Affected test suites: 278 passed, 6 skipped. * Breeze mypy on changed source and test files: passed. * Ruff formatting and lint checks: passed. * FastAPI OpenAPI generation hook: passed. * `git diff --check`: clean. The `ts-compile-lint-ui` hook has a repository configuration issue: generated `openapi-gen` files are ignored by Oxlint even though the hook passes them for linting. This was reproduced independently on an unchanged generated file. The hook was skipped for the commit; the generated API files were verified through the OpenAPI generation hook. ## Generative AI disclosure - [x] Yes (OpenCode, DeepSeek V4 PRO) Generated-by: OpenCode (DeepSeek V4 PRO) following [[the guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions). -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
