Skip to content

CancellationToken ignored or misused in several async methods #397

@rido-min

Description

@rido-min

Summary

Several async methods accept a CancellationToken parameter but either ignore it, pass CancellationToken.None instead, or use the wrong token. This means callers cannot cancel operations even when they provide a token.

Locations

Passes CancellationToken.None instead of the provided parameter

  • Teams.AI.Models.OpenAI/OpenAIChatModel.Send.cs:51ChatClient.CompleteChatAsync(chatMessages, requestOptions, CancellationToken.None) ignores the cancellationToken parameter received at line 27
  • Teams.AI.Models.OpenAI/OpenAIChatModel.Send.cs:92ChatClient.CompleteChatStreamingAsync(chatMessages, requestOptions, CancellationToken.None) ignores the cancellationToken parameter received at line 71

Uses wrong token (context property instead of parameter)

  • Teams.Apps/Contexts/Context.Send.cs:64await Sender.Send(activity, Ref, CancellationToken) uses the context's CancellationToken property instead of the cancellationToken method parameter

Token not propagated to underlying call

  • Teams.Common/Http/HttpClient.cs:60await _client.SendAsync(httpRequest) does not pass the available cancellationToken to the underlying HttpClient.SendAsync

Token accepted but unused

  • Teams.Common/Http/HttpClient.cs:121, 146CreateResponse methods accept CancellationToken but never use it

Missing from interface method

  • Teams.AI/ChatPlugin.cs:69OnBuildInstructions is the only method in the interface missing a CancellationToken parameter (all other methods have one)
  • Teams.AI/BaseChatPlugin.cs:39 — Corresponding base implementation also missing the parameter

Impact

  • OpenAI API calls are effectively uncancellable — request timeouts and graceful shutdown cannot abort in-flight calls
  • The Context.Send bug means per-call cancellation tokens passed by callers are silently ignored
  • HTTP calls in Teams.Common cannot be cancelled at the transport level

Suggested fix

  • Replace CancellationToken.None with the cancellationToken parameter
  • Fix the parameter shadowing in Context.Send.cs
  • Propagate the token through HttpClient.SendAsync and CreateResponse
  • Add CancellationToken to OnBuildInstructions interface method

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions