Skip to content

Do not corrupt XCom values that already parse as JSON in the bytea to JSONB migration (#71926) - #72886

Merged
vatsrahul1001 merged 2 commits into
v3-3-testfrom
backport-xcom-migration-chain-v3-3-test
Sep 10, 2026
Merged

vatsrahul1001 merged 2 commits into
v3-3-testfrom
backport-xcom-migration-chain-v3-3-test

Conversation

@vatsrahul1001

Copy link
Copy Markdown
Contributor

Backport of #71926 to v3-3-test for the 3.3.2 release, including its prerequisite #69064.

#71926 depends on #69064 ("Guard XCom/DagRun pickle-removal migrations 0049/0055 against NaN/Infinity and U+0000"), which was never backported to the 3.3 line (no milestone). Cherry-picking #71926 alone onto v3-3-test conflicted (229-line divergence in 0049) because #69064 was missing. With #69064 applied first, both cherry-pick cleanly.

Both commits are included in chronological order:

  • #69064 — guard the bytea→JSONB conversion against invalid bytes (NaN/Infinity/U+0000)
  • #71926 — do not corrupt XCom values that already parse as JSON (the target fix)

Verification: the resulting migration SQL (0049/0055) is byte-for-byte identical to main (0 diff), i.e. exactly the SQL main's CI validated on Postgres and MySQL. ruff clean. The @pytest.mark.backend("postgres") / ("mysql") migration tests included in these PRs run in this PR's CI DB matrix.

Scope note: these are already-shipped 3.0.0 migrations, so the fix benefits deployments that have not yet run 0049/0055 (fresh installs / upgrades from pre-3.0); it does not re-run for deployments already on 3.x.

Was generative AI tooling used to co-author this PR?
  • Yes — Claude Code (Opus 4.8)

Generated-by: Claude Code (Opus 4.8) following the guidelines

seanmuth and others added 2 commits September 10, 2026 19:24
Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
(cherry picked from commit 54f4300)
… JSONB migration (#71926)

* Substitute null for non-finite floats in the XCom bytea to JSONB migration

The sanitizer in 0049 wrapped NaN/Infinity/-Infinity in unescaped double
quotes, which is only valid at the top level of a JSON document. When the
XCom value is a JSON string wrapping another JSON document every interior
quote is backslash-escaped, so the injected quote closes the wrapping
string and the bytea -> JSONB cast aborts. null needs no quoting, so it is
correct at any nesting depth.

Also moves the run of spaces after the delimiter inside group 1 in the
MySQL pattern, matching PostgreSQL, so the replacement stops dropping it.

closes: #71921

* Skip the non-finite float rewrite for XCom values that already parse as JSON

If a value parses as JSON, any NaN or Infinity in it can only be inside a
string, so the rewrite has nothing to fix and the value is left byte for
byte alone. That is the shape reported in #71921 and it means the migration
no longer edits the contents of a JSON string at all.

The sanitizer is now two statements per dialect. The U+0000 strip runs
first and unconditionally, only on rows that contain the escape; the
non-finite rewrite runs second, only on rows that do not parse. The order
matters: a value invalid only because of the escape would otherwise look
like it needs the rewrite and be corrupted by it.

Rows that do need the rewrite still get bare null, since a value can be
invalid at the top level and also wrap an escaped document, and only null
is valid at every nesting depth.

PostgreSQL has no non-throwing JSON validator before 16 and 14 is still
supported, so the check goes through a session-local pg_temp function with
a cheap regex pre-test. MySQL and SQLite use the native JSON_VALID, with a
JSON1 probe and an unguarded fallback on SQLite as in migration 0117. The
guard also shrinks the documented SQLite false positive, since a value
holding "NaN detected" now parses and is skipped.

closes: #71921

* Narrow the SQLite JSON1 probe to OperationalError in migration 0049

A missing json_valid() raises OperationalError. Catching every exception
also swallowed connection failures and silently sanitized without the
guard, rewriting rows that already parse.

(cherry picked from commit 6b60399)
@boring-cyborg boring-cyborg Bot added the area:db-migrations PRs with DB migration label Sep 10, 2026
@vatsrahul1001 vatsrahul1001 added this to the Airflow 3.3.2 milestone Sep 10, 2026
@vatsrahul1001 vatsrahul1001 added the type:bug-fix Changelog: Bug Fixes label Sep 10, 2026
@vatsrahul1001
vatsrahul1001 merged commit 1ab3d9f into v3-3-test Sep 10, 2026
74 of 75 checks passed
@vatsrahul1001
vatsrahul1001 deleted the backport-xcom-migration-chain-v3-3-test branch September 10, 2026 15:30
kravii pushed a commit to acceldata-io/airflow that referenced this pull request Nov 19, 2026
… JSONB migration (apache#71926) (apache#72886)

* Guard JSONB migration against invalid bytes conversion (apache#69064)

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
(cherry picked from commit 54f4300)

* Do not corrupt XCom values that already parse as JSON in the bytea to JSONB migration (apache#71926)

* Substitute null for non-finite floats in the XCom bytea to JSONB migration

The sanitizer in 0049 wrapped NaN/Infinity/-Infinity in unescaped double
quotes, which is only valid at the top level of a JSON document. When the
XCom value is a JSON string wrapping another JSON document every interior
quote is backslash-escaped, so the injected quote closes the wrapping
string and the bytea -> JSONB cast aborts. null needs no quoting, so it is
correct at any nesting depth.

Also moves the run of spaces after the delimiter inside group 1 in the
MySQL pattern, matching PostgreSQL, so the replacement stops dropping it.

closes: apache#71921

* Skip the non-finite float rewrite for XCom values that already parse as JSON

If a value parses as JSON, any NaN or Infinity in it can only be inside a
string, so the rewrite has nothing to fix and the value is left byte for
byte alone. That is the shape reported in apache#71921 and it means the migration
no longer edits the contents of a JSON string at all.

The sanitizer is now two statements per dialect. The U+0000 strip runs
first and unconditionally, only on rows that contain the escape; the
non-finite rewrite runs second, only on rows that do not parse. The order
matters: a value invalid only because of the escape would otherwise look
like it needs the rewrite and be corrupted by it.

Rows that do need the rewrite still get bare null, since a value can be
invalid at the top level and also wrap an escaped document, and only null
is valid at every nesting depth.

PostgreSQL has no non-throwing JSON validator before 16 and 14 is still
supported, so the check goes through a session-local pg_temp function with
a cheap regex pre-test. MySQL and SQLite use the native JSON_VALID, with a
JSON1 probe and an unguarded fallback on SQLite as in migration 0117. The
guard also shrinks the documented SQLite false positive, since a value
holding "NaN detected" now parses and is skipped.

closes: apache#71921

* Narrow the SQLite JSON1 probe to OperationalError in migration 0049

A missing json_valid() raises OperationalError. Catching every exception
also swallowed connection failures and silently sanitized without the
guard, rewriting rows that already parse.

(cherry picked from commit 6b60399)

---------

Co-authored-by: Sean Muth <[email protected]>
Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Co-authored-by: Hemkumar Chheda <[email protected]>
(cherry picked from commit 1ab3d9f)
senthh pushed a commit to acceldata-io/airflow that referenced this pull request Nov 23, 2026
… JSONB migration (apache#71926) (apache#72886)

* Guard JSONB migration against invalid bytes conversion (apache#69064)

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
(cherry picked from commit 54f4300)

* Do not corrupt XCom values that already parse as JSON in the bytea to JSONB migration (apache#71926)

* Substitute null for non-finite floats in the XCom bytea to JSONB migration

The sanitizer in 0049 wrapped NaN/Infinity/-Infinity in unescaped double
quotes, which is only valid at the top level of a JSON document. When the
XCom value is a JSON string wrapping another JSON document every interior
quote is backslash-escaped, so the injected quote closes the wrapping
string and the bytea -> JSONB cast aborts. null needs no quoting, so it is
correct at any nesting depth.

Also moves the run of spaces after the delimiter inside group 1 in the
MySQL pattern, matching PostgreSQL, so the replacement stops dropping it.

closes: apache#71921

* Skip the non-finite float rewrite for XCom values that already parse as JSON

If a value parses as JSON, any NaN or Infinity in it can only be inside a
string, so the rewrite has nothing to fix and the value is left byte for
byte alone. That is the shape reported in apache#71921 and it means the migration
no longer edits the contents of a JSON string at all.

The sanitizer is now two statements per dialect. The U+0000 strip runs
first and unconditionally, only on rows that contain the escape; the
non-finite rewrite runs second, only on rows that do not parse. The order
matters: a value invalid only because of the escape would otherwise look
like it needs the rewrite and be corrupted by it.

Rows that do need the rewrite still get bare null, since a value can be
invalid at the top level and also wrap an escaped document, and only null
is valid at every nesting depth.

PostgreSQL has no non-throwing JSON validator before 16 and 14 is still
supported, so the check goes through a session-local pg_temp function with
a cheap regex pre-test. MySQL and SQLite use the native JSON_VALID, with a
JSON1 probe and an unguarded fallback on SQLite as in migration 0117. The
guard also shrinks the documented SQLite false positive, since a value
holding "NaN detected" now parses and is skipped.

closes: apache#71921

* Narrow the SQLite JSON1 probe to OperationalError in migration 0049

A missing json_valid() raises OperationalError. Catching every exception
also swallowed connection failures and silently sanitized without the
guard, rewriting rows that already parse.

(cherry picked from commit 6b60399)

---------

Co-authored-by: Sean Muth <[email protected]>
Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Co-authored-by: Hemkumar Chheda <[email protected]>
(cherry picked from commit 1ab3d9f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:db-migrations PRs with DB migration type:bug-fix Changelog: Bug Fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants