diff --git a/ZGUI.css b/ZGUI.css index a5a5e8b..c118389 100644 --- a/ZGUI.css +++ b/ZGUI.css @@ -1827,6 +1827,25 @@ button:not(.tablinks):not(.action-btn):not(.editor-source-tab):not(.status-state color: #9f2d20; } +/* Retry button shown in a ROOT pane whose load failed or timed out. */ +.web-root-retry { + background: var(--mu-green); + border: 0; + border-radius: 6px; + color: #fff; + cursor: pointer; + font-size: 13px; + margin-left: 8px; + min-height: 32px; + padding: 0 14px; +} + +.web-root-retry:hover, +.web-root-retry:focus-visible { + filter: brightness(1.08); + outline: none; +} + .web-plot-window[data-plot-state="maximized"] .web-root-plots { max-height: none; } diff --git a/muSRFitRoot.js b/muSRFitRoot.js index f3235fc..6ec8975 100644 --- a/muSRFitRoot.js +++ b/muSRFitRoot.js @@ -107,6 +107,55 @@ if (!window.MusrfitRootLayoutBound && typeof window.addEventListener === "functi window.MusrfitRootLayoutBound = true; } +// Time-bound an async operation so a JA-root/JSROOT fetch that never settles +// degrades to a visible error instead of leaving "Loading ROOT canvas..." up +// forever on an empty pane. Returns the promise's value when it wins the race +// and throws a TimeoutError describing the step when the deadline passes. +// Overridable so tests (and deployments tuning latency) can set a short budget. +if (typeof window.MusrfitRootFetchTimeoutMs === "undefined") { + window.MusrfitRootFetchTimeoutMs = 60000; +} + +function musrfitRootTimeoutMs() { + let configured = Number(window.MusrfitRootFetchTimeoutMs); + return Number.isFinite(configured) && configured > 0 ? configured : 60000; +} + +function musrfitRootTimeoutError(step, url) { + let error = new Error(step + " timed out after " + + Math.round(musrfitRootTimeoutMs() / 1000) + "s: " + url); + error.name = "TimeoutError"; + return error; +} + +function withMusrfitRootTimeout(promise, step, url) { + return new Promise(function(resolve, reject) { + let settled = false; + let timer = setTimeout(function() { + if (settled) { + return; + } + settled = true; + reject(musrfitRootTimeoutError(step, url)); + }, musrfitRootTimeoutMs()); + Promise.resolve(promise).then(function(value) { + if (settled) { + return; + } + settled = true; + clearTimeout(timer); + resolve(value); + }, function(error) { + if (settled) { + return; + } + settled = true; + clearTimeout(timer); + reject(error); + }); + }); +} + // Plot shortcuts should not steal keystrokes from inputs, selects, or editable // regions that may appear in future plot controls. function musrfitRootIsEditableTarget(target) { @@ -498,6 +547,30 @@ function disconnectMusrfitRootResizeObservers(container) { }); } +// Renders are asynchronous (fetch + decode + draw). Rapid toggling of the +// clean/original view can start several renders on the same container at once; +// the newest render's replaceChildren() detaches the canvases the older in-flight +// renders are still drawing into. Guard each container with a generation token so +// a stale draw aborts instead of throwing "Could not draw ..." on a removed node. +var musrfitRootContainerGeneration = new WeakMap(); + +// Return a token that strictly increases for every new render on `container`. +function beginMusrfitRootRender(container) { + let generation = musrfitRootContainerGeneration.get(container) || 0; + generation += 1; + musrfitRootContainerGeneration.set(container, generation); + return generation; +} + +// A render is stale when a newer render replaced it, or when the specific canvas +// this draw targets has been detached from the document (e.g. window closed). +function musrfitRootRenderIsStale(container, generation, canvas) { + if (musrfitRootContainerGeneration.get(container) !== generation) { + return true; + } + return !canvas || !canvas.isConnected; +} + // JSROOT does not automatically know that our floating dialog was resized by // CSS or by the user, so explicitly notify it when the canvas has real geometry. function resizeMusrfitRootCanvas(jsroot, canvas) { @@ -538,6 +611,7 @@ function installMusrfitRootResizeObserver(jsroot, canvas) { // Draw all ROOT canvases returned by musrview. Each PLOT block becomes one pane; // errors are shown beside the affected pane rather than failing the whole window. async function renderMusrfitRootPlots(container, plotSpecs, renderVersion, viewMode = "clean") { + let renderToken = beginMusrfitRootRender(container); disconnectMusrfitRootResizeObservers(container); container.replaceChildren(); if (!plotSpecs.length) { @@ -550,7 +624,8 @@ async function renderMusrfitRootPlots(container, plotSpecs, renderVersion, viewM let jsroot = null; try { - jsroot = await loadMusrfitJSRoot(); + jsroot = await withMusrfitRootTimeout( + loadMusrfitJSRoot(), "Loading JSROOT", window.MusrfitJSRootURL); } catch (error) { let message = document.createElement("div"); message.className = "web-root-message web-root-error"; @@ -559,6 +634,9 @@ async function renderMusrfitRootPlots(container, plotSpecs, renderVersion, viewM container.appendChild(message); return; } + if (musrfitRootRenderIsStale(container, renderToken, container)) { + return; + } for (let plotIndex = 0; plotIndex < plotSpecs.length; plotIndex++) { let plotSpec = plotSpecs[plotIndex]; @@ -578,12 +656,27 @@ async function renderMusrfitRootPlots(container, plotSpecs, renderVersion, viewM container.appendChild(pane); try { - let file = await jsroot.openFile(musrfitRootCacheBustUrl(plotSpec.url, renderVersion)); - let object = await file.readObject(plotSpec.item); + let file = await withMusrfitRootTimeout( + jsroot.openFile(musrfitRootCacheBustUrl(plotSpec.url, renderVersion)), + "Opening ROOT file", plotSpec.url); + if (musrfitRootRenderIsStale(container, renderToken, canvas)) { + return; + } + let object = await withMusrfitRootTimeout( + file.readObject(plotSpec.item), + "Decoding object " + plotSpec.item, plotSpec.url); + if (musrfitRootRenderIsStale(container, renderToken, canvas)) { + return; + } if (viewMode === "clean") { cleanMusrfitRootCanvas(object); } - let painter = await jsroot.draw(canvasId, object, ""); + let painter = await withMusrfitRootTimeout( + jsroot.draw(canvasId, object, ""), + "Drawing ROOT canvas", plotSpec.url); + if (musrfitRootRenderIsStale(container, renderToken, canvas)) { + return; + } // The normal JSROOT point tooltip contains file/object details and can // obscure a large fraction of a multi-run plot. Clean view suppresses @@ -596,9 +689,22 @@ async function renderMusrfitRootPlots(container, plotSpecs, renderVersion, viewM resizeMusrfitRootCanvas(jsroot, canvas); status.remove(); } catch (error) { + if (musrfitRootRenderIsStale(container, renderToken, canvas)) { + return; + } status.className = "web-root-message web-root-error"; status.textContent = "Could not draw " + plotSpec.item + ": " + (error && error.message ? error.message : String(error)); + let retry = document.createElement("button"); + retry.type = "button"; + retry.className = "web-root-retry"; + retry.textContent = "Retry"; + retry.title = "Try loading " + plotSpec.url + " again"; + status.appendChild(document.createTextNode(" ")); + status.appendChild(retry); + retry.addEventListener("click", function() { + renderMusrfitRootPlots(container, plotSpecs, renderVersion, viewMode); + }); } } } diff --git a/tests/root-plot-clean.test.js b/tests/root-plot-clean.test.js index 0763155..10872f4 100644 --- a/tests/root-plot-clean.test.js +++ b/tests/root-plot-clean.test.js @@ -13,7 +13,11 @@ function loadRootLibrary() { const sandbox = { console: console, window: {}, - document: {} + document: {}, + setTimeout: setTimeout, + clearTimeout: clearTimeout, + Promise: Promise, + Error: Error }; sandbox.global = sandbox; vm.runInNewContext(code, sandbox, { filename: "muSRFitRoot.js" }); @@ -270,12 +274,93 @@ function testTouchActionMenu() { assert.match(code, /!plotActionMenu\.contains\(event\.target\)/); } +// A ROOT pane can hang on "Loading ROOT canvas..." when JSROOT's openFile / +// readObject / draw never settles (e.g. a ranged fetch against a server that +// never answers). Every await is time-bound so the pane degrades to a visible +// error, and the error carries a Retry button for recovery. +function testLoadTimeout() { + const lib = loadRootLibrary(); + + // A promise that wins the race resolves through the wrapper unchanged. + return lib.withMusrfitRootTimeout(Promise.resolve(42), "step", "http://u/r.root") + .then(function(value) { + assert.strictEqual(value, 42, "resolved promise passes through"); + }) + .then(function() { + // A promise that never settles must reject with a TimeoutError that + // names the step and the URL so the stuck pane becomes diagnosable. + lib.window.MusrfitRootFetchTimeoutMs = 30; + return lib.withMusrfitRootTimeout( + new Promise(() => {}), "Opening ROOT file", "http://h/x.root"); + }) + .then( + () => Promise.reject(new Error("expected the stuck promise to time out")), + (error) => { + assert.strictEqual(error.name, "TimeoutError"); + assert.match(String(error.message), /Opening ROOT file/); + assert.match(String(error.message), /http:\/\/h\/x\.root/); + } + ); +} + +// The three per-pane awaits are each wrapped with the deadline, and a failure +// renders an inline Retry button instead of a bare message. +function testLoadTimeoutSource() { + const code = fs.readFileSync(path.join(repoRoot, "muSRFitRoot.js"), "utf8"); + assert.match(code, /withMusrfitRootTimeout\(\s*jsroot\.openFile/); + assert.match(code, /withMusrfitRootTimeout\(\s*file\.readObject/); + assert.match(code, /withMusrfitRootTimeout\(\s*jsroot\.draw/); + assert.match(code, /withMusrfitRootTimeout\(\s*loadMusrfitJSRoot\(\)/); + assert.match(code, /status\.appendChild\(retry\)/); + assert.match(code, /Could not load JSROOT:/); + const css = fs.readFileSync(path.join(repoRoot, "ZGUI.css"), "utf8"); + assert.match(css, /\.web-root-retry/); +} + +// Toggling clean/original starts a fresh render on the same container while the +// previous one may still be fetching/drawing. A generation token lets the stale +// render abort instead of drawing into a canvas that a newer render already +// removed, which previously surfaced as "Could not draw ..." on Firefox. +function testRenderGenerationGuard() { + const lib = loadRootLibrary(); + const firstContainer = {}; + const secondContainer = {}; + + // Each render advances the generation for its own container only. + assert.strictEqual(lib.beginMusrfitRootRender(firstContainer), 1); + assert.strictEqual(lib.beginMusrfitRootRender(firstContainer), 2); + assert.strictEqual(lib.beginMusrfitRootRender(secondContainer), 1); + + // A canvas is stale after the container moves on (a newer render started) + // or once the canvas is detached from the document. + let detachedCanvas = {}; + detachedCanvas.isConnected = false; + assert.ok(lib.musrfitRootRenderIsStale(firstContainer, 1, detachedCanvas)); + assert.strictEqual( + lib.musrfitRootRenderIsStale(firstContainer, 2, { isConnected: true }), + false, + "current generation + attached canvas is not stale" + ); + // An old render token against the current generation is stale. + assert.ok(lib.musrfitRootRenderIsStale(firstContainer, 1, { isConnected: true })); + + // Every render path funnels through the tokenized entrypoint and checks + // staleness after each await so stale draws never paint a removed canvas. + const code = fs.readFileSync(path.join(repoRoot, "muSRFitRoot.js"), "utf8"); + assert.match(code, /let renderToken = beginMusrfitRootRender\(container\)/); + assert.match(code, /musrfitRootRenderIsStale\(container, renderToken, canvas\)/); + assert.match(code, /musrfitRootRenderIsStale\(container, renderToken, container\)/); +} + function runTests() { testCleanLegendLabels(); testCleanCanvasLayout(); testCrowdedLegendFallsBackAndShrinks(); testCleanViewIsDefaultAndToggleExists(); testTouchActionMenu(); + testLoadTimeoutSource(); + testRenderGenerationGuard(); + testLoadTimeout(); console.log("ROOT clean-view tests passed."); }