Skip to content

push-gateway: lock expired installations before reaping - #9627

Merged
TheSentinel454 merged 1 commit into
mainfrom
elrond/push-gateway-reaper-lock-order
Nov 17, 2026
Merged

TheSentinel454 merged 1 commit into
mainfrom
elrond/push-gateway-reaper-lock-order

Conversation

@TheSentinel454

@TheSentinel454 TheSentinel454 commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

What

The push gateway reaper deletes expired delegations and then expired installations in two statements. A delegation writer (upsert_delegation) that holds the installation lock can commit a new child between them. With the foreign key, the installation delete then fails, and the reap transaction rolls back.

This PR locks the expired installations with FOR UPDATE first, as every authority mutation does. It then deletes their delegations and the installations by id. A writer that was holding the lock commits first, and the delete sees its child. A later writer finds the parent gone and is rejected.

No schema change. This is the code half of #9610. The gateway image with this change must run everywhere before the delegation foreign key is dropped.

Tests

Blox at 20b66f8f: fmt, clippy, and the push-gateway PostgreSQL tests (15/15) pass.

  • reaper_deletes_delegation_committed_while_it_waits: a writer holds the installation lock and inserts a child. The reaper starts and waits. The writer commits. The reaper deletes both rows.
  • delegation_for_missing_installation_is_rejected: upsert_delegation for an unknown installation returns Rejected and writes nothing.

What got simpler

The reaper now uses the same lock order as every other authority write. The child delete no longer needs a correlated subquery.

buzz-review-completed

🤖 Generated with Claude Code

The reaper deleted expired children and then expired parents in two
statements. A delegation writer that held the parent lock could commit
a new child between them. With the foreign key, the parent delete then
fails. Without it, the child would outlive its parent.

Lock the expired parents with FOR UPDATE first, as every authority
mutation does. Then delete their children and the parents by id.

This is the code change that must run everywhere before the delegation
foreign key can be dropped.

Co-authored-by: Elrond <28d6302a099e5225b02c4155ac4236e4912603df2ab08dbfc2f4fef08ce598c8@buzz.block.builderlab.xyz>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Luke Tornquist <tornquist@squareup.com>
@TheSentinel454
TheSentinel454 requested a review from a team as a code owner November 17, 2026 21:40
@github-actions github-actions Bot added the codex-security-review-current The posted Codex security review matches its recorded range. label Nov 17, 2026
@github-actions

Copy link
Copy Markdown

🔐 Codex Security Review

Note: This is an automated, security-focused review generated by Codex.
Use it as a supplement to human review; false positives are possible.

Scope

  • Exact PR diff: 051dd2d7aaae2cb6ce288cf55d571e9790fc38c2...20b66f8fa433348bb62c935047c553d4a93d3c05
  • Model: gpt-6.1-sol

💡 Click "edited" above to see earlier reviews for this PR.


Review Summary

Overall Risk: NONE

No concrete security, correctness, or reliability findings in the authorized PR range. The reaper now locks expired installations before deleting their delegations, matching the delegation writer’s lock order.

Findings

No concrete security, correctness, or reliability findings were identified.

Notes

  • Review used read-only source and history inspection. Builds, tests, and database concurrency checks were not executed, as instructed.

Generated by Codex Security Review |
Requested by: @TheSentinel454 |
Workflow run

@johnmatthewtennant johnmatthewtennant left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Reviewed exact head 20b66f8fa433348bb62c935047c553d4a93d3c05 with the complete 18/18 review, including Builderbot. No correctness blockers found. All 15 PostgreSQL tests passed, including the concurrent writer/reaper regression; the normal full gateway package run passed 95 tests. An exploratory run enabling every ignored test had 108 library passes and four failures for missing device, APNs, or rendered-Helm fixtures; those external-fixture checks remain unvalidated here.

@TheSentinel454
TheSentinel454 merged commit 44cdd9f into main Nov 17, 2026
90 checks passed
@TheSentinel454
TheSentinel454 deleted the elrond/push-gateway-reaper-lock-order branch November 17, 2026 22:18
TheSentinel454 added a commit that referenced this pull request Nov 17, 2026
The gateway already checks this reference in its own transactions:
upsert_delegation locks the installation FOR UPDATE and rejects a
missing or revoked one, the reaper locks expired installations before
it deletes them and their delegations (#9627), and delivery rejects a
delegation whose installation is gone.

Gateway migration 0008 drops the constraint. Relay migration 0096 drops
the relay's legacy copy (docs/database.md Rule 4); no relay code reads
or writes these tables.

Required stop: every gateway must run #9627 or later before this ships.

Co-authored-by: Elrond <28d6302a099e5225b02c4155ac4236e4912603df2ab08dbfc2f4fef08ce598c8@buzz.block.builderlab.xyz>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Luke Tornquist <tornquist@squareup.com>
wpfleger96 added a commit that referenced this pull request Nov 18, 2026
…c-spec-mobile

* origin/main: (37 commits)
  buzz-db: drop the five single-table guard triggers (BUZZ-318 phase 2) (#9398)
  push-gateway: lock expired installations before reaping (#9627)
  Retire the 0032 roster trigger in migration 0095 (#8696)
  fix(db): hard-delete superseded relay-signed lists (#9586)
  Add community-scoped user and agent profile cleanup (#9580)
  fix(sdk): timestamp channel messages with millisecond offsets (#9548)
  Check product_feedback's community in the writing transaction (#9608)
  buzz-db: stop calling the dropped lock-key function in SQL (#9615)
  Clean up account records and refresh member lists (#9364)
  perf(mobile): save own thread interest once per unread catch-up (#9614)
  buzz-db: fix main (renumber the function drop to 0094, update_channel test call) (#9616)
  feat(relay): name the agent whose add policy refuses a DM (#9601)
  buzz-db: drop the community_deletion_lock_key and profile_name_fields functions (contract) (#9598)
  fix(deps): clear chrono, humantime and zip advisories from the dependency policy (#9609)
  docs: define mobile diagnostic reporting (#9600)
  build(mobile): link iOS plugins through Swift Package Manager (#9597)
  feat(push-gateway): enroll Android installations and send FCM wakes (#9560)
  perf(mobile): cache and animate custom emoji in the iOS picker (#9479)
  fix(ios): end each bold or strikethrough run at its first closing marker (#9559)
  Apply the private-to-open rule in update_channel too (#9581)
  ...

Signed-off-by: Will Pfleger <wpfleger@block.xyz>
wpfleger96 added a commit that referenced this pull request Nov 18, 2026
…e-recovery

* origin/main:
  feat(cli)!: consistent channels list/get/search output (type, visibility, labels, real created_at) (#9617)
  ci(docker): publish the push gateway only when its inputs change (#9658)
  feat(mobile): reply to a message from its iOS notification (#9439)
  fix(mobile): stop reporting saved images as failed on iOS (#9599)
  perf(db): stop per-id planning of wide #e reads (#9591)
  [buzz] fix(ci): link relay image indexes to their source (#9637)
  feat(push-gateway): verify Android Key Attestation (#9562)
  fix(relay): remove an owner's agents with the owner (#9575)
  relay: refuse a reaction or deletion whose h names another channel (#9006)
  buzz-db: drop the five single-table guard triggers (BUZZ-318 phase 2) (#9398)
  push-gateway: lock expired installations before reaping (#9627)
  Retire the 0032 roster trigger in migration 0095 (#8696)
  fix(db): hard-delete superseded relay-signed lists (#9586)
  Add community-scoped user and agent profile cleanup (#9580)
  fix(sdk): timestamp channel messages with millisecond offsets (#9548)
  Check product_feedback's community in the writing transaction (#9608)
  buzz-db: stop calling the dropped lock-key function in SQL (#9615)

Signed-off-by: Will Pfleger <wpfleger@block.xyz>
TheSentinel454 added a commit that referenced this pull request Nov 19, 2026
The gateway already checks this reference in its own transactions:
upsert_delegation locks the installation FOR UPDATE and rejects a
missing or revoked one, the reaper locks expired installations before
it deletes them and their delegations (#9627), and delivery rejects a
delegation whose installation is gone.

Gateway migration 0008 drops the constraint. Relay migration 0101 drops
the relay's legacy copy (docs/database.md Rule 4); no relay code reads
or writes these tables.

Required stop: every gateway must run #9627 or later before this ships.

Co-authored-by: Elrond <28d6302a099e5225b02c4155ac4236e4912603df2ab08dbfc2f4fef08ce598c8@buzz.block.builderlab.xyz>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Luke Tornquist <tornquist@squareup.com>
TheSentinel454 added a commit that referenced this pull request Nov 19, 2026
## Summary

Retire the last foreign key in the push gateway schema:
`push_gateway_delegations.installation_id →
push_gateway_installations(id)`. This follows `docs/database.md` Rule 2
and the relay FK retirement work (#9399, #9404).

- **Gateway migration 0008** drops the constraint.
- **Relay migration 0101** drops the relay's legacy copy (Rule 4:
gateway schema changes go in both).
`schema/tables/public/push_gateway_delegations.sql` loses the inline
`REFERENCES`.

## Required stop

Every gateway and rollback target must run #9627 (`44cdd9fe`) or later
before this merges and deploys. #9627 makes the reaper lock expired
installations before it deletes their delegations. Without the FK, an
older reaper can leave an orphaned delegation. This is step 2 of the
Rule 5 retirement sequence; #9627 is step 1.

## Why the app already enforces the reference

| Path | Check |
|---|---|
| `upsert_delegation` (only delegation insert) | Locks the installation
`FOR UPDATE` in the same transaction; rejects a missing or revoked one.
|
| `create_installation` (replaces expired installations) | Locks the
matching installations `FOR UPDATE`, deletes their delegations, then the
installations. |
| `reap_expired` | Locks expired installations `FOR UPDATE`, deletes
their delegations, then the installations (#9627). |
| `authorize_delivery` | Reads the installation through the delegation;
a missing installation is `Rejected`. |
| `revoke_delegation` | Updates the delegation only; an orphan has no
effect. |
| `revoke_installation`, `rotate_endpoint`, `advance_assertion_counter`
| Update the installation; they never delete it. |

The relay never reads or writes the `push_gateway_*` tables (the gateway
requires its own database), so dropping the relay copy changes no relay
behavior.

## Tests

The gateway tests run against the migrated schema, which no longer has
the FK, so they prove the app check.

- `migrated_schema_has_no_foreign_keys` (new): the gateway schema has no
foreign keys after migrations. It fails if gateway migration 0008 is
removed.
- `delegation_for_missing_installation_is_rejected` and
`reaper_deletes_delegation_committed_while_it_waits` (from #9627) now
run without the FK.
- `reaper_retains_revocation_tombstones_until_authority_expiry`: its
hand-built schema drops the FK too.
- Relay: `push_gateway_delegations` joins the constraint parity check in
`admin_schema_parity_between_desired_state_and_migrations`, and the
migration count test covers 0101.

## Validation

Blox at `33f7df90` (before the rename to 0101; CI covers the rebased
head):

- `cargo fmt --check`, clippy (`buzz-push-gateway`, `buzz-db`, all
targets), gateway unit tests, `buzz-db` migration unit tests: pass.
- `scripts/postgres-test-run.sh -p buzz-push-gateway --lib --tests`:
16/16 pass.
- `buzz-db` Postgres tests for migrations, parity, push, floor
retirement and retained guards: 56/56 pass, including
`admin_schema_parity_between_desired_state_and_migrations`.
- Must-fail check: with gateway migration 0008 removed,
`migrated_schema_has_no_foreign_keys` fails (1 foreign key vs 0).

## Coordination

- On main after #8696, #9398 and #9710 (relay 0095–0100). #9627 shipped
this PR's reaper fix and tests separately, so this PR is the schema drop
only.
- Renumbered to relay 0101 after #9710 took 0098–0100 (rename only; file
contents unchanged). #9690 and #9621 also add a relay 0098; whichever
merges second renumbers.
- #8328 adds the gateway's declarative schema with the inline FK.
Whichever PR merges second removes it there.

## What got simpler

One fewer database object. Every gateway reference check now lives in
one place: the gateway's own transactions.

buzz-review-completed

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Signed-off-by: Luke Tornquist <tornquist@squareup.com>
Co-authored-by: Elrond <28d6302a099e5225b02c4155ac4236e4912603df2ab08dbfc2f4fef08ce598c8@buzz.block.builderlab.xyz>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

This branch was previously deployed

1 inactive deployment
codex-review — 20b66f8f Deployed Nov 17, 2026 by TheSentinel454 via Run Codex Security Review #15840
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

codex-security-review-current The posted Codex security review matches its recorded range.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants