Skip to content

v1.9.5: fix "Canvas is already in use" errors and leaked charts - #9

Merged
gitluis merged 1 commit into
masterfrom
fix-chart-double-render
Oct 26, 2026
Merged

gitluis merged 1 commit into
masterfrom
fix-chart-double-render

Conversation

@gitluis

@gitluis gitluis commented Oct 25, 2026 •

Copy link
Copy Markdown
Owner

Stacked on #8 (fix-changelog-crlf), which is stacked on #7. Merge them in order. Released as v1.9.5 (bug fix, patch bump).

The bug

The console showed errors like this:

Canvas is already in use. Chart with ID '4' must be destroyed before the canvas with ID 'sp_ally_checking' can be reused.

They appeared once per Overview chart:

Found while investigating Luis's #3 review. It was already on master.

A second, quieter problem in the same area: every render leaked its charts.

Cause

  • Two renders in a row: views draw charts in setTimeout(…, 0) (CLAUDE.md §7). At startup, loadFiles() renders Overview, then init.js calls showView('overview') before those timers run. Both batches look up the same canvas IDs and find the same new canvases, so the second new Chart() throws.
  • The leak: views rebuild their DOM on every render, but nothing destroyed the previous charts. Chart.instances grew by one per chart per render (8 → 12 → 20 in the test), and each one kept its detached canvas and data alive.

Fix

A guard in ui.js, next to the other chart helpers, enforces §7 rule 4 ("always destroy a chart before recreating it on the same canvas") in one place:

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);
  }
};

How it was tested

Headless Chromium, run before and after the fix:

Check Before After
Startup re-import: no "Canvas is already in use" ❌ 4 errors ✅
Startup re-import: every Overview canvas has exactly one live chart ❌ 8 charts for 4 canvases ✅
renderActive() ×3 in a row: no errors, no extra charts ❌ ✅
Every view rendered twice back to back, light and dark ❌ ✅
Chart instances stay bounded after 46 renders n/a ✅
Chart.defaults.color still follows the theme ✅ ✅
#7 Reconnect restoring 257 transactions: no errors ❌ 4 errors ✅

Also checked:

To check

  • Reload FinLens with your folder connected and an empty database. Then click Reconnect folder after a browser restart. The console should show no "Canvas is already in use" errors, and the charts should look normal.

Generated by Claude Code

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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QoR1wxcaDjVVk3xmd5rRCt
@gitluis

gitluis commented Oct 26, 2026

Copy link
Copy Markdown
Owner Author

Review: tested ✅

Tested in Edge on Windows, opening index.html from disk, with the samples/ data in a test folder. The footer shows v1.9.5, and Chart._finlensGuard returns true.

Check Result
renderActive() ×3 on Overview ✅ No errors · charts: 4 | all on page: true
Every chart view rendered twice, ending on Overview (light theme) ✅ No errors · charts: 4 | all on page: true
Same check, dark theme ✅ Same result; chart labels readable
Forecast → Savings and → Investments: slider redraws after leaving and coming back ✅ Redraws, console clean
Reconnect with an empty browser, after restarting Edge ✅ "Restored from finlens-state.json" (257 transactions), charts normal, no errors · charts: 4 | all on page: true
Before: the same ×3 check on master (v1.9.4) ❌ 8 "Canvas is already in use" errors · charts: 8 | all on page: false

Screenshots

1. Setup: v1.9.5 footer, guard loaded
01-setup

2. Test 1: three renders in a row
02-test1-three-renders

3. Test 2: every chart view twice, light theme
03-test2-light

4. Test 2: every chart view twice, dark theme
04-test2-dark

5. Forecast → Savings: after leaving and coming back
05-forecast-savings

6. Forecast → Investments: after leaving and coming back
06-forecast-investments

7. Reconnect: after restarting Edge, empty browser
07-reconnect-banner

8. Reconnect: restored from finlens-state.json
08-reconnect-restored

9. Reconnect: chart check after restoring
09-reconnect-check

10. Before: the same check on master (v1.9.4)
10-before-master

Notes (non-blocking)

Charts that leave the page are freed on the next chart draw, not when you leave the view. Stopping on a view without charts (Settings, Transactions, Debt, AI Analysis) keeps the previous view's charts in memory until a view with charts draws. Trends → AI Analysis left 3, and opening Overview brought it back to 0. That's at most one view's charts, and nothing redraws them while they wait. No change needed.

In Edge, folder access lasts until the browser closes, not just one page load. For the Reconnect check, a reload didn't bring back the "Backups paused" banner. It only appeared after closing Edge completely and reopening the profile. CLAUDE.md §14 and the comment in init.js say access lapses on every page load. Worth correcting in a small docs PR; it doesn't affect this one.

Approved to merge.

@gitluis
gitluis merged commit dc80ff0 into master Oct 26, 2026
@gitluis
gitluis deleted the fix-chart-double-render branch October 26, 2026 02:04
gitluis pushed a commit that referenced this pull request Oct 26, 2026
PR #11 was branched before #9 (v1.9.5) merged, so the version strings,
CHANGELOG.md and the CLAUDE.md footer conflicted. Resolved by keeping
v1.9.6 everywhere, ordering the changelog 1.9.6 → 1.9.5 → 1.9.4, and
keeping master's v1.9.5 entry in the CLAUDE.md "Last updated" line.
No other changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019JQE8BsrWkojjpK1xAUMgr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants