Skip to content

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

In progress

Description

@robertpenner

Parent / source

What to build

Ensure $/cancelRequest notifications received by lsp.Server are never silently dropped while their target request is still waiting in s.requestQueue (before dispatchLoop dequeues it into s.pendingClientRequests).

Required observable behavior:

  • If a client sends request R followed by $/cancelRequest(R) while R is still queued in s.requestQueue, the server must not execute R with an uncanceled context and must respond with lsproto.ErrorCodeRequestCancelled (or honor cancellation before running the handler).
  • Existing cancellation behavior for requests already dispatched and executing asynchronously must remain intact.
  • Cancellation tracking must not leak memory for completed requests, out-of-order cancels for unknown IDs, or duplicate cancel notifications.
  • $/cancelRequest must continue to cancel in-flight requests without blocking behind slow synchronous notifications in s.requestQueue.

Decisions and constraints

  • Follow all shared constraints in End-to-end red-to-green TLA+ fixes and upstream PR prep for flagship TypeScript 7 concurrency bugs #1 (clean fix branch from main at e0fce98, no unsolicited PR against microsoft/TypeScript, official tla2tools.jar only).
  • Preserve LSP message ordering for non-cancel requests and notifications passing through s.requestQueue.
  • Leave internal representation choices (for example registering pendingClientRequests in readLoop vs tracking queued-cancel IDs) to the implementer, provided the green TLA+ model verifies no dropped cancellation, no deadlock, and bounded state cleanup.

Acceptance criteria

  1. Green TLA+ model: Add a fixed configuration (and updated spec transitions on tla-concurrency-verification) checked by tla2tools.jar / TLC that verifies NoDroppedCancellation, deadlock freedom, and quiescent map cleanup (0 errors across all reachable states), wired into verification/tla/scripts/run-tlc.sh.
  2. Go fix and regression tests: On a clean fix branch off main (e0fce98), fix tsc/internal/lsp/server.go and add in-package regression test(s) covering:
    • $/cancelRequest arriving while the target request is queued behind synchronous notifications in s.requestQueue (returns ErrorCodeRequestCancelled, does not run hover/handler).
    • $/cancelRequest arriving for an already-executing async request.
    • $/cancelRequest for a nonexistent or already-completed request ID (no panic, no leaked map entry).
  3. Verification: go -C ./tsc test -race -timeout=60s ./internal/lsp/... passes (including the new regression tests), and npx hereby check:format and npx hereby lint pass for touched files.
  4. Upstream preparation: Include a concise ready-to-file upstream bug report and PR description (referencing the fix branch on robertpenner/TypeScript) in an issue comment when done.

Dependencies

None. Independently executable.

Starting references

  • tsc/internal/lsp/server.go:937-1075 (readLoop, cancelRequest, dispatchLoop)
  • tsc/internal/lsp/dynamic_queue.go
  • verification/tla/specs/LspServerCancel/LspServerCancel.tla
  • verification/tla/reproducers/lsp_cancel_repro_test.go

Activity

  1. added
    bug
    ready-for-agent
    on Nov 26, 2026
  2. robertpenner commented on Nov 26, 2026

    @robertpenner
    OwnerAuthor

    Just-in-Time Agent Brief (#2)

    • Codebase verified at: e0fce98 (main), 3c1c901 (tla-concurrency-verification).
    • Root cause confirmed: tsc/internal/lsp/server.go:994-1064 puts incoming client requests into s.requestQueue in readLoop without inserting them into s.pendingClientRequests, while readLoop handles $/cancelRequest immediately against s.pendingClientRequests. Any cancellation arriving before dispatchLoop dequeues the request is silently dropped.
    • Design for green model and fix:
      • Extend pendingClientRequest with canceled bool (cancel remains nil while queued).
      • Register req.ID != nil in s.pendingClientRequests inside readLoop before s.requestQueue.Put(ctx, req) (removing it on Put error).
      • In cancelRequest: if found with cancel != nil, call cancel() and delete; if found with cancel == nil, mark canceled = true; if absent, no-op (prevents unbounded growth on unknown or late cancels).
      • In dispatchLoop: after popping req, lock pendingClientRequestsMu; if marked canceled, delete, respond with ErrorCodeRequestCancelled, and skip running the handler; otherwise attach cancel to the entry. Because requestCtx is still created in dispatchLoop, s.GetLocale() remains ordered after preceding notifications.
    • Verification plan: Exhaustive TLC check (LspServerCancel_Fixed.cfg, 0 errors on NoDroppedCancellation, NoLeakedPendingEntries, and deadlock freedom), TDD regression tests under go -C ./tsc test -race ./internal/lsp/..., and gofmt / go vet.
  3. robertpenner commented on Nov 26, 2026

    @robertpenner
    OwnerAuthor

    Slice Complete: Red-to-Green Verified (#2)

    • Fix branch: fix/lsp-queued-cancel (based on main e0fce98)
    • Fork draft PR: opened on robertpenner/TypeScript (Closes #2)
    • Green TLA+ verification: LspServerCancel_Fixed.cfg passed with 0 errors (30 distinct states, 45 generated, deadlock checking enabled) on NoDroppedCancellation, NoLeakedPendingEntries, and CanceledRequestRespondsCancelled.
    • Go verification: go -C ./tsc test -race -count=1 ./internal/lsp/... passed (3/3 packages, including TestCancelRequestWhileQueuedInRequestQueue and TestCancelActiveRequestCancelsContext); gofmt and go vet clean.

    Draft Upstream Bug Report (for microsoft/TypeScript)

    Title: LSP $/cancelRequest is silently dropped when target request is still queued in Server.requestQueue

    Description:
    In tsc/internal/lsp/server.go, readLoop enqueues incoming client requests into s.requestQueue without adding them to s.pendingClientRequests, while dispatchLoop registers them in s.pendingClientRequests only after dequeuing. However, readLoop intercepts $/cancelRequest immediately (bypassing s.requestQueue) and looks up the ID in s.pendingClientRequests. During typing bursts when dispatchLoop is busy processing edits, queued requests sit in s.requestQueue, so $/cancelRequest misses s.pendingClientRequests, is silently discarded, and dispatchLoop subsequently executes the stale request with an uncanceled context.

    Registering queued request IDs in s.pendingClientRequests inside readLoop (with cancel == nil, marked canceled = true if canceled before dispatch) and short-circuiting with ErrorCodeRequestCancelled in dispatchLoop eliminates dropped cancellations without leaking unknown/duplicate cancel IDs or disturbing s.GetLocale() ordering.

    (Clean fix branch ready at robertpenner/TypeScript:fix/lsp-queued-cancel once an upstream issue is milestoned.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugready-for-agent

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions