From c8f0648fe81664e185722c14d49ffb8eaf69a8b7 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 25 Oct 2026 03:00:14 +0000 Subject: [PATCH] Fix "Canvas is already in use" errors and leaked charts; v1.9.5 Views draw charts in setTimeout(..., 0). Two renders before those timers run (loadFiles() then showView() at boot when the folder re-imports, or Reconnect re-rendering after an import) queue two charts per canvas, and Chart.js throws for the second. Separately, every render rebuilt the DOM without destroying the previous charts, so Chart.instances grew forever. ui.js now wraps the global Chart in a subclass that, before drawing, destroys any chart on the target canvas and any chart whose canvas is no longer in the page. No view code changes; statics are inherited. Tested headless: boot re-import, triple renderActive(), every view rendered twice in both themes, Forecast slider redraws, #7 Reconnect restore: no errors, charts == canvases, instances stay bounded. Before the fix, 5 of those 7 checks failed. Pre-existing on master. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01QoR1wxcaDjVVk3xmd5rRCt --- CHANGELOG.md | 12 ++++++++++++ CLAUDE.md | 6 +++--- README.md | 4 ++-- index.html | 2 +- js/ui.js | 21 +++++++++++++++++++++ js/views-settings.js | 2 +- 6 files changed, 40 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1144552..e3be6dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,18 @@ Format follows [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). --- +## [1.9.5] — 2026-10-25 + +### Fixed +- **"Canvas is already in use" console errors.** Views draw their charts in `setTimeout(…, 0)`. When a view rendered twice before those timers ran, both batches targeted the same canvas IDs and Chart.js threw for the second. This happened at startup when the folder's CSVs re-imported (`loadFiles()` renders, then `init.js` calls `showView()`), and after Reconnect. The bug was already on `master`. +- **Charts leaked on every render.** Views rebuild their DOM on each render, but the old charts were never destroyed. Chart.js kept every one alive, along with its canvas and data. +- **Fix:** a guard in `ui.js` replaces the global `Chart` with a subclass. Before drawing, it destroys any chart already on that canvas and any chart whose canvas has left the page. No view code changed. Statics such as `Chart.defaults` and `Chart.getChart` still work. + +### Docs +- **CLAUDE.md §7** says rule 4 is now enforced by the guard. + +--- + ## [1.9.4] — 2026-10-25 ### Fixed diff --git a/CLAUDE.md b/CLAUDE.md index 23c6fac..75d7828 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -59,7 +59,7 @@ Until then, this section is the rule. FinLens is a **single-origin, zero-build, vanilla JavaScript personal finance dashboard** that runs entirely in the browser. There is no server, no npm, no bundler, no framework, and no TypeScript. The entry point is `index.html`; everything else is a plain `.js` file in the `js/` folder. -**Current version:** v1.9.4 (updated in `index.html` footer, `README.md`, and `CHANGELOG.md` on every release). +**Current version:** v1.9.5 (updated in `index.html` footer, `README.md`, and `CHANGELOG.md` on every release). **What it does:** Imports CSV exports from Ally Bank (Checking + Savings) and Chase credit cards, parses and normalizes them into a unified transaction model, and renders a multi-tab financial dashboard with budgeting, debt tracking, savings forecasting, investment planning, and Claude-powered AI analysis. @@ -341,7 +341,7 @@ These rules exist because Chart.js will cause infinite layout loops if violated. 1. **Always** set `responsive: true, maintainAspectRatio: false`. 2. **Always** wrap `` in a `.chart-wrap` div (or `.spark-wrap` for sparklines). 3. **Always** initialize charts inside `setTimeout(() => { new Chart(...) }, 0)` — the canvas must be in the DOM first. -4. **Always** destroy a chart before recreating it on the same canvas — or use a unique canvas ID per render. +4. **Always** destroy a chart before recreating it on the same canvas — or use a unique canvas ID per render. `ui.js` enforces this: `Chart` is a guard subclass that destroys any chart already on the canvas, and any chart whose canvas has left the page, before drawing. Keep it. Without it, two renders in a row throw "Canvas is already in use" and every render leaks its charts. 5. **Never** let a chart container have `height: auto` — always an explicit pixel height. ```js @@ -785,7 +785,7 @@ Multiple Chase card files can be loaded simultaneously. Each gets a unique `acco --- -*Last updated: v1.9.4 — 2026-10-25 (views-db.js changelog reads CRLF checkouts; v1.9.3: §14 folder permission: re-request on click, Reconnect banner, browser copy wins; v1.9.2: §0 branch, PR and approval workflow added; v1.9.1: §4.5 rehydrate transactions loaded from JSON; v1.9.0: §20 Data & Reset added; v1.8.1: §10 re-upload dedup rewritten for the occurrence-suffix fix; v1.8.0: AI Analysis restored with API-key and output hardening; `renderMarkdown()` added; `samples/` added; §1–3, §13, §14, §17 corrected to match the code)* +*Last updated: v1.9.5 — 2026-10-25 (§7 chart guard; v1.9.4: views-db.js changelog reads CRLF checkouts; v1.9.3: §14 folder permission: re-request on click, Reconnect banner, browser copy wins; v1.9.2: §0 branch, PR and approval workflow added; v1.9.1: §4.5 rehydrate transactions loaded from JSON; v1.9.0: §20 Data & Reset added; v1.8.1: §10 re-upload dedup rewritten for the occurrence-suffix fix; v1.8.0: AI Analysis restored with API-key and output hardening; `renderMarkdown()` added; `samples/` added; §1–3, §13, §14, §17 corrected to match the code)* --- diff --git a/README.md b/README.md index e007294..c825f15 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ > A zero-install, browser-native finance dashboard. Drop in your bank CSVs, get instant clarity on your money. -**Version:** v1.9.4  ·  **Stack:** Vanilla JS, HTML, CSS  ·  **Install:** None +**Version:** v1.9.5  ·  **Stack:** Vanilla JS, HTML, CSS  ·  **Install:** None --- @@ -235,4 +235,4 @@ See [CHANGELOG.md](CHANGELOG.md) for the full release history. --- -*FinLens v1.9.4 — Built for one person's finances, designed to be useful for anyone's.* +*FinLens v1.9.5 — Built for one person's finances, designed to be useful for anyone's.* diff --git a/index.html b/index.html index 221bd42..7e87f88 100644 --- a/index.html +++ b/index.html @@ -326,7 +326,7 @@
⚙️ Settings
diff --git a/js/ui.js b/js/ui.js index d8b1eff..782404f 100644 --- a/js/ui.js +++ b/js/ui.js @@ -40,6 +40,27 @@ function applyTheme() { } } +/* ---------- CHART GUARD (CLAUDE.md §7, rule 4) ---------- */ +// Views draw charts in setTimeout(…, 0). When a view renders twice before those timers +// run (loadFiles() then showView() at boot, or Reconnect re-rendering after an import), +// both timers target the same canvas IDs and Chart.js throws "Canvas is already in use" +// for the second. Every `new Chart(...)` goes through this subclass, which destroys a +// chart already on the canvas first. It also destroys charts whose canvas has left the +// page: views rebuild their DOM on every render (§12) and never destroyed the old charts, +// so each render leaked them. Statics (defaults, getChart, register) are inherited. +if (typeof Chart !== 'undefined' && !Chart._finlensGuard) { + const ChartJS = Chart; + Chart = class extends ChartJS { + constructor(item, config) { + for (const c of Object.values(ChartJS.instances)) if (!c.canvas || !c.canvas.isConnected) c.destroy(); + const existing = ChartJS.getChart(item); + if (existing) existing.destroy(); + super(item, config); + } + }; + Chart._finlensGuard = true; +} + /* ---------- CHART PALETTE ---------- */ const CHART_PALETTE = ['#00C49A','#3B82F6','#8B5CF6','#F59E0B','#EF4444','#94A3B8','#F472B6','#FCD34D','#34D399','#7DD3FC','#FB923C','#A78BFA']; const CHART_DEFAULTS = { diff --git a/js/views-settings.js b/js/views-settings.js index ddd6acd..338309c 100644 --- a/js/views-settings.js +++ b/js/views-settings.js @@ -49,7 +49,7 @@ function _renderGeneralSettings(root) { -
FinLens v1.9.4 ·${State.transactions.length} transactions loaded${State.lastRefresh?' · Refreshed '+dayjs(State.lastRefresh).format('MMM D, YYYY h:mm A'):''}
`; +
FinLens v1.9.5 ·${State.transactions.length} transactions loaded${State.lastRefresh?' · Refreshed '+dayjs(State.lastRefresh).format('MMM D, YYYY h:mm A'):''}
`; root.appendChild(displayCard); /* ---- Anthropic API Key ---- */