Skip to content

Fix duplicate LSP server creation due to race condition#496

Merged
mehmetozguldev merged 1 commit into
athasdev:masterfrom
SnaetWarre:fix/lsp-duplicate-server-creation
Jan 3, 2026
Merged

Fix duplicate LSP server creation due to race condition#496
mehmetozguldev merged 1 commit into
athasdev:masterfrom
SnaetWarre:fix/lsp-duplicate-server-creation

Conversation

@SnaetWarre

Copy link
Copy Markdown
Contributor

Problem

The LSP system was creating duplicate server instances when multiple calls to startForFile occurred simultaneously. This resulted in two LSP server processes being spawned for the same workspace/language combination.

Root Cause

The server tracking key was added to the activeLanguageServers Set after the async backend call completed, creating a race condition window where multiple concurrent calls could all see an empty Set and proceed to create duplicate servers.

Solution

  • Moved server tracking before the backend invocation to eliminate the race condition
  • Added error handling to remove the server from tracking if the backend call fails, maintaining consistency

Changes

  • src/features/editor/lsp/lsp-client.ts: Reordered operations to add server key to Set before invoking backend, added try-catch for cleanup on failure
  • src-tauri/src/lsp/manager.rs: Removed debug instrumentation (cleanup only)

Testing

Verified with instrumented logs that:

  • Only ONE LSP server instance is created (instead of two)
  • Race condition is eliminated even with 5ms timing difference between calls
  • Deduplication works correctly for subsequent file opens
  • Error handling maintains consistency

Impact

  • Reduces resource usage by preventing duplicate LSP servers
  • Improves LSP reliability and performance
  • Eliminates confusing duplicate notification messages

- Move server tracking before backend invocation to prevent race condition
- Add error handling to maintain consistency if backend call fails
- Ensures only one LSP server instance is created per workspace/language
- Fixes issue where multiple simultaneous calls would bypass deduplication
@mehmetozguldev
mehmetozguldev self-requested a review January 3, 2026 13:18

@mehmetozguldev mehmetozguldev left a comment

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.

LGTM

@mehmetozguldev
mehmetozguldev merged commit c6aef33 into athasdev:master Jan 3, 2026
2 checks passed
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.

2 participants