Repository navigation
fix(embedded): prevent duplicate React root on rehandshake - #39860
Conversation
The `started` flag and the inline `createRoot(appMountPoint).render(...)` call both lived inside the 'message' event handler, so each port-transfer event reset `started` to false and could invoke `createRoot` a second time on the same DOM node — which React 18 warns about and where behavior is undefined. Hoist `started` and a lazily-created `root` to module scope so that re-handshakes reuse the existing root, and unmount it in the existing `beforeunload` handler.
Code Review Agent Run #59d120Actionable Suggestions - 0Review Details
Bito Usage GuideCommands Type the following command in the pull request comment and save the comment.
Refer to the documentation for additional commands. Configuration This repository uses Documentation & Help |
|
The flagged logic error is correct: hoisting superset-frontend/src/embedded/index.tsx |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #39860 +/- ##
==========================================
- Coverage 64.36% 64.36% -0.01%
==========================================
Files 2569 2569
Lines 134745 134752 +7
Branches 31278 31280 +2
==========================================
- Hits 86732 86728 -4
- Misses 46515 46526 +11
Partials 1498 1498
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Hoisting `started` to module scope and setting it `true` synchronously after `start()` removed the original retry-on-rehandshake behavior: a transient `/api/v1/me/roles/` failure left `started=true` permanently, so a follow-up port-transfer with a valid token could not retry and the embed stayed stuck on the failure message. Only mark `started=true` after a successful render. Track in-flight attempts separately to drop concurrent calls, and reset both flags in the error path so a fresh handshake can retry.
Setting `started = true` synchronously at the entry of start() matches the original sync-guard semantics and prevents the duplicate-call race; resetting it on error keeps the rehandshake-retry behavior. The separate startInFlight flag was redundant.
Code Review Agent Run #dc8276Actionable Suggestions - 0Review Details
Bito Usage GuideCommands Type the following command in the pull request comment and save the comment.
Refer to the documentation for additional commands. Configuration This repository uses Documentation & Help |
…ation The iframe's `load` listener is registered before `iframe.src`, so it fires on every load, reloads included, and opens a fresh `MessageChannel` each time. But it only returns that channel through `resolve()` of a promise that is already settled from the second load onwards. Nobody keeps the new `port1`, so a dashboard that navigates itself — a tab link, a link in a Markdown chart, a drill-down — leaves a blank frame: the new document waits for a guest token on a channel whose other end is gone, while `ourPort` keeps talking to the document that disappeared. apache#39860 already made the embedded page tolerate a second 'port transfer' handshake; this completes the host half. - `connect(iframe)` is extracted from the listener: it opens the channel, transfers the port, constructs and starts the `Switchboard`, and replays the host-defined methods — a reloaded document has never heard of them. - `ourPort`, `refreshTimer` and `unmounted` move above `mountIframe`, which now resolves `void`; the listener reassigns `ourPort` on every load. The existing accessors read that variable, so they follow the current port unchanged. - A reload cancels the pending timer and re-fetches a token rather than replaying the cached one: refresh happens 5s before expiry, so a reload landing just before it would hand the new page a token with seconds to live, and its `GET /api/v1/me/roles/` would 401 into the "session interrupted" banner. - `refreshGuestToken` takes a generation number and bails out if a reload superseded it, so a refresh left in flight cannot emit a stale token or arm a second timer. - `resolvePermalinkUrl` and `observeDataMask` are registered through `defineHostMethod`, so they survive a reload. - A `load` arriving after `unmount()` is now ignored; it used to open a channel. `MessagePort` queues what arrives before `start()`, so emitting the token right after the handshake is safe — that is already the property the first load relies on. Three tests cover re-authentication on the new port, the replay of host-defined methods, and a load after unmount. All three fail without this change.
…ation The iframe's `load` listener is registered before `iframe.src`, so it fires on every load, reloads included, and opens a fresh `MessageChannel` each time. But it only returns that channel through `resolve()` of a promise that is already settled from the second load onwards. Nobody keeps the new `port1`, so a dashboard that navigates itself — a tab link, a link in a Markdown chart, a drill-down — leaves a blank frame: the new document waits for a guest token on a channel whose other end is gone, while `ourPort` keeps talking to the document that disappeared. apache#39860 already made the embedded page tolerate a second 'port transfer' handshake; this completes the host half. A `Connection` bundles a port with everything scoped to the document on the other end of it — its switchboard, the `get` calls still awaiting a reply, and whether it has been handed a token. The SDK holds exactly one, and a load replaces it. - `connect(iframe)` is extracted from the listener: it opens the channel, transfers the port, constructs and starts the `Switchboard`, and replays the host-defined methods — a reloaded document has never heard of them. - The accessors go through `portGet`, which scopes a call to the connection it was issued on. A `get` still in flight when the document leaves is rejected with `PortClosedError` instead of hanging forever on a reply that can no longer come, and the retired port is closed. Same for `unmount()`. - A reload re-fetches a token rather than replaying the cached one: refresh happens 5s before expiry, so a reload landing just before it would hand the new page a token with seconds to live, and its `GET /api/v1/me/roles/` would 401 into the "session interrupted" banner. - Not every navigation lands on the embedded page — a link in a Markdown chart can point at a plain Superset URL, or off site. The SDK probes the new document with an unknown method first: any reply, including the error an embedded page returns for a method it does not implement, proves a switchboard is listening. Silence times out and no token is minted, so the host's endpoint is not billed for a document that cannot use one. - `refreshGuestToken` takes a generation number and bails out if a reload superseded it, so a refresh left in flight cannot emit a stale token or arm a second timer. A token from a superseded cycle is still handed over when the current document has none, rather than leaving the page blank for a whole retry interval. - The initial fetch failing tears the iframe down only when no reload has taken over. Once a reload owns the token chain, that chain decides whether the embed lives; a rejection from a fetch nobody is waiting for any more must not take a dashboard that is already rendering away from the host. - `resolvePermalinkUrl` and `observeDataMask` are registered through `defineHostMethod`, so they survive a reload, and the theme the host pushed with `setThemeConfig`/`setThemeMode` is replayed to the new document after its token — a new realm holds none of it. - A `load` arriving after `unmount()` is now ignored; it used to open a channel. `MessagePort` queues what arrives before `start()`, so emitting the token right after the handshake is safe — that is already the property the first load relies on. Ten tests cover the new port and its fresh token, the replay of host methods and of the theme, the rejection of calls stranded by a navigation and by `unmount()`, a navigation that is not the embedded page, both directions of the race between the initial fetch and a reload, and a load after unmount. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…ation The iframe's `load` listener is registered before `iframe.src`, so it fires on every load, reloads included, and opens a fresh `MessageChannel` each time. But it only returns that channel through `resolve()` of a promise that is already settled from the second load onwards. Nobody keeps the new `port1`, so a dashboard that navigates itself — a tab link, a link in a Markdown chart, a drill-down — leaves a blank frame: the new document waits for a guest token on a channel whose other end is gone, while `ourPort` keeps talking to the document that disappeared. apache#39860 already made the embedded page tolerate a second 'port transfer' handshake; this completes the host half. A `Connection` bundles a port with everything scoped to the document on the other end of it — its switchboard, the `get` calls still awaiting a reply, and whether it has been handed a token. The SDK holds exactly one, and a load replaces it. - `connect(iframe)` is extracted from the listener: it opens the channel, transfers the port, constructs and starts the `Switchboard`, and replays the host-defined methods — a reloaded document has never heard of them. - The accessors go through `portGet`, which scopes a call to the connection it was issued on. A `get` still in flight when the document leaves is rejected with `PortClosedError` instead of hanging forever on a reply that can no longer come, and the retired port is closed. Same for `unmount()`. - A reload re-fetches a token rather than replaying the cached one: refresh happens 5s before expiry, so a reload landing just before it would hand the new page a token with seconds to live, and its `GET /api/v1/me/roles/` would 401 into the "session interrupted" banner. - Not every navigation lands on the embedded page — a link in a Markdown chart can point at a plain Superset URL, or off site. The SDK probes the new document with an unknown method first: any reply, including the error an embedded page returns for a method it does not implement, proves a switchboard is listening. Silence times out and no token is minted, so the host's endpoint is not billed for a document that cannot use one. - `refreshGuestToken` takes a generation number and bails out if a reload superseded it, so a refresh left in flight cannot emit a stale token or arm a second timer. A token from a superseded cycle is still handed over when the current document has none, rather than leaving the page blank for a whole retry interval. - The initial fetch failing tears the iframe down only when no reload has taken over. Once a reload owns the token chain, that chain decides whether the embed lives; a rejection from a fetch nobody is waiting for any more must not take a dashboard that is already rendering away from the host. - `resolvePermalinkUrl` and `observeDataMask` are registered through `defineHostMethod`, so they survive a reload, and the theme the host pushed with `setThemeConfig`/`setThemeMode` is replayed to the new document after its token — a new realm holds none of it. - A `load` arriving after `unmount()` is now ignored; it used to open a channel. `MessagePort` queues what arrives before `start()`, so emitting the token right after the handshake is safe — that is already the property the first load relies on. Ten tests cover the new port and its fresh token, the replay of host methods and of the theme, the rejection of calls stranded by a navigation and by `unmount()`, a navigation that is not the embedded page, both directions of the race between the initial fetch and a reload, and a load after unmount. Signed-off-by: Fabrice Meyer <[email protected]>
…ation The iframe's `load` listener is registered before `iframe.src`, so it fires on every load, reloads included, and opens a fresh `MessageChannel` each time. But it only returns that channel through `resolve()` of a promise that is already settled from the second load onwards. Nobody keeps the new `port1`, so a dashboard that navigates itself — a tab link, a link in a Markdown chart, a drill-down — leaves a blank frame: the new document waits for a guest token on a channel whose other end is gone, while `ourPort` keeps talking to the document that disappeared. apache#39860 already made the embedded page tolerate a second 'port transfer' handshake; this completes the host half. A `Connection` bundles a port with everything scoped to the document on the other end of it — its switchboard, the `get` calls still awaiting a reply, and whether it has been handed a token. The SDK holds exactly one, and a load replaces it. - `connect(iframe)` is extracted from the listener: it opens the channel, transfers the port, constructs and starts the `Switchboard`, and replays the host-defined methods — a reloaded document has never heard of them. - The accessors go through `portGet`, which scopes a call to the connection it was issued on. A `get` still in flight when the document leaves is rejected with `PortClosedError` instead of hanging forever on a reply that can no longer come, and the retired port is closed. Same for `unmount()`. - A reload re-fetches a token rather than replaying the cached one: refresh happens 5s before expiry, so a reload landing just before it would hand the new page a token with seconds to live, and its `GET /api/v1/me/roles/` would 401 into the "session interrupted" banner. - Not every navigation lands on the embedded page — a link in a Markdown chart can point at a plain Superset URL, or off site. The SDK probes the new document with an unknown method first: any reply, including the error an embedded page returns for a method it does not implement, proves a switchboard is listening. Silence times out and no token is minted, so the host's endpoint is not billed for a document that cannot use one. - `refreshGuestToken` takes a generation number and bails out if a reload superseded it, so a refresh left in flight cannot emit a stale token or arm a second timer. - `resolvePermalinkUrl` and `observeDataMask` are registered through `defineHostMethod`, so they survive a reload, and the theme the host pushed with `setThemeConfig`/`setThemeMode` is replayed to the new document after its token — a new realm holds none of it. - A `load` arriving after `unmount()` is now ignored; it used to open a channel. `MessagePort` queues what arrives before `start()`, so emitting the token right after the handshake is safe — that is already the property the first load relies on. Signed-off-by: Fabrice Meyer <[email protected]>
…ation The iframe's `load` listener is registered before `iframe.src`, so it fires on every load, reloads included, and opens a fresh `MessageChannel` each time. But it only returns that channel through `resolve()` of a promise that is already settled from the second load onwards. Nobody keeps the new `port1`, so a dashboard that navigates itself — a tab link, a link in a Markdown chart, a drill-down — leaves a blank frame: the new document waits for a guest token on a channel whose other end is gone, while `ourPort` keeps talking to the document that disappeared. apache#39860 already made the embedded page tolerate a second 'port transfer' handshake; this completes the host half. - `connect(iframe)` is extracted from the listener: it opens the channel, transfers the port, constructs and starts the `Switchboard`, and replays the host-defined methods — a reloaded document has never heard of them. - `ourPort`, `refreshTimer` and `unmounted` move above `mountIframe`, which now resolves `void`; the listener reassigns `ourPort` on every load. The existing accessors read that variable, so they follow the current port unchanged. - A reload cancels the pending timer and re-fetches a token rather than replaying the cached one: refresh happens 5s before expiry, so a reload landing just before it would hand the new page a token with seconds to live, and its `GET /api/v1/me/roles/` would 401 into the "session interrupted" banner. - `refreshGuestToken` takes a generation number and bails out if a reload superseded it, so a refresh left in flight cannot emit a stale token or arm a second timer. - `resolvePermalinkUrl` and `observeDataMask` are registered through `defineHostMethod`, so they survive a reload. - A `load` arriving after `unmount()` is now ignored; it used to open a channel. `MessagePort` queues what arrives before `start()`, so emitting the token right after the handshake is safe — that is already the property the first load relies on. Three tests cover re-authentication on the new port, the replay of host-defined methods, and a load after unmount. All three fail without this change. Signed-off-by: Fabrice Meyer <[email protected]>
SUMMARY
Follow-up to #38563. The
startedflag and the inlinecreateRoot(appMountPoint).render(...)call both lived inside the message handler closure, so each'port transfer'MessageEvent resetstarted=falseand could callcreateRoota second time on the same DOM node. React 18 logs "You are calling ReactDOMClient.createRoot() on a container that has already been passed to createRoot() before" and behavior is undefined.Hoist
startedand a lazily-createdrootto module scope, render via the existing root on rehandshake, and unmount it in the existingbeforeunloadhandler. The root is created lazily so the no-iframe failure path can still write toappMountPoint.innerHTMLwithout conflicting with React.BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
N/A — no UI change; defensive fix for a console warning + undefined behavior on rehandshake.
TESTING INSTRUCTIONS
'port transfer'handshake from the parent (e.g. recreate the iframe channel).createRoot()warning in console; the embedded app continues to render correctly.ADDITIONAL INFORMATION