Skip to content

fix(dashboard): prevent duplicate screenshot downloads#39525

Open
geido wants to merge 1 commit intomasterfrom
geido/fix-screenshot-dup-download
Open

fix(dashboard): prevent duplicate screenshot downloads#39525
geido wants to merge 1 commit intomasterfrom
geido/fix-screenshot-dup-download

Conversation

@geido
Copy link
Copy Markdown
Member

@geido geido commented Apr 21, 2026

SUMMARY

Fixes a race condition in useDownloadScreenshot where some users saw the dashboard screenshot (PDF/PNG) downloaded twice when triggered via the browser. The polling loop that waits for the generated screenshot had no in-flight guard, so interval ticks could stack concurrent GETs; when the image became ready, each in-flight request returned 200 and each triggered a.click().

Why it started happening recently — the polling logic was pre-existing, but #38880 (fix(dashboard): Ensure screenshot downloads always generate fresh images/pdfs) added force=true to the cache_dashboard_screenshot POST. Before, the backend could serve a cached screenshot immediately and no retries were needed. After, the async Celery task runs on every download, so the retry path is now the default, exposing the race on every request.

The fix — two guards in downloadScreenshot (both local to one invocation):

  • isFetchingfetchImageWithRetry short-circuits while a GET is still pending, so interval ticks can't stack concurrent requests. Reset in .finally.
  • isDownloaded — the success .then bails if already downloaded, and sets the flag + calls stopIntervals('success') before a.click() so any late-arriving 200 can't fire a second download.

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

N/A — network-level behavior. Reported pattern: multiple 200 responses for /screenshot/<cache_key>/?download_format=pdf each triggering a browser download.

TESTING INSTRUCTIONS

Unit tests added in useDownloadScreenshot.test.ts:

  • does not issue overlapping GETs while a previous GET is in-flight — verifies the isFetching guard by advancing fake timers while the first GET is pending.
  • triggers only one download when multiple successful responses race — verifies only one a.click() fires even when subsequent responses resolve 200.

Both tests fail against the current master behavior (click called 5× without the fix) and pass with the fix applied.

cd superset-frontend
npx jest src/dashboard/hooks/useDownloadScreenshot.test.ts

Manual: open a large dashboard → Download → PDF/PNG. Confirm exactly one file is downloaded on slow-generation paths.

ADDITIONAL INFORMATION

  • Has associated issue:
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • Migration is atomic, supports rollback & is backwards-compatible
    • Confirm DB migration upgrade and downgrade tested
    • Runtime estimates and downtime expectations provided
  • Introduces new feature or API
  • Removes existing feature or API

Add isFetching / isDownloaded guards in useDownloadScreenshot so
concurrent poll requests cannot each resolve with 200 and trigger
multiple a.click() downloads. Regression surfaced by #38880 which
forced async regeneration on every download, making the retry path
the default and exposing the pre-existing race.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@bito-code-review
Copy link
Copy Markdown
Contributor

bito-code-review Bot commented Apr 21, 2026

Code Review Agent Run #5bced2

Actionable Suggestions - 0
Review Details
  • Files reviewed - 2 · Commit Range: 90774c6..90774c6
    • superset-frontend/src/dashboard/hooks/useDownloadScreenshot.test.ts
    • superset-frontend/src/dashboard/hooks/useDownloadScreenshot.ts
  • Files skipped - 0
  • Tools
    • Eslint (Linter) - ✔︎ Successful
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers a full AI review.

  • /pause - Pauses automatic reviews on this pull request.

  • /resume - Resumes automatic reviews.

  • /resolve - Marks all Bito-posted review comments as resolved.

  • /abort - Cancels all in-progress reviews.

Refer to the documentation for additional commands.

Configuration

This repository uses Superset You can customize the agent settings here or contact your Bito workspace admin at evan@preset.io.

Documentation & Help

AI Code Review powered by Bito Logo

@dosubot dosubot Bot added change:frontend Requires changing the frontend dashboard:export Related to exporting dashboards labels Apr 21, 2026
Copy link
Copy Markdown
Member

@msyavuz msyavuz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with a small nit


test('downloadScreenshot calls API with force=true to ensure fresh screenshots', async () => {
const mockCacheKey = 'test-cache-key';
const RETRY_INTERVAL = 3000;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have this somewhere else as well that we can use?

@bito-code-review
Copy link
Copy Markdown
Contributor

The PR only modifies the test file for the useDownloadScreenshot hook. It doesn't show usage of the hook elsewhere in the codebase.

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 21, 2026

Codecov Report

❌ Patch coverage is 92.85714% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 64.50%. Comparing base (191337e) to head (90774c6).
⚠️ Report is 27 commits behind head on master.

Files with missing lines Patch % Lines
...ntend/src/dashboard/hooks/useDownloadScreenshot.ts 92.85% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #39525      +/-   ##
==========================================
+ Coverage   64.46%   64.50%   +0.04%     
==========================================
  Files        2559     2559              
  Lines      133564   133641      +77     
  Branches    31016    31049      +33     
==========================================
+ Hits        86097    86207     +110     
+ Misses      45974    45941      -33     
  Partials     1493     1493              
Flag Coverage Δ
javascript 66.55% <92.85%> (+0.07%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

change:frontend Requires changing the frontend dashboard:export Related to exporting dashboards size/L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants