neoclide/coc.nvim · info · CancellationError

Request cancelled

Error message

Request cancelled

What it means

The diagnostics provider throws CancellationError when the cancellation token was fired while awaiting the DocumentDiagnosticRequest response. This avoids processing a stale diagnostic report for a document/version that is no longer relevant.

Source

Thrown at src/language-client/diagnostic.ts:486

  private createProvider(): DiagnosticProvider {
    const provider: DiagnosticProvider = {
      onDidChangeDiagnostics: this.onDidChangeDiagnosticsEmitter.event,
      provideDiagnostics: (document, previousResultId, token) => {
        const middleware = this.client.middleware!
        const client = this._client
        const provideDiagnostics: ProvideDiagnosticSignature = (document, previousResultId, token) => {
          const uri = client.code2ProtocolConverter.asUri(document instanceof URI ? document : URI.parse(document.uri))
          const params: DocumentDiagnosticParams = {
            identifier: this.options.identifier,
            textDocument: { uri },
            previousResultId
          }
          return this.sendRequest(DocumentDiagnosticRequest.type, params, token, { kind: DocumentDiagnosticReportKind.Full, items: [] }).then(async result => {
            if (this.isDisposed) {
              return { kind: DocumentDiagnosticReportKind.Full, items: [] }
            }
            if (token.isCancellationRequested) {
              throw new CancellationError()
            }
            if (result === undefined || result === null) {
              return { kind: DocumentDiagnosticReportKind.Full, items: [] }
            }
            // make handleDiagnostics middleware works
            if (middleware.handleDiagnostics && result.kind == DocumentDiagnosticReportKind.Full) {
              middleware.handleDiagnostics(uri, result.items, (_, diagnostics) => {
                result.items = diagnostics
              })
            }
            return result
          })
        }
        return middleware.provideDiagnostics
          ? middleware.provideDiagnostics(document, previousResultId, token, provideDiagnostics)
          : provideDiagnostics(document, previousResultId, token)
      }
    }

View on GitHub (pinned to 50e974d969)

Solutions

  1. Treat CancellationError as expected in diagnostic provider wrappers
  2. Ensure the token source is only cancelled when diagnostics are truly invalidated
  3. Check server responsiveness; slow diagnostic responses increase cancellation windows
  4. Return a cached/stale report when cancellation races are frequent

Example fix

// before
const report = await provideDiagnostics(uri, token)
// after
const report = await provideDiagnostics(uri, token).catch(e => {
  if (e instanceof CancellationError) return { kind: DocumentDiagnosticReportKind.Full, items: [] }
  throw e
})
Defensive patterns

Strategy: try-catch

Validate before calling

if (token.isCancellationRequested) return { kind: DocumentDiagnosticReportKind.Full, items: [] }

Type guard

function isCancellationError(e: unknown): e is CancellationError {
  return e instanceof CancellationError
}

Try / catch

try {
  return await provideDiagnostics(uri, token)
} catch (e) {
  if (isCancellationError(e)) return { kind: DocumentDiagnosticReportKind.Full, items: [] }
  throw e
}

Prevention

When it happens

Trigger: provideDiagnostics issued a textDocument/diagnostic request; the token was cancelled (buffer changed, pull-diagnostics re-registered, or client disposed) while the request was in flight; on response the token.isCancellationRequested check throws.

Common situations: Rapid buffer switching with pull diagnostics enabled; server slow to answer documentDiagnostic requests; refresh interval firing while a previous pull is pending.

Related errors


AI-assisted analysis of neoclide/coc.nvim@50e974d969 (2026-08-31). Data as JSON: /api/errors/8461df8981559b48. Report an issue: GitHub.