Skip to content

fix(shell): cancel catalog handshake when its frame unregisters - #335

Open
jgindin wants to merge 5 commits into
pr8-restore-v0-9-renderersfrom
pr9-cancel-handshake-on-unregister
Open

jgindin wants to merge 5 commits into
pr8-restore-v0-9-renderersfrom
pr9-cancel-handshake-on-unregister

Conversation

@jgindin

@jgindin jgindin commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Description

Summary

This PR cancels an in-flight catalog handshake at the moment its frame is unregistered. It replaces the workaround from #334, which let the 5 s watchdog run and then suppressed its error if the handshake window was no longer registered.

  • What changes for users: on /demos, when a card is evicted before it answers GET_CATALOG, isHandshakeInProgress() returns to false and handshakeState() to 'settled' immediately, instead of up to 5 s later. No error is reported, as before.
  • When the handshake is cancelled: only when a live catalog is already established for the current renderer URL, meaning a live A2UI_CATALOG handshake succeeded for that URL since the last URL change. A cache-only catalog or an earlier failed handshake does not count.
  • Second intended change: a RENDERER_READY that is processed after its frame was already unregistered no longer starts a handshake, under the same live-catalog condition (see Changes).
  • One behavior difference from #334: if a registered iframe element's window is replaced in place (the element is registered again, or a new document announces itself in it) while a handshake is waiting on the old window, and the new document never announces RENDERER_READY within the watchdog period, the watchdog error is now reported. #334 stayed silent there, because at timer time the old window was no longer registered. No unregister notification exists for that case, since the element is still registered. When the new document does announce, the existing takeover branch handles it as before.
  • What stays exactly as before: in every other case (first handshake, cache-only catalog, prior failed handshake, renderer URL change, a different window unregistered) the 5 s watchdog, its error and the takeover by a replacement frame are unchanged. A dead frame with no live catalog still waits 5 s and still reports Watchdog timeout: A2UI_CATALOG not received within 5 seconds.
  • Untouched: the generation + URL snapshot guard (isStaleContinuation), the source-window guard in handleCatalogEnvelope, the priorWindowDetached takeover branch, CATALOG_WATCHDOG_TIMEOUT_MS and CATALOG_WATCHDOG_MESSAGE.
  • Depends on #325's handshake-window fix (887fcb08): the cancel matches a handshake by its window, and before that fix a handshake started right after a renderer URL change lost its window. That ordering issue was listed here as a known limit in an earlier draft; it is now fixed in #325.

Changes

  • HostCommunication.windowUnregistered$ (shell/src/app/shell/host-communication/host-communication.ts): a plain Subject<Window> exposed read-only, like sendToServer$. unregisterIframe emits the window as its last step, only if the window was registered before the call and is not registered after it. A window still held by another registration, an unknown target and a repeated unregistration do not emit. registerIframe(null) emits each window the registry held. A window replaced in place on a still-registered element does not emit. The subject completes in ngOnDestroy.
  • Cancel on unregister (shell/src/app/storage/catalog-management/catalog-management.ts): CatalogManagement subscribes next to its messageStream$ subscription, outside the concatMap, with takeUntilDestroyed() and the handler in untracked. If the emitted window is the handshake window, a handshake is in progress and a live catalog is established for the current URL, the timer is cleared, the handshake window is dropped and the state settles. The live-catalog check calls syncResolutionGeneration() first, so a catalog from the previous renderer URL does not count.
  • Skip a handshake for a frame that is already gone (handleRendererReady): a RENDERER_READY can wait in the concatMap behind an asynchronous catalog commit. If its frame is unregistered meanwhile, the notification has already passed. The handshake is therefore not started when the announcement has a source window, that window is not registered, and a live catalog is established for the current URL. A RENDERER_READY without a source window, and any announcement when no live catalog is established, start a handshake as before.
  • Removed: the suppression branch in the watchdog callback, its TODO(follow-up), and the {settle} option of resetHandshakeWatchdog. _hasEstablishedLiveCatalog stays; it is now read by the two places above.

Tests Whose Setup Changed / Redundant Specs Dropped

Four existing specs in catalog-management.spec.ts modelled an unregistration only by changing what the isRegisteredWindow double returns. They now call one spec helper, unregisterWindow, which removes the window from the set and emits windowUnregistered$, as the real HostCommunication does. Each change is one line (registered.delete(w) -> unregisterWindow(registered, w)); no assertion changed:

  • does not swallow watchdog error when a prior handshake failed and a subsequent frame detaches
  • completes a subsequent RENDERER_READY to A2UI_CATALOG handshake normally after a silent detached clear
  • logs watchdog error after A -> B -> A renderer URL switches without a fresh live handshake
  • logs watchdog error when catalog is hydrated only from IndexedDB cache and frame detaches

Two earlier #334 detached-window specs (cancels stale watchdog without error when a secondary frame detaches after catalog receipt and still fires watchdog error if an unregistered initial frame times out before any catalog receipt) were dropped in 0029c5d9 because every post-unregistration assertion in them was a strict subset of the new specs below (and the kept spec checks activeCatalog()?.catalogId in setup): clears the handshake at once when its window is unregistered after a live catalog and keeps the watchdog running when the handshake window is unregistered before any live catalog.

fires watchdog error when catalog is established and handshake window is still registered is not changed.

Test notes

  • New in host-communication.spec.ts (5): emission on unregister with the registry already updated; the remembered window for an iframe detached before unregisterIframe; no emission while another registration holds the window; no emission for an unknown or already removed target; registerIframe(null).
  • New in catalog-management.spec.ts (7): immediate clear with no timer advance and no error after 5000 ms; a different window unregistered; no live catalog (watchdog error at 5 s); a live catalog from the previous URL does not count; queued RENDERER_READY for an unregistered window is skipped; an unregistered window with no live catalog still starts a handshake; a RENDERER_READY without a source window still starts a handshake.
  • does not count a live catalog from the previous renderer URL changes the URL and announces with no tick and no signal read in between. It fails if the handler does not call syncResolutionGeneration() and passes with it.
  • Spec doubles: MockHostCommunication in demos.spec.ts gains windowUnregistered$ and isRegisteredWindow (returns true: every frame announcing in that spec is a mounted DemoCard frame, which GuestFrame registers). The double in chat-message.spec.ts gains windowUnregistered$. No assertion in either spec changed.

Commits

  • 2373579f feat(shell): emit windowUnregistered$ from HostCommunication
  • 2cd2eb05 fix(shell): cancel catalog handshake when its frame unregisters
  • 4d643c0e fix(shell): skip catalog handshake for an unregistered frame
  • 4e897075 refactor(shell): drop the late watchdog suppression branch
  • 0029c5d9 test(shell): drop redundant detached-window handshake specs

6 files changed, 471 insertions(+), 101 deletions(-)

Stack

Stacked PR — base: pr8-restore-v0-9-renderers -> merge in order:

Verification

Run locally at 0029c5d9 (pr9-cancel-handshake-on-unregister tip, rebased onto 2dfa7517), one command at a time:

  • corepack yarn workspace a2ui-bridge clean && corepack yarn build: exit 0
  • corepack yarn test: 3690/3690 passed (scripts: 48, a2ui-bridge: 338, lit-basic-catalog: 26, ng-basic-catalog: 34, react-basic-catalog: 26, react-slack-catalog: 197, a2ui-composer-shell: 3021)
  • corepack yarn lint, corepack yarn tsc, corepack yarn prettier:check, corepack yarn generate:demos:check (46 demos): all exit 0
  • From shell/, E2E_HEADLESS=true corepack yarn playwright test --project=e2e --workers=1 --retries=0 e2e/demos.e2e.ts e2e/dual-version-renderers.e2e.ts e2e/slack-assistant.e2e.ts e2e/gallery.e2e.ts: 51/51 passed (6.0m)
  • Full headless E2E suite (corepack yarn --cwd shell e2e-headless): initial run 243/244 passed (9.1m; 1 timeout at e2e/guest-frame-sends.e2e.ts:587), sequential rerun (e2e/guest-frame-sends.e2e.ts --workers=1 --retries=0) 24/24 passed (3.8m), clean full e2e-headless rerun 244/244 passed (10.4m)

Prior pre-R2 stress runs at 757bc433 (before rebase onto 2dfa7517 and dropping the two duplicate specs): demos.e2e.ts --repeat-each=5 ran twice (90/90 and 90/90), demo-card-commit-accuracy.e2e.ts --repeat-each=5 passed 25/25, and e2e-headless ran twice (244/244 and 244/244).

Not run at 0029c5d9: visual regression (test:visual), stress reruns (--repeat-each=5), addlicense -check (no new files), CI.

Known limits

  • A dead handshake with no live catalog still waits 5 s. If the handshake frame is unregistered before any live catalog is established for the current URL, isHandshakeInProgress() stays true until the watchdog fires and reports its error. This is deliberate: cancelling silently there can leave the shell with no catalog, no error and nothing pending.
  • A registered element whose window is replaced in place produces no notification (see the behavior difference in the Summary). A handshake waiting on the old window is not cancelled; it is taken over when the new document announces, or ends with the watchdog error if it never does.
  • Unresolved observation on the stack below this PR. One demos stress run on an earlier #334 branch failed 2/90 at demos.e2e.ts:258 (header title missing after 5 s). It was not reproduced in 7 further plain runs of the same command on that branch (630/630, four of them with temporary console logging in the shell) plus 60/60 runs of that test alone, nor in 720 traced runs (4 unrelated timeouts); 930143a2 passed 360/360 in the same session, and the two pre-R2 stress runs on this branch passed 180/180. Static analysis found no path by which #325's fix can stall the workspace handshake. The artifacts of the failing run were not kept; cause not established.

Pre-launch Checklist

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request introduces a windowUnregistered$ observable to HostCommunication to notify subscribers when a guest window's registration is removed. CatalogManagement subscribes to this stream to immediately cancel handshakes waiting on unregistered windows when a live catalog is already established, replacing the previous approach of waiting for the 5-second watchdog timer to expire. Comprehensive unit tests have been added to verify the unregistration emission logic and handshake cancellation behavior. I have no feedback to provide.

@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

⚡ A2UI Composer PR Preview

Your automated preview is successfully live (commit 0029c5d):
👉 Launch PR Preview

Note: This environment will be wiped automatically when the PR is merged or closed.

@jgindin
jgindin requested a review from mikzat November 25, 2026 02:05
expect(service.isHandshakeInProgress()).toBe(true);

registered.delete(win2);
unregisterWindow(registered, win2);

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: now that 757bc43 switched this spec (L1854-1900) and the next one (L1902-1929) to unregisterWindow, both are strict subsets of the new specs at L2211-2242 and L2274-2302, which assert the state both right after unregistration and after 5000 ms. Could drop the two earlier copies.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressed in 0029c5d (after rebasing onto PR #334 at 2dfa7517). Dropped the two redundant detached-window handshake specs in shell/src/app/storage/catalog-management/catalog-management.spec.ts that were strict subsets of the new unregistration specs.

HostCommunication gains windowUnregistered$, a plain Subject exposed
read-only like sendToServer$. unregisterIframe emits the guest window
as its last step, and only when the window was registered before the
call and is not registered after it. A window still held by another
registration, an unknown target and a repeated unregistration do not
emit. registerIframe(null) emits each window the registry held. The
subject completes in ngOnDestroy.

Nothing subscribes yet; the catalog handshake uses it in the next
commit.
CatalogManagement subscribes to HostCommunication.windowUnregistered$
next to its messageStream$ subscription. When the unregistered window
is the one the handshake is waiting on and a live catalog is already
established for the current renderer URL, the handshake ends at once:
the watchdog timer is cleared, the handshake window is dropped and the
state settles, with no error.

In every other case nothing changes: a different window, no handshake
in progress, or no live catalog for the current URL (first handshake,
cache-only catalog, prior failure, URL change) leave the 5 s watchdog
and the takeover branch as they were.

The subscription is outside the concatMap so that a cancellation does
not wait behind an asynchronous catalog commit. The live-catalog check
calls syncResolutionGeneration() first, so a catalog from the previous
renderer URL does not count before the effect has observed the change.

The late suppression branch in the watchdog callback is still present;
a later commit removes it.

The HostCommunication doubles in demos.spec.ts and
chat-message.spec.ts gain windowUnregistered$, since both specs build
a real CatalogManagement over them.
A RENDERER_READY can wait in the concatMap behind an asynchronous
catalog commit. If its frame is unregistered meanwhile, the
windowUnregistered$ notification has already passed without finding a
handshake to cancel, and the handshake started afterwards could only
time out.

handleRendererReady now returns without starting a handshake when the
announcement has a source window, that window is not registered and a
live catalog is established for the current renderer URL.

Unchanged: a RENDERER_READY without a source window, a registered
source window, and an unregistered source window when no live catalog
is established all start a handshake as before.

MockHostCommunication in demos.spec.ts gains isRegisteredWindow,
returning true. The real service always has the method, and every
frame that announces in that spec is a mounted DemoCard frame, which
GuestFrame registers. No assertion in that spec changes.
The watchdog callback no longer checks, when the 5 s timer fires,
whether the handshake window is still registered. That case is now
handled when the frame is unregistered (cancel) or when its queued
RENDERER_READY is processed (skip), so the branch, its TODO and the
{settle} option of resetHandshakeWatchdog are removed. The unregister
handler settles the handshake state itself.

_hasEstablishedLiveCatalog stays; its JSDoc now describes the two
places that read it.

Six existing specs that modelled an unregistration only by changing
what the isRegisteredWindow double returns now go through the
unregisterWindow spec helper, which also emits windowUnregistered$,
as the real HostCommunication does. Their assertions are unchanged.
Remove the two earlier detached-window watchdog specs that are strict
subsets of the newer immediate-clear and pre-catalog watchdog specs.
@jgindin
jgindin force-pushed the pr9-cancel-handshake-on-unregister branch from 757bc43 to 0029c5d Compare November 25, 2026 22:50

This branch has not been deployed

No deployments
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.

2 participants