Repository navigation
Conversation
|
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
|
|
cc @moomindani for Databricks team review |
moomindani
left a comment
There was a problem hiding this comment.
Thanks for this, and welcome — this is a real bug with a real operational impact, and the diagnosis is correct.
I verified it end-to-end against a local Postgres 16.13 rather than reasoning about it, writing the actual payload shapes into a jsonb column:
| Case | Result |
|---|---|
errors[].error with NUL, unsanitized |
rejected: UntranslatableCharacter: unsupported Unicode escape sequence — exactly the error you reported |
same, via make_jsonb_safe |
stored OK |
| unpaired surrogate, unsanitized | rejected: InvalidTextRepresentation: invalid input syntax for type json |
| same, sanitized | stored OK |
So both classes you strip are genuinely rejected by Postgres, and the fix genuinely resolves them. The triggerer CrashLoopBackOff consequence follows from the event being retried, which makes this worth fixing rather than working around.
I also checked the regex is not over-broad: \x01, \x1f, newlines, tabs, and non-BMP characters (emoji) are all accepted by jsonb, and your pattern preserves all of them. It strips exactly the two rejected classes and nothing else — that scoping is right.
One thing I want to record because I initially suspected it was a gap and it turned out not to be: the same TriggerEvent carries run_state, which embeds Databricks-controlled state_message. That is not a second hole, because RunState.to_json() stringifies it first, so a NUL becomes a literal \u0000 escape inside a JSON string value and Postgres accepts it. I confirmed that stores fine. Scoping the fix to errors is correct — no need to widen it.
One blocker before this can merge: a ruff failure, flagged inline. CI's Static checks job is already failing on it.
Drafted-by: Claude Code (Opus 5)
|
@fabbuc-gyg can you please fix the open items? |
|
Hi @eladkal, So sorry for the delay. Will check this out today. |
1b79983 to
f1bb073
Compare
|
Should be fine now :-) |
moomindani
left a comment
There was a problem hiding this comment.
Both points from my July review are closed on f1bb073570. LGTM.
Worth recording because it is not visible from the diff: I mutation-tested the sanitization tests, since tests like these easily pass either way. Making make_jsonb_safe return its argument unchanged fails exactly the four stripping tests and leaves the clean-text and non-string cases green — they pin the behaviour.
#72313 touches the same two failed_tasks.append(...) sites, so whichever lands second needs a trivial rebase; the changes compose.
Drafted-by: Claude Code (Opus 5); reviewed by @moomindani before posting
seanmuth
left a comment
There was a problem hiding this comment.
Thanks for tracking this down — we hit this exact crash in production yesterday: Airflow 3.1.8, triggerer stuck in CrashLoopBackOff for ~10h on a DatabricksExecutionTrigger whose failed task's error output carried a raw NUL byte from a Spark stack trace ("col" was called from <command--1> — a column-not-found AnalysisException). Confirmed make_jsonb_safe() on extract_failed_task_errors/_async would have prevented that specific crash. Nice, tight fix.
One gap I'd flag before merge, though: RunState.to_json() (hooks/databricks.py:147) does json.dumps(self.__dict__) on state_message with no sanitization, and DatabricksExecutionTrigger embeds that string verbatim as the "run_state" key of the same TriggerEvent — at triggers/databricks.py:155, and again at :181/:208 in the workflow-task retry/terminal branches. state_message is the exact same Databricks free-text field this PR already treats as untrusted (it's the error fallback here) — so a NUL byte or lone surrogate landing in state_message instead of run_output["error"] reproduces the identical crash, entirely bypassing make_jsonb_safe():
>>> import json
>>> json.dumps({"state_message": "boom \u0000 more"})
'{"state_message": "boom \\u0000 more"}'That \u0000 escape survives nested inside the "run_state" string, and hits the same Postgres UntranslatableCharacter once the outer payload lands in next_kwargs (jsonb) — same failure mode, different field, and it ships unfixed even after this PR merges.
Worth noting this isn't unique to RunState either — ClusterState.to_json() and SQLStatementState.to_json() (same file) follow the identical state_message → unsanitized json.dumps(self.__dict__) pattern, so the same class of bug likely reaches other Databricks triggers too.
Given that, I'd suggest sanitizing at the source — inside to_json() itself (or wherever state_message gets assigned) — rather than only at the two extract_failed_task_errors call sites, so every caller gets the protection instead of just this one. Doesn't need to block this PR if you'd rather land the reported-crash fix now and follow up separately, but I'd hate to see this merge as "the NUL byte fix" and have the next crash report be the same root cause from a different field.
Drafted-by: Claude Code (Sonnet 5); reviewed by @seanmuth before posting
|
The production case is useful — an independent report with a real The second hole is not there, though, and I checked rather than reasoned. I replayed both payload shapes into a real Postgres 16.13
The double encoding is what saves it. I suspect the repro is what led there: in Python One thing does remain, and it is not this: So this looks right to me as it stands, and my approval holds. Drafted-by: Claude Code (Opus 5); reviewed by @moomindani before posting |
|
Is there anything blocking this from a merge? :-) |
|
Nothing from my side — the approval stands, and the rebase I flagged in my merge-order note is not needed. #72313 landed since then, and the two changes sit on different lines: it rewrote the loop header ( So it is waiting on a committer to merge. Drafted-by: Claude Code (Opus 5); reviewed by @moomindani before posting |
|
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed in 5 days if no further activity occurs. Thank you for your contributions. |
|
@eladkal , Can we merge this sometime? At my company, we will need this feature.. Or is there anything else required from my side? |
…in jsonb Databricks run output is arbitrary external text and can contain NUL bytes or unpaired UTF-16 surrogates (e.g. when a task emits binary data). A failed task's error is placed in the DatabricksExecutionTrigger event and persisted by Airflow into the deferred task instance's next_kwargs (a jsonb column). Postgres jsonb rejects those characters, so the triggerer crashes on write (psycopg2 UntranslatableCharacter) and, because the invalid event is retried, enters a CrashLoopBackOff that stalls all deferrable tasks. Strip these characters from the extracted error in both the sync and async extract_failed_task_errors helpers before it is emitted in the event. Signed-off-by: Fabian Bücheler <fabian.bucheler@getyourguide.com> Co-authored-by: Cursor <cursoragent@cursor.com>
change comment
Signed-off-by: Fabian Bücheler <fabian.bucheler@getyourguide.com> Co-authored-by: Cursor <cursoragent@cursor.com>
f1bb073 to
89a7a43
Compare
|
Followup on my 9/4 review: I'm retracting the I also re-checked the head after the 11/05 rebase. The PR's own changes ( |
What
Strip characters that Postgres
jsonbcannot store (NUL bytes and unpaired UTF-16 surrogates) from a failed Databricks task's error text, in bothextract_failed_task_errorsandextract_failed_task_errors_async.Why
DatabricksExecutionTriggerputs each failed task's error into itsTriggerEventwhich gets persisted in the deferred task instance'snext_kwargs, which is a Postgresjsonbcolumn. Since Databricks output can contain NUL bytes, postgres rejects this with an error like this:Because the invalid event is retried, the triggerer enters a
CrashLoopBackOffwhich makes it unhealthy.How
A small
make_jsonb_safe()helper regex-strips\x00and\ud800-\udffffrom the error string (non-string values pass through unchanged). It is applied to the extracted error in both the sync and async helpers, so the emitted trigger event is always persistable.Tests
Added unit tests in
providers/databricks/tests/unit/databricks/utils/test_databricks.py:make_jsonb_safecases (NUL strip, unpaired-surrogate strip, clean-text passthrough, non-string passthrough)extract_failed_task_errorsandextract_failed_task_errors_asyncsanitize error outputWas generative AI tooling used to co-author this PR?
Generated-by: Cursor
Made with Cursor