pyrefly icon indicating copy to clipboard operation
pyrefly copied to clipboard

Fix diagnostic race condition when switching Python interpreters

Open kinto0 opened this issue 4 weeks ago • 1 comments

Summary: When switching Python interpreters via workspace/didChangeConfiguration, the LSP would sometimes show stale diagnostics because of a race condition in how config invalidation and diagnostic publishing interacted.

The issue was that invalidate_config_and_validate_in_memory() would:

  1. Invalidate configs and mark modules as dirty
  2. Commit the transaction with updated configs
  3. Send RecheckFinished event to trigger diagnostic publishing

However, the RecheckFinished event handler could run and publish diagnostics before modules had actually re-run with the new config, resulting in diagnostics that still showed errors from the old interpreter's site-packages.

The fix is to publish diagnostics immediately after committing the config invalidation transaction, using a fresh transaction that reflects the newly committed state. This ensures diagnostics always use the updated configs.

Additionally, extracted the diagnostic publishing logic into a reusable publish_diagnostics_for_handles() method to avoid code duplication.

This likely helps with https://github.com/facebook/pyrefly/issues/1667

Differential Revision: D87848835

kinto0 avatar Nov 25 '25 06:11 kinto0

@kinto0 has exported this pull request. If you are a Meta employee, you can view the originating Diff in D87848835.

meta-codesync[bot] avatar Nov 25 '25 06:11 meta-codesync[bot]