Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

fix: guard live-token poll re-entrancy and sweep leaked wallet connect listeners

nirholas5a8ac9120652
diff --git a/src/home-live-token.js b/src/home-live-token.js
index 0d57f9420..1d2cdc3a5 100644
--- a/src/home-live-token.js
+++ b/src/home-live-token.js
@@ -82,6 +82,7 @@ class HomeLiveToken {
this.seen = new Set();
this.pollTimer = null;
this.tickTimer = null;
+ this.fetching = false;
this.failCount = 0;
this.visible = false;
this.destroyed = false;
@@ -129,9 +130,16 @@ class HomeLiveToken {
// ── Polling ─────────────────────────────────────────────────────────────────
_poll() {
- if (this.destroyed || !this.coin) return;
+ // In-flight guard: a visibility toggle (IntersectionObserver crossing the
+ // scroll boundary) re-enters _poll(), which would stack a second fetch on
+ // top of one already running and fork the poll loop. Only ever one fetch
+ // and one pending timer at a time.
+ if (this.destroyed || !this.coin || this.fetching) return;
clearTimeout(this.pollTimer);
+ this.pollTimer = null;
+ this.fetching = true;
this._fetchTrades().finally(() => {
+ this.fetching = false;
if (this.destroyed || !this.visible) return;
this.pollTimer = setTimeout(() => this._poll(), POLL_MS);
});
diff --git a/src/wallet/connect-button.js b/src/wallet/connect-button.js
index be93af05b..0edca5057 100644
--- a/src/wallet/connect-button.js
+++ b/src/wallet/connect-button.js
@@ -368,6 +368,25 @@ export class ConnectWalletController extends EventTarget {
}
}
+/**
+ * Live connect buttons, tracked so we can dispose controllers whose mount
+ * element has left the DOM. Without this, each SPA navigation that re-mounts
+ * the button on a fresh element leaks two `window.ethereum` listeners, which
+ * eventually trips MetaMask's MaxListenersExceededWarning (warns at 10+).
+ * @type {Set<{ ctrl: ConnectWalletController, mountEl: HTMLElement }>}
+ */
+const _liveButtons = new Set();
+
+/** Dispose controllers whose mount element is no longer in the document. */
+function disposeDetachedButtons() {
+ for (const rec of _liveButtons) {
+ if (!rec.mountEl.isConnected) {
+ rec.ctrl.dispose();
+ _liveButtons.delete(rec);
+ }
+ }
+}
+
let _pickerStylesInjected = false;
function ensurePickerStyles() {
@@ -492,14 +511,23 @@ const LABEL_DEFAULTS = {
* @returns {ConnectWalletController}
*/
export function createConnectWalletButton(mountEl, opts = {}) {
- // Dispose any previous controller on this mount point (handles HMR remounts).
- mountEl._cwbCtrl?.dispose();
+ // Dispose any previous controller on this mount point (handles HMR remounts)
+ // and sweep controllers whose mount element has left the DOM (SPA navigations)
+ // so their window.ethereum listeners can't accumulate.
+ if (mountEl._cwbCtrl) {
+ mountEl._cwbCtrl.dispose();
+ for (const rec of _liveButtons) {
+ if (rec.ctrl === mountEl._cwbCtrl) _liveButtons.delete(rec);
+ }
+ }
+ disposeDetachedButtons();
const labels = { ...LABEL_DEFAULTS, ...(opts.labels || {}) };
const allowedChainIds = opts.allowedChainIds || DEFAULT_CHAIN_IDS;
const wcProjectId = opts.wcProjectId || import.meta.env?.VITE_WALLETCONNECT_PROJECT_ID || null;
const ctrl = new ConnectWalletController({ ...opts, allowedChainIds, wcProjectId });
mountEl._cwbCtrl = ctrl;
+ _liveButtons.add({ ctrl, mountEl });
const btn = document.createElement('button');
btn.type = 'button';