Skip to content

fix: debounce did_change diagnostics to prevent stale analysis backlog#761

Merged
psteinroe merged 1 commit into
supabase-community:mainfrom
mvanhorn:fix/744-debounce-did-change-diagnostics
Jun 22, 2026
Merged

fix: debounce did_change diagnostics to prevent stale analysis backlog#761
psteinroe merged 1 commit into
supabase-community:mainfrom
mvanhorn:fix/744-debounce-did-change-diagnostics

Conversation

@mvanhorn

Copy link
Copy Markdown
Contributor

Summary

  • Add a per-URL debounce map (Arc<Mutex<FxHashMap<Url, AbortHandle>>>) to Session that tracks one pending diagnostic task per open document.
  • Replace the direct session.update_diagnostics(url).await call in did_change with session.schedule_diagnostics(url), which cancels any in-flight task for that URL and spawns a new one that sleeps 300 ms before running analysis.
  • Add cancel_pending_diagnostics called from did_close so no stale diagnostics fire after a document is closed.
  • The existing is_stale_diagnostics version guard is retained as a second safety net for concurrent races.

Why this matters

Fixes #744. When editing large SQL files, every keystroke sends a textDocument/didChange notification and the server was calling update_diagnostics synchronously for each one. This queued dozens to hundreds of back-to-back analysis requests (some involving DB-backed typechecking), wasting resources on both server and database. With this change, only the final change in a burst triggers an analysis run - matching editor conventions and preventing the stale-analysis backlog described in the issue.

Testing

  • Rapid-fire did_change notifications for the same URL: only one update_diagnostics call fires after the burst, not one per change (the abort path resets the timer each time).
  • Two different URLs changing concurrently: each URL has its own debounce slot; neither blocks the other.
  • A single isolated did_change with no follow-up: diagnostics still publish after the 300 ms delay.
  • did_close while a debounce task is pending: cancel_pending_diagnostics aborts the task before the document is removed.
  • Build: cargo build -p pgls_lsp and cargo clippy -p pgls_lsp --all-targets pass with no new errors or warnings.

Add a per-URL debounce in Session that cancels any pending diagnostic
task when a new textDocument/didChange notification arrives, then spawns
a fresh 300 ms sleep before calling update_diagnostics. Only the final
change in a rapid-fire burst triggers a full analysis run.

The existing is_stale_diagnostics guard is retained as a second safety
net for any concurrent races. did_close now cancels a pending debounce
task for the URL before clearing the document, preventing stale
diagnostics from being published after the file is closed.

Fixes supabase-community#744

@psteinroe psteinroe left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

thanks for fixing this! also thanks for the comments :)

@psteinroe psteinroe merged commit 708863a into supabase-community:main Jun 22, 2026
9 checks passed
@mvanhorn

Copy link
Copy Markdown
Contributor Author

Thanks @psteinroe, follow-up landed - debouncing did_change diagnostics is a nice complement to the stale-version discard from the other PR.

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.

Large file edits trigger too many sequential analysis requests (no coalescing/cancellation)

2 participants