Skip to content

fix(lsp): preserve $/cancelRequest for queued client requests - #7

Draft
robertpenner wants to merge 2 commits into
mainfrom
fix/lsp-queued-cancel
Draft

robertpenner wants to merge 2 commits into
mainfrom
fix/lsp-queued-cancel

Conversation

@robertpenner

Copy link
Copy Markdown
Owner

Closes #2

Summary

Fixes a race in lsp.Server (tsc/internal/lsp/server.go) where $/cancelRequest notifications were silently dropped when their target request was still waiting in s.requestQueue ahead of dispatchLoop.

Root Cause

readLoop previously enqueued incoming client requests into s.requestQueue without recording them in s.pendingClientRequests, while dispatchLoop populated s.pendingClientRequests only after dequeuing. Because readLoop intercepts $/cancelRequest immediately against s.pendingClientRequests, any cancellation arriving while the target request sat behind preceding edits or notifications in s.requestQueue missed s.pendingClientRequests and was discarded.

Fix

  • Extend pendingClientRequest with a canceled bool flag (cancel remains nil while queued).
  • Register non-nil request IDs in s.pendingClientRequests in readLoop before s.requestQueue.Put(ctx, req) (deleting on Put failure).
  • In cancelRequest: if an entry has a non-nil cancel, invoke it and delete; if queued (cancel == nil), mark canceled = true; if absent, no-op (no map growth on unknown or duplicate cancels).
  • In dispatchLoop: check pendingReq.canceled after popping; if already canceled while queued, delete the entry, respond immediately with lsproto.ErrorCodeRequestCancelled, and skip running the handler. requestCtx is still constructed in dispatchLoop, preserving locale ordering after preceding notifications.

Verification

  • TLA+ model (LspServerCancel_Fixed.cfg on tla-concurrency-verification): 0 errors across 30 distinct states (45 generated) with deadlock checking enabled, verifying NoDroppedCancellation, NoLeakedPendingEntries, and CanceledRequestRespondsCancelled.
  • go -C ./tsc test -race -count=1 ./internal/lsp/...: all 3 packages pass (lsp, lsproto, lspwatcher), including TestCancelRequestWhileQueuedInRequestQueue and TestCancelActiveRequestCancelsContext.
  • gofmt and go vet ./internal/lsp/...: clean.

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.

Fix dropped $/cancelRequest for queued LSP requests (tsc/internal/lsp/server.go)

1 participant