Skip to content

After a Render replaces the caller, its delayed wait=False actions run even after a newer result #307

Closed

Description

@JuroOravec

What happens

A handler replaces its component with a different one, and schedules a
delayed action that does not hold up the rest of the list:

class Events:
    def save(self, data: FormIn):
        save_form(data)
        return [
            actions.Render(SavedPanel()),
            actions.Dispatch("Form:saved", delay=3, wait=False),
        ]

Within those 3 seconds the user clicks something in SavedPanel, and
the bridge accepts that call's result. When the 3 seconds are up, the
first call's Form:saved event still fires, now on SavedPanel, after
the newer result. A "saved" toast, for example, appears over whatever
the newer result showed.

The Events spec says a delayed action should be dropped here.
packages/protocol/events/v1/spec.md ("Order and timing", line 695)
says that before a delayed action runs, the browser client "first checks
that the calling component is still mounted and that no newer result
for it has been applied; otherwise it skips a non-blocking action and
fires citry:events:stale."

Why

The bridge keeps a record for each mounted component instance, and
counts the results it accepted there (acceptedEpoch). When the Render
replaces the caller, the bridge retires the old record and marks the
call acceptedRemount (its own Render replaced the caller). The call's
later actions go to remountTimers (line 444). When they fire,
continuationCurrent compares against the old record's count only.
SavedPanel's results raise the count in its own new record, so nothing
tells the old delayed action that a newer result arrived.

The comment above remountTimers says on purpose that a call's later
actions still run after its own Render replaced the caller. The test
"delayed actions after a render that remounted the sender still run"
locks that in for the case where no newer result arrives. Neither covers
the case where one does.

The spec does not match the code in either case. Read strictly, its
"still mounted" clause drops every delayed action after a replacement,
because the old caller is never mounted again, which is the behavior
that test says must not happen.

A delayed State refresh aimed at the caller is not affected: it names
the old render ID, which no longer exists after the replacement, so the
host ignores it. One aimed at another component still applies late.

Reproduction

Found with a TLA+ model of the bridge (see #308) and confirmed
with this test against the bridge source at 05db9ce6b. It fails with
actual: ['newer result applied', 'event:old:followRemount=true']:

test("remounted call's delay drops after newer result", async () => {
  const descriptor = {
    componentClassId: "Board_1",
    eventHandlers: { move: { httpMethod: "POST", usesState: true } },
  };
  const record = [];
  const transaction = {};
  let generation = 1;
  let bridge;
  const host = {
    ...basicHost((source) =>
      source.generation === generation
        ? { ...contextFor(`server_${generation}`), descriptor }
        : null,
    ),
    async prepareRender() {
      return { transaction };
    },
    abortRender() {},
    async commitRender() {
      bridge.retire({ stableId: "board", generation: 1 }, transaction);
      generation = 2;
    },
    dispatchEvent(name, _detail, _source, followRemount) {
      record.push(`event:${name}:followRemount=${followRemount}`);
    },
  };
  let calls = 0;
  bridge = bridgeModule.createVueEventsBridge({
    endpoint: "/events",
    host,
    fetch: async (_url, init) => {
      calls += 1;
      const envelope = JSON.parse(init.body);
      return resultResponse(
        envelope,
        calls === 1
          ? [
              remountingRender,
              {
                action: "event",
                eventName: "old",
                delay: 0.05,
                wait: false,
              },
              { action: "data", value: 1 },
            ]
          : [{ action: "data", value: 2 }],
      );
    },
  });
  await bridge.send({
    source: { stableId: "board", generation: 1 },
    handler: "move",
  });
  // The new component sends a call; its result is applied
  // before the old timer fires.
  const value = await bridge.send({
    source: { stableId: "board", generation: 2 },
    handler: "move",
  });
  assert.equal(value, 2);
  record.push("newer result applied");
  await new Promise((resolve) => setTimeout(resolve, 80));
  assert.deepEqual(record, ["newer result applied"]);
});

basicHost, contextFor, resultResponse and remountingRender are
the helpers in packages/js/citry-client/test/vue-events.test.mjs.

Fix direction

Both options need a spec change, because the "still mounted" clause has
to allow the replacement case either way:

  1. Drop after a newer result. Also count accepted results for the
    spot on the page where the component is mounted (stableId), and
    have a delayed action from a replacing call check that count. Add
    the replacement exception to the spec for the case with no newer
    result. The existing replacement tests still pass, because they
    apply no newer result.
  2. Always run. A delayed Redirect(delay=5, wait=False) after a
    "saved" screen is a reasonable pattern that should survive the user
    clicking in the new component. Then rewrite the spec paragraph to say
    that after a call's own Render replaced the caller, its delayed
    actions always run, and update the epoch row of the stale reasons
    table in docs_site/content/reference/browser-apis.md.

Option 1 seems safer for events and URL changes. A redirect may deserve
option 2, so the answer could differ by action type.

Activity

  1. added
    bug
    javascript
    on Oct 24, 2026
  2. added 2 commits that reference this issue on Oct 24, 2026
    5123d91
    db39fe8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugjavascript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions