Skip to content

Restore LSP request cancellation - #5172

Merged
crtschin merged 2 commits into
haskell:masterfrom
crtschin:crtschin/fix-request-cancellation
Nov 16, 2026
Merged

crtschin merged 2 commits into
haskell:masterfrom
crtschin:crtschin/fix-request-cancellation

Conversation

@crtschin

@crtschin crtschin commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #5171.

Supports $/cancelRequest again. This implements it as the following:

  1. On receiving a request, insert the request id into a Map of pending ids.
  2. On receiving a $/cancelRequest, flag that id in the map as cancelled.
  3. We then race the handler of the request against waiting for that cancellation flag.

  • I read and followed our AI policy
  • I proposed a solution draft in the corresponding issue (for non-trivial changes and new features)
    • This is especially encouraged for new contributors

@crtschin crtschin changed the title WIP: Restore LSP request cancellation Restore LSP request cancellation Nov 10, 2026
@crtschin
crtschin marked this pull request as ready for review November 10, 2026 20:30
@crtschin
crtschin requested a review from wz1000 as a code owner November 10, 2026 20:30
@DeeKahy

DeeKahy commented Nov 12, 2026

Copy link
Copy Markdown

Tried this out and it fixes my case.

I built HLS from this commit and ran three eval loops, then sent $/cancelRequest. Before (2.13.0.0), HLS never replied and the loop kept going. With this PR, it replies RequestCancelled about a second later and the loop stops.

  • -- >>> forever (appendFile "/tmp/hb" "x" >> threadDelay 100000)
  • -- >>> let go :: Int -> IO (); go n = (if n mod 20000000 == 0 then appendFile "/tmp/hb" "x" else return ()) >> go (n + 1) in go 0
  • -- >>> let spin :: Int -> Int; spin n = spin (n + 1) in spin 0

Thanks for putting in the effort! <3

@fendor fendor 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.

LGTM, some clarification questions

Comment on lines +275 to +277
cancelled <- atomically isCancelled
if cancelled
then onCancel

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.

Why this initial check? This checks this is cancelled before we are starting to respond to the request.

Please add a comment, I imagine we are checking whether to even start the request if it had already been cancelled, but why only on startup, if we race them against each other?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Good point, it's not worth it. Dropped

Comment on lines +78 to +83
-- This has to be masked, otherwise the handler might get cancelled after
-- the request was deleted from the pending set, causing us to forget to
-- send a response.
mask_ $ do
wasPending <- liftIO $ atomically $ removePendingRequest pending sid
when wasPending $ (runReaderT . unServerM) (resp r) st

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.

Why would we ever not send a response?
Also, who might interrupt this?

@crtschin crtschin Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Also, who might interrupt this?

It would be waitForCancel that terminates this in-between the atomically . removePendingRequest and the when wasPending.

Why would we ever not send a response?

If a cancellation arrives between the above two lines. We end up removing the request from the pending request set, which is currently used as a signal to send a response.

In the other thread, as the request was cancelled, waitForCancel will proceed and check if it needs to respond with the cancellation response. But the request isn't in the pending set anymore.

Edit: Extended the comment to include this.

@crtschin
crtschin force-pushed the crtschin/fix-request-cancellation branch from 0159e51 to d85dec1 Compare November 16, 2026 20:17
@crtschin
crtschin force-pushed the crtschin/fix-request-cancellation branch from d85dec1 to 7fba209 Compare November 16, 2026 20:19
@crtschin
crtschin enabled auto-merge (squash) November 16, 2026 21:16
@crtschin
crtschin merged commit 95eb9af into haskell:master Nov 16, 2026
57 of 59 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.

Incorrectly ignore cancelRequest notifications

3 participants