Skip to content

Remove unnecessary _connectCts field from McpClientImpl#1323

Merged
stephentoub merged 2 commits intomainfrom
copilot/remove-connect-cts-field
Feb 20, 2026
Merged

Remove unnecessary _connectCts field from McpClientImpl#1323
stephentoub merged 2 commits intomainfrom
copilot/remove-connect-cts-field

Conversation

Copy link
Contributor

Copilot AI commented Feb 20, 2026

_connectCts in McpClientImpl was a stored CancellationTokenSource that was never independently cancelled, never disposed (resource leak), and never read after being set. The actual initialization timeout is handled by the properly-scoped initializationCts (using + CancelAfter).

Changes

  • Removed private CancellationTokenSource? _connectCts field
  • Removed the two lines in ConnectAsync that created the linked CancellationTokenSource and reassigned cancellationToken — the method now uses the parameter directly
// Before
_connectCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
cancellationToken = _connectCts.Token;
try { ... }

// After
try { ... }
Original prompt

The _connectCts field in McpClientImpl is unnecessary and should be removed.

In src/ModelContextProtocol.Core/Client/McpClientImpl.cs, the field _connectCts (line 25) is declared as:

private CancellationTokenSource? _connectCts;

It is only used in ConnectAsync (lines 529-530):

_connectCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
cancellationToken = _connectCts.Token;

This field is unnecessary for the following reasons:

  1. Never cancelled independently. Nothing in the class ever calls _connectCts.Cancel(). The only way the linked token fires is if the original caller's cancellationToken is cancelled — which would have the same effect if cancellationToken were used directly.

  2. Never disposed. DisposeAsync() disposes _taskCancellationTokenProvider, _sessionHandler, and _transport, but never disposes _connectCts. This is a resource leak — CancellationTokenSource is IDisposable and holds native resources when linked.

  3. No external reader. Nothing else in the class reads _connectCts after ConnectAsync sets it. It's stored in a field but never referenced again.

  4. The real timeout mechanism is initializationCts. The actual initialization timeout logic uses a separate CancellationTokenSource (initializationCts) that is properly scoped with using and has CancelAfter applied.

Changes needed:

  1. Remove the _connectCts field declaration on line 25.
  2. In ConnectAsync, remove the two lines that create and use _connectCts (lines 529-530). The method should just use the cancellationToken parameter directly — the rest of the method already works correctly with that since initializationCts links to it.

The resulting ConnectAsync should start like:

public async Task ConnectAsync(CancellationToken cancellationToken = default)
{
    try
    {
        // We don't want the ConnectAsync token to cancel the message processing loop after we've successfully connected.
        // The session handler handles cancelling the loop upon its disposal.
        _ = _sessionHandler.ProcessMessagesAsync(CancellationToken.None);
        // ... rest unchanged

This pull request was created from Copilot chat.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Copilot AI changed the title [WIP] Remove unnecessary _connectCts field from McpClientImpl Remove unnecessary _connectCts field from McpClientImpl Feb 20, 2026
Copilot AI requested a review from stephentoub February 20, 2026 02:13
@stephentoub stephentoub marked this pull request as ready for review February 20, 2026 02:14
@stephentoub stephentoub enabled auto-merge (squash) February 20, 2026 02:14
@stephentoub stephentoub merged commit 49141a3 into main Feb 20, 2026
9 of 10 checks passed
@stephentoub stephentoub deleted the copilot/remove-connect-cts-field branch February 20, 2026 03:42
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.

3 participants

Comments