Skip to content

Fix superseding shared child events - #7162

Closed
harsh21234i wants to merge 5 commits into
reflex-dev:mainfrom
harsh21234i:harsh21234i-fix-chained-supersession
Closed

harsh21234i wants to merge 5 commits into
reflex-dev:mainfrom
harsh21234i:harsh21234i-fix-chained-supersession

Conversation

@harsh21234i

@harsh21234i harsh21234i commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #7041.

When two different superseding root events enqueue the same superseding child event, the second child event did not cancel the first because supersession was only applied to root-level enqueues. This could allow stale background work to continue running alongside the newer event.

This change applies the existing supersession-key logic to chained events while preserving existing behavior for non-superseding handlers.

Included:

  • Regression test covering distinct root chains sharing a superseding child event.

  • Documentation update for chained-event supersession behavior.

  • reflex-base changelog fragment.

Validation:

  • 45 event processor tests passed.

  • Changed-file pre-commit checks passed, including Ruff, codespell, pyright, ty, and the pyi hook.

Review in cubic

@harsh21234i
harsh21234i requested a review from a team as a code owner September 16, 2026 04:24
@harsh21234i
harsh21234i force-pushed the harsh21234i-fix-chained-supersession branch from 60a1a64 to d47f94f Compare September 16, 2026 04:25
@greptile-apps

greptile-apps Bot commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The implementation appears safe to merge; the only outstanding concern is the previously reported non-blocking changelog whitespace.

Findings

  1. P2 Changelog has trailing whitespace ▶

Summary

Fixes latest-wins handling for superseding handlers used as chained events.

  • Applies supersession checks to both root and chained enqueues.
  • Preserves self-chaining by avoiding cancellation when the previous keyed invocation is an ancestor.
  • Adds regression coverage for shared children, self-chaining, and sibling invocations.
  • Updates the public handler documentation and changelog fragment.

Reviews (3) · Last reviewed commit: "Document supersession enqueue ordering"

@@ -0,0 +1 @@
Ensure superseding event handlers shared by distinct event chains cancel stale child invocations.

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.

P2 Changelog has trailing whitespace

The added changelog line ends with trailing whitespace, which causes git diff --check to fail. Remove the trailing carriage-return or whitespace character so standard Git whitespace validation passes.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@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 harsh21234i:harsh21234i-fix-chained-supersession (f7e3d2b) with main (11628ea)

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

@FarhanAliRaza FarhanAliRaza 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 by exercising this branch in a real app (two roots returning a shared @rx.event(background=True, supersedes=True) child, a root-supersession pair, a self-chaining superseding poll loop, and a same-batch sibling yield), driven headless in Chromium, plus a static pass.

Verdict: LGTM — the fix does what it claims, with only minor non-blocking notes (inline).

Verified live on this branch:

  • The #7041 repro is fixed: clicking Event A then Event B while the shared child sleeps yields do_stuff(a) started → do_stuff(b) started → do_stuff(b) finished, with the stale a invocation cancelled mid-sleep. No console errors.
  • Root-level supersession is unchanged (two rapid clicks of the same superseding root → only the second finishes).
  • The _is_future_ancestor guard works: a self-chaining superseding handler runs its full chain to completion.
  • Plain (non-superseding) events unaffected.

Test/static evidence:

  • The new regression test fails on main's event_processor.py and passes with the fix — and the main failure mode also shows the old code leaked the _superseded tracking entry, which this change cleans up.
  • Full tests/units/reflex_base suite passes (443 passed, 1 skipped); pyright and ruff clean on the changed files; the touched functions stay under the C901 complexity threshold.
  • No component changes, so no pyi_hashes.json update needed; backend-only, no prod-build impact.

Inline comments: one suggested test to pin the (apparently intended) new sibling-yield cancellation semantics, one docstring suggestion for a latest-enqueue-wins edge case, and a news-fragment naming nit.


Generated by Claude Code

Comment on lines +591 to +594
key = (event.name, token)
previous = self._superseded.get(key)
if previous is not None and not previous.all_done():
is_ancestor = previous is not None and _is_future_ancestor(previous, tracked)
if previous is not None and not is_ancestor and not previous.all_done():

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.

Untested behavior change worth pinning with a unit test: because the supersession key is (event.name, token) and ignores args, a handler that yields the same superseding handler twice in one batch — e.g. return [State.do_other("x"), State.do_other("y")] — now has y cancel x before it even starts (verified in a live app on this branch; previously both ran, since chained enqueues never superseded). The siblings share a parent, so the ancestor check doesn't protect them.

This is consistent with latest-wins and with how roots already behave, so it looks intended — but it's downstream-visible beyond the #7041 scenario and no test covers it. Suggest adding a test asserting only the last sibling runs, so the semantics are locked in deliberately.


Generated by Claude Code


When True, enqueuing this handler cancels the previous unfinished
invocation chain for the same handler and client token. This also
applies when the handler is yielded by multiple distinct roots.

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.

Edge case that might deserve a sentence here: cancellation is latest-enqueue-wins, not latest-user-action-wins. Roots with different names don't supersede each other, so if the older root chain is still running and enqueues the shared superseding child after the newer chain already did, the stale chain's invocation cancels the newer one. Unlikely in practice (children are usually enqueued promptly), and inherent to the (event.name, token) key design — just worth documenting so the semantics aren't surprising.


Generated by Claude Code

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.

Nit: the +slug form is for before the PR number is known — this can be renamed to 7162.bugfix.md now.


Generated by Claude Code

@harsh21234i

Copy link
Copy Markdown
Contributor Author

Hi @FarhanAliRaza , I’ve addressed the review feedback on PR #7162 and pushed the latest changes. The final commit is f7e3d2b3. I also added regression coverage, updated the changelog, and verified that 51 targeted tests and pre-commit checks pass. The branch is ready for another review. Thanks!

@FarhanAliRaza

Copy link
Copy Markdown
Contributor

superceded by this #7168

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