Skip to content

[BUG] GetStreamingResponseAsync cancellationToken is not working #744

@bharathm03

Description

@bharathm03

Describe the bug

I'm using AzureOpenAIClient, I've set timeout for streaming response using CancellationTokenSource.CancelAfter. But sometimes the timeout is not happening. The program struck at await foreach (ChatResponseUpdate update in stream.WithCancellation(cts.Token))

Steps to reproduce

Complete code

using var overallCts = new CancellationTokenSource(TimeSpan.FromMinutes(2));
using var cts = CancellationTokenSource.CreateLinkedTokenSource(overallCts.Token, cancellationToken);
cts.CancelAfter(TimeSpan.FromSeconds(45)); // initial timeout of 45 seconds
var stream = client.GetStreamingResponseAsync(messages, options, cts.Token);
await foreach (ChatResponseUpdate update in stream.WithCancellation(cts.Token))
{
  cts.CancelAfter(TimeSpan.FromSeconds(15));
  // my code
}

Sometimes code struck at await foreach forever. None of the other logs were triggered within the loop or outside the loop. What could be reason?

Code snippets

OS

Azure App Service (Linux)

.NET version

9

Library version

2.3.0-beta.2

Metadata

Metadata

Assignees

Labels

area: responsesThis item is related to Responsesneeds-author-feedbackWorkflow: More information is needed from author to address the issue.questionCategory: The issue is seeking information about the library or its usage.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions