Skip to content

Fix 500 when a toggle or refresh clears the chart cache mid-request - #209

Draft
posthog[bot] wants to merge 1 commit into
mainfrom
posthog-self-driving/fixdashboard-guard-shared-batch-caches-69e25a
Draft

posthog[bot] wants to merge 1 commit into
mainfrom
posthog-self-driving/fixdashboard-guard-shared-batch-caches-69e25a

Conversation

@posthog

@posthog posthog Bot commented Sep 17, 2026

Copy link
Copy Markdown

Problem

  • A user hit four 500s in 0.11 s on the demo dashboard: a chart request read a cache entry another request had just cleared.
  • FastHTML runs the sync handlers in a thread pool, so requests overlap. The lazy chart route /batch/{batch_name}/chart/{chart_index} indexed the shared df_cache, stats_cache, and chart_cache dicts directly and used a check-then-use on chart_cache.
  • A toggle (chart_cache.clear()), a refresh (deletes one batch), or a search (clear_batch_cache) landing mid-request removes the key, so the route raises KeyError and returns a 500 instead of a chart.
  • It recurs for anyone who flips a display toggle while a batch page is still loading its charts.

Changes

  • Read each cache once into a local and rebuild on a miss, so the request keeps its own references that a concurrent clear cannot remove — the pattern the index and anomaly-list routes already use.
  • Replace the chart_cache check-then-use with setdefault plus a local fig; the render now uses that local instead of indexing the dict a second time.
  • calculate_metric_stats accepts an explicit dataframe and returns the stats, so the caller never re-reads a dict that may have been cleared.
  • Add a regression test that clears every cache during chart creation and asserts the route still renders. It reproduces the original KeyError on the old code.

Test plan

  • pytest tests/test_dashboard.py — 9 passed
  • ruff check and ruff format clean on the changed files
  • Confirmed the new test fails with KeyError against the pre-fix render path and passes after

Created with PostHog Desktop from this inbox report.

The lazy chart route read the shared df_cache, stats_cache, and chart_cache
dicts with direct indexing and a check-then-use pattern. FastHTML runs the sync
handlers in a thread pool, so a toggle, refresh, or search that clears a cache
between the check and the read makes the route raise KeyError and return a 500.

Read each cache once into a local and rebuild on a miss, so the request holds
its own references that a concurrent clear cannot remove. calculate_metric_stats
now accepts an explicit dataframe and returns the stats.

Add a regression test that clears every cache during chart creation and asserts
the route still renders.

Generated-By: PostHog Desktop
Task-Id: 6e8d2f02-9a52-45fd-a954-48b673129c29
@coderabbitai

coderabbitai Bot commented Sep 17, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: b7a971dd-a870-4374-a950-7d85ef0d2248

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions

Copy link
Copy Markdown

📊 Test Coverage Report

Coverage: 56% (yellow)

✅ Coverage maintained or improved!

💡 See detailed coverage report in the tests README

This branch has not been deployed

No deployments
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.

0 participants