Skip to content

Cancel in-flight queries when panel scrolls out of view#1417

Open
setoh2000 wants to merge 1 commit intografana:mainfrom
setoh2000:pr-query-cancellation
Open

Cancel in-flight queries when panel scrolls out of view#1417
setoh2000 wants to merge 1 commit intografana:mainfrom
setoh2000:pr-query-cancellation

Conversation

@setoh2000
Copy link
Copy Markdown

Context

#1148 added lazy loading support: SceneQueryRunner no longer starts queries for panels that are outside the viewport when a time range change or variable change occurs.

However, there is still a gap: if a query is already running when the panel scrolls out of view, the request is left alive until it completes. This PR addresses that remaining case by cancelling in-flight queries as soon as a panel leaves the viewport.

Motivation

Grafana's Panel Repeat feature makes it easy to generate 100+ panels from a single template. When a user scrolls quickly through such a dashboard, each panel fires a query the moment it enters the viewport — even briefly. As the user scrolls past, all those queries remain in-flight — consuming browser connections, backpressure on the datasource, and delaying results for the panels actually on screen.

A concrete example: a dashboard with 100 repeated panels, each running a query that takes 100 ms+. Scrolling to the bottom means waiting for ~90 in-flight queries from off-screen panels to complete before the visible ones get their turn.

What this PR adds

A private _isQueryInFlight boolean is introduced to SceneQueryRunner to track whether a datasource query is currently running.

When isInViewChanged(false) is called while a query is in flight, the subscription is immediately unsubscribed and the panel is flagged to re-run when it returns to view — exactly the same behavior as when a variable changes while a panel is out of view.

Two async race condition guards are also added inside runWithTimeRange (after the await getDataSource() call) to handle the case where the viewport changes during the async setup phase.

@cla-assistant
Copy link
Copy Markdown

cla-assistant bot commented Apr 2, 2026

CLA assistant check
All committers have signed the CLA.

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.

1 participant