Skip to content

Keep cancellation token source cancellation idempotent - #1952

Merged
Dirk Bäumer (dbaeumer) merged 2 commits into
microsoft:mainfrom
pradeep-ramola:fix/idempotent-cancellation
Nov 24, 2026
Merged

Dirk Bäumer (dbaeumer) merged 2 commits into
microsoft:mainfrom
pradeep-ramola:fix/idempotent-cancellation

Conversation

@pradeep-ramola

Copy link
Copy Markdown
Contributor

Summary

Avoid calling cancel() on the immutable CancellationToken.Cancelled and CancellationToken.None sentinels.

CancellationTokenSource uses those objects when cancelled or disposed before its token is accessed. A later cancel() currently casts the sentinel to MutableToken and throws TypeError: this._token.cancel is not a function:

const source = new CancellationTokenSource();
source.cancel();
source.cancel(); // throws

const disposed = new CancellationTokenSource();
disposed.dispose();
disposed.cancel(); // throws

Only invoke cancel() when the stored token is actually a MutableToken, matching the existing type check in dispose(). Preserve the lazy immutable sentinels and the current behavior of materialized tokens.

Tests

Add four shared lifecycle tests and include their compiled module in the browser test bundle:

  • repeated cancellation before token access preserves CancellationToken.Cancelled;
  • cancellation after lazy disposal preserves CancellationToken.None;
  • a materialized token notifies listeners exactly once;
  • disposing materialized-token listeners still suppresses notifications during later cancellation.

Before the source change, the first two tests reproduce the TypeError and the two listener controls pass. All four now pass in Node.js and Chromium.

Validation

Based on current upstream main (898f44d0):

  • npm run compile:jsonrpc
  • npm --prefix jsonrpc run test:node -- --reporter dot (83 passing)
  • npm --prefix jsonrpc run lint (passed; existing Stylistic configuration deprecation notice)
  • npm --prefix jsonrpc run test:browser (17 passing in headless Chromium)
  • git diff --check

No public API, wire-format, package-version, or dependency changes. Other package suites and VS Code integration tests were not run. AI assistance was used to develop and validate this change.

Copilot AI balanced review requested due to automatic review settings November 23, 2026 00:55

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues are reported for the JSON-RPC cancellation idempotency change.

No open issues 🎉

What changed in this PR

Makes JSON-RPC cancellation idempotent by avoiding calls to cancel() on immutable token sentinels, while preserving existing behavior for materialized tokens.

Changes:

  • Restricts cancellation calls to MutableToken instances.
  • Adds four lifecycle tests covering repeated cancellation, disposal, and listener notifications.
  • Includes the shared cancellation tests in the browser test bundle.
File Description
jsonrpc/​src/​common/​test/​cancellation.test.ts Adds four cancellation lifecycle regression tests.
jsonrpc/​src/​common/​cancellation.ts Guards cancellation with a MutableToken instance check.
jsonrpc/​src/​browser/​test/​webpack.config.js Adds the compiled cancellation tests to the browser bundle.

Review effort: Balanced

@dbaeumer

Copy link
Copy Markdown
Member

Copilot resolve the merge conflicts in this pull request

1 similar comment
@dbaeumer

Copy link
Copy Markdown
Member

Copilot resolve the merge conflicts in this pull request

Copilot AI balanced review requested due to automatic review settings November 24, 2026 16:17

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues remain for the changes intended to make JSON-RPC cancellation idempotent.

No open issues 🎉

Review effort: Balanced

@dbaeumer
Dirk Bäumer (dbaeumer) merged commit 67a2e79 into microsoft:main Nov 24, 2026
4 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.

4 participants