Skip to content

Order superseding event cancellation by root generation - #7168

Merged
masenf merged 2 commits into
mainfrom
farhan/pensive-galileo-z2arcf
Sep 17, 2026
Merged

masenf merged 2 commits into
mainfrom
farhan/pensive-galileo-z2arcf

Conversation

@FarhanAliRaza

@FarhanAliRaza FarhanAliRaza commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

All Submissions:

  • Have you followed the guidelines stated in CONTRIBUTING.md file?
  • Have you checked to ensure there aren't any other open Pull Requests for the desired changed?

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Changes To Core Features:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your core changes, as applicable?
  • Have you successfully ran tests with your changes locally?

Closes #7041.

Superseding handlers only cancelled prior invocations when enqueued as chain roots, so two different chains yielding the same superseding handler left the stale invocation running alongside the newer one.

This is a standalone alternative to #7162: instead of applying enqueue-order supersession to child events with an ancestor-walk exception, it orders supersession by root generation.

The model: each root enqueue receives a monotonic stamp (EventFuture.root_gen); chained events inherit their parent's stamp. _superseded tracks the live invocations of the newest generation per (event name, token), and _supersede_previous becomes a three-way comparison:

  • Newer generation → cancel every live invocation of the older generation (the Supersedes does not cancel events from different chains. #7041 fix).
  • Same generation → coexist: self-chaining superseding handlers keep working, and sibling fan-out from one parent (return [refresh("x"), refresh("y")]) runs all siblings.
  • Older generation (a stale, still-running chain enqueues the shared handler after a newer chain already has) → the stale invocation is dropped at enqueue instead of cancelling the newer chain's work.

This encodes "newest user action wins" in the data model: no O(depth) ancestor walk (an O(1) integer compare instead), no special case for self-chains, and the stale-late-enqueue inversion becomes impossible rather than a documented caveat.

Included:

  • Regression tests that fail on main: a shared superseding child cancelled across distinct root chains, and a stale chain's late enqueue being dropped without cancelling newer work.
  • Behavior-pinning tests: self-chaining superseding handlers continue, and sibling invocations from one parent all run.
  • reflex-base changelog fragment and docstring updates for the new semantics.

Validation:

  • All 52 event-processor tests pass; 644 across tests/units/reflex_base + tests/units/test_state.py.
  • Both regression tests verified to fail against main's event processor.
  • Verified live in a real app driven by headless Chromium: the Supersedes does not cancel events from different chains. #7041 repro (two roots sharing a superseding background child) is fixed, root supersession and a self-chaining superseding poll loop (including mid-run restart) behave, sibling fan-out runs both invocations, no console errors.
  • Ruff, ruff format, and pyright clean on the changed files.

Known trade-off: stale-drop protection lasts while the newer chain is still tracked; after its supersession slot is pruned, a very late stale enqueue runs as fresh work. Remembering the max generation per key forever would grow unboundedly, and pruning still strictly improves on enqueue-order semantics, where the stale enqueue cancels the newer work.

@FarhanAliRaza
FarhanAliRaza requested a review from a team as a code owner September 16, 2026 20:19
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 16, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-16T20:24:58.986367Z 780f54d PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@greptile-apps

greptile-apps Bot commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The PR appears safe to merge; the completed-parent adjustment fixes the previous cancellation gap without introducing a demonstrated new failure.

Summary

This PR changes superseding event cancellation to order invocations by the generation of their root event.

  • Chained superseding handlers inherit their root generation.
  • Newer generations cancel tracked invocations from older generations, while same-generation invocations coexist.
  • Late invocations attached to completed parents register independently so that future generations can still cancel them.
  • Regression tests cover cross-chain cancellation, stale late enqueueing, self-chaining, sibling fan-out, and the previously reported late-invocation case.
  • The previous finding is fully addressed and its thread is resolved.

Reviews (4) · Last reviewed commit: "Keep late-chained superseding invocation..."

@cubic-dev-ai cubic-dev-ai Bot 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.

All reported issues were addressed across 6 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

@codspeed

codspeed Bot commented Sep 16, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 52 untouched benchmarks
⏩ 8 skipped benchmarks1


Comparing farhan/pensive-galileo-z2arcf (7917840) with main (1bc8936)

Open in CodSpeed

Footnotes

  1. 8 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@cubic-dev-ai cubic-dev-ai Bot 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.

All reported issues were addressed

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 780f54ded0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@FarhanAliRaza
FarhanAliRaza force-pushed the farhan/pensive-galileo-z2arcf branch 4 times, most recently from 6adc4c9 to 789f8ae Compare September 16, 2026 20:32
Superseding handlers previously only cancelled prior invocations when
enqueued as chain roots, so two different chains yielding the same
superseding handler left the stale invocation running (#7041).

Give each root enqueue a monotonic generation stamp that chained events
inherit, and apply supersession on every enqueue as a generation
comparison: a newer chain's invocation cancels all live invocations
from older chains, invocations within one chain (self-chains and
sibling fan-out from one parent) coexist, and a stale chain enqueuing
the handler after a newer chain already has is dropped at enqueue
instead of cancelling the newer work.

Fixes #7041
@FarhanAliRaza
FarhanAliRaza force-pushed the farhan/pensive-galileo-z2arcf branch from 789f8ae to 3bf5ff5 Compare September 16, 2026 20:46
An invocation late-chained to a completed parent is not attached to the
parent's cancellation tree, but it still inherited the covered flag and
skipped supersession registration, so a newer generation could never
cancel it. Inherit coverage only from a still-running parent, so a
late-chained invocation registers itself in the slot.

Also qualify the supersedes docstring: stale enqueues are dropped while
the newer chain's invocations are still tracked.

@masenf masenf left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

minor issue where an rx.call_script callback fires after some delay, which could end up cancelling later scheduled work with stale results. not going to block on that though, it seems like it would be complex and difficult to track the generation number for a frontend-executing event like call_script

@masenf
masenf merged commit aaa44a0 into main Sep 17, 2026
134 of 135 checks passed
@masenf
masenf deleted the farhan/pensive-galileo-z2arcf branch September 17, 2026 00:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Supersedes does not cancel events from different chains.

2 participants