From 82df33019285fb9fb33d33e808ed36b1a9fbdd39 Mon Sep 17 00:00:00 2001 From: RunTimeAdmin <248247024+RunTimeAdmin@users.noreply.github.com> Date: Sun, 15 Nov 2026 17:09:44 -0600 Subject: [PATCH] Time out the app and testnet pages' RPC reads after 20 seconds A node that accepted the connection and never answered left /app and /testnet waiting with no message (finding 22 of the 15 November performance review). Each page now aborts a read that has not finished in 20 s, body included, as the epochs page does, and reports it where it reports any failed read: the log on /app, the card on /testnet. The startup reads in app.js stay as they are. symbol() and decimals() are read from the token address that svr() returns when the fee registry is live, so the four cannot go in one batch. Both scripts are bumped to ?v=timeout1. One regression test per page fails on the old code. Co-Authored-By: Claude Opus 5.5 --- oracle/site-app.test.js | 32 +++++++++++++++++++++++++++----- oracle/site-pages.test.js | 31 +++++++++++++++++++++++++++++++ site/app.html | 2 +- site/assets/app.js | 32 ++++++++++++++++++++++++-------- site/assets/testnet.js | 26 ++++++++++++++++++++------ site/testnet.html | 2 +- 6 files changed, 104 insertions(+), 21 deletions(-) diff --git a/oracle/site-app.test.js b/oracle/site-app.test.js index 5ca7211..e55c3de 100644 --- a/oracle/site-app.test.js +++ b/oracle/site-app.test.js @@ -43,9 +43,10 @@ function element(id) { /** * The page, loaded. `agents` maps an address to its chain state; `hold(did)` may return a promise the - * RPC waits on before it answers a lookup of that agent, or rejects with. + * RPC waits on before it answers a lookup of that agent, or rejects with. `rpc` replaces the stub RPC's + * fetch. Every timer the page sets fires at once, and `waits` records the delay it asked for. */ -function load({ agents = {}, hold = () => null } = {}) { +function load({ agents = {}, hold = () => null, rpc = null } = {}) { const els = new Map(); const $ = (id) => { if (!els.has(id)) els.set(id, element(id)); return els.get(id); }; const byDid = new Map(Object.entries(agents).map(([addr, s]) => [didOf(addr), { addr, ...s }])); @@ -84,19 +85,27 @@ function load({ agents = {}, hold = () => null } = {}) { } return { json: async () => one(body) }; }; + const waits = []; const ctx = vm.createContext({ document: { getElementById: $, createElement: () => element(null), querySelector: () => null, querySelectorAll: () => [] }, window: { addEventListener() {}, dispatchEvent() {} }, Event: class { constructor(type) { this.type = type; } }, SigvaraDid: Did, - fetch, - setTimeout: (fn) => setImmediate(fn), + fetch: rpc ?? fetch, + setTimeout: (fn, ms) => { waits.push(ms); return setImmediate(fn); }, + clearTimeout: clearImmediate, + AbortController, console, }); vm.runInContext(SOURCE, ctx, { filename: 'app.js' }); - return { ctx, $, batches, get: (name) => vm.runInContext(name, ctx) }; + return { ctx, $, batches, waits, get: (name) => vm.runInContext(name, ctx) }; } +// A node that accepts the connection and never answers: the request ends only if the page aborts it. +const neverAnswers = (_url, init) => new Promise((_, reject) => { + init.signal?.addEventListener('abort', () => reject(new DOMException('This operation was aborted', 'AbortError'))); +}); + /** An EIP-1193 wallet. `chain` can be changed under the page, as a user switching networks does. */ function wallet({ chain = ARC, account = OPERATOR, switchError = null, addSwitches = true } = {}) { const p = { @@ -233,3 +242,16 @@ test('a Suspended agent below the minimum is not called suspended by its operato assert.match(status, /bond it back to the minimum and reactivate it/); assert.match(page.$('stakeNote').innerHTML, /then reactivate it/); }); + +test('a read the RPC never answers fails after 20 seconds with a message, rather than leaving the page waiting', async () => { + const page = load({ rpc: neverAnswers }); + await idle(); + assert.equal(page.$('feeNow').textContent, 'unavailable', 'the token reads at load are still waiting'); + assert.match(page.$('log').innerHTML, /RPC read failed: the RPC did not answer within 20 seconds/); + + page.$('agentAddr').value = B; + page.ctx.lookup(); // not awaited: a lookup with no limit never settles + await idle(); + assert.match(page.$('log').innerHTML, /lookup failed: the RPC did not answer within 20 seconds/); + assert.deepEqual(page.waits, [20000, 20000], 'one 20 s limit on each request'); +}); diff --git a/oracle/site-pages.test.js b/oracle/site-pages.test.js index ea6fdf6..9f9e3f4 100644 --- a/oracle/site-pages.test.js +++ b/oracle/site-pages.test.js @@ -85,3 +85,34 @@ test('the agents page reads the chain only for the agents it shows, 50 rows at a assert.match(html, /Rows 51 to 100 of 130\./); assert.deepEqual([...new Set(asked.slice(300))], Array.from({ length: 40 }, (_, i) => word(i + 61)), 'only the rows not read before'); }); + +test('the testnet page reports a read the RPC never answers after 20 seconds, rather than waiting for good', async () => { + const source = fs.readFileSync(path.join(__dirname, '..', 'site', 'assets', 'testnet.js'), 'utf8'); + const els = new Map(); + const $ = (id) => { + if (!els.has(id)) els.set(id, { textContent: '', value: '', href: '', setAttribute() {}, addEventListener() {} }); + return els.get(id); + }; + let ready = null; + const waits = []; + const ctx = vm.createContext({ + window: { SigvaraDid: did }, + document: { getElementById: $, querySelector: () => null, addEventListener: (type, fn) => { if (type === 'DOMContentLoaded') ready = fn; } }, + // An address in the query string starts a check when the page loads. + URLSearchParams, + location: { search: '?a=0x00000000000000000000000000000000000000a1' }, + AbortController, + // A node that accepts the connection and never answers: the request ends only if the page aborts it. + fetch: (_url, init) => new Promise((_, reject) => { + init.signal?.addEventListener('abort', () => reject(new DOMException('This operation was aborted', 'AbortError'))); + }), + // The page's timer fires at once, and the test checks the delay it asked for. + setTimeout: (fn, ms) => { waits.push(ms); return setImmediate(fn); }, + clearTimeout: clearImmediate, + }); + vm.runInContext(source, ctx); + ready(); + for (let i = 0; i < 20; i++) await new Promise(setImmediate); + assert.equal($('cardDid').textContent, 'read failed: the RPC did not answer within 20 seconds'); + assert.deepEqual(waits, [20000]); +}); diff --git a/site/app.html b/site/app.html index 5568364..a29a48d 100644 --- a/site/app.html +++ b/site/app.html @@ -140,6 +140,6 @@

Activity

- + diff --git a/site/assets/app.js b/site/assets/app.js index ca9da95..3f059c5 100644 --- a/site/assets/app.js +++ b/site/assets/app.js @@ -127,10 +127,31 @@ function decodeString(hex) { } // ---- chain io ---- +// Every RPC read goes through here. A node that accepts the connection and never answers would leave +// the page waiting with no message, so a request that has not finished in 20 s, body included, is +// aborted and fails like any other read. The epochs page uses the same limit (epochs.js). +async function rpcPost(body) { + const ctl = new AbortController(); + const t = setTimeout(() => ctl.abort(), 20000); + try { + const res = await fetch(RPC, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify(body), + signal: ctl.signal, + }); + return await res.json(); + } catch (e) { + if (e.name === "AbortError") throw new Error("the RPC did not answer within 20 seconds"); + throw e; + } finally { + clearTimeout(t); + } +} + async function rpcRead(to, data) { const body = { jsonrpc: "2.0", id: 1, method: "eth_call", params: [{ to, data }, "latest"] }; - const res = await fetch(RPC, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body) }); - const j = await res.json(); + const j = await rpcPost(body); if (j.error) throw new Error(j.error.message); return j.result; } @@ -150,12 +171,7 @@ async function rpcBatch(calls) { jsonrpc: "2.0", id: i, method: "eth_call", params: [{ to: c.to, data: c.data }, "latest"], })); - const res = await fetch(RPC, { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify(body), - }); - const arr = await res.json(); + const arr = await rpcPost(body); if (!Array.isArray(arr)) throw new Error(arr?.error?.message ?? "batch call failed"); const byId = new Map(arr.map(r => [r.id, r])); return calls.map((c, i) => { diff --git a/site/assets/testnet.js b/site/assets/testnet.js index 574119a..5ec6aea 100644 --- a/site/assets/testnet.js +++ b/site/assets/testnet.js @@ -52,12 +52,26 @@ params: c.params || [{ to: c.to, data: c.data }, "latest"], }; }); - var res = await fetch(RPC, { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify(body), - }); - var arr = await res.json(); + // A node that accepts the connection and never answers would leave the card reading the + // chain with no message, so a request that has not finished in 20 s, body included, is + // aborted and reported like any other failed read. The epochs page uses the same limit. + var ctl = new AbortController(); + var timer = setTimeout(function () { ctl.abort(); }, 20000); + var arr; + try { + var res = await fetch(RPC, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify(body), + signal: ctl.signal, + }); + arr = await res.json(); + } catch (e) { + if (e.name === "AbortError") throw new Error("the RPC did not answer within 20 seconds"); + throw e; + } finally { + clearTimeout(timer); + } if (!Array.isArray(arr)) throw new Error((arr && arr.error && arr.error.message) || "RPC batch failed"); var byId = {}; arr.forEach(function (r) { byId[r.id] = r; }); diff --git a/site/testnet.html b/site/testnet.html index cf27de6..dbdba8c 100644 --- a/site/testnet.html +++ b/site/testnet.html @@ -187,6 +187,6 @@

Five steps to a live agent.

- +