deepgram/deepgram-dotnet-sdk

Multiple calls to SetTimeout on the Deepgram client throws exceptions

Closed this issue · 3 comments

What is the current behavior?

When recreating the client and setting the timeout more than once(on different DeepgramClient instances), all calls after the first result in an exception.

Steps to reproduce

. Create a DeepgramClient
. Set the timeout to any value
. Dispose of the DeepgramClient
. Create a new DeepgramClient
. Set the timeout to any value that is not the same as in step 2.

Expected behavior

The timeout for the network call should be changed and no exception should be thrown

Please tell us about your environment

This error is generic and is not tied to any one platform. If it runs .NET, it is affected.

Thanks for raising this issue, @ThindalTV .

I have tried to reproduce it. But I'm not sure how you "Dispose of the DeepgramClient" (my experience with .NET is limited).

Here is my code:

using Deepgram.Clients;
using Deepgram.Models;
using Newtonsoft.Json;

namespace SampleApp
{
    class Program
    {
        static async Task Main(string[] args)
        {
            // Step 1: Create a DeepgramClient
            var credentials = new Credentials("");

            try
            {
                // Make a network call here (e.g., transcription request)
                var deepgramClient = new DeepgramClient(credentials);
                deepgramClient.SetHttpClientTimeout(TimeSpan.FromSeconds(30));

                var response1 = await deepgramClient.Transcription.Prerecorded.GetTranscriptionAsync(
                    new UrlSource("https://static.deepgram.com/examples/nasa-spacewalk-interview.wav"),
                    new PrerecordedTranscriptionOptions(){
                        Tier = "nova"
                    }
                );

                Console.WriteLine(JsonConvert.SerializeObject(response1));

                // Make another network call using a new DeepgramClient instance
                var deepgramClient2 = new DeepgramClient(credentials);
                deepgramClient2.SetHttpClientTimeout(TimeSpan.FromSeconds(60));

                var response2 = await deepgramClient2.Transcription.Prerecorded.GetTranscriptionAsync(
                    new UrlSource("https://static.deepgram.com/examples/nasa-spacewalk-interview.wav"),
                    new PrerecordedTranscriptionOptions(){
                        Tier = "nova"
                    }
                );
                Console.WriteLine(JsonConvert.SerializeObject(response2));
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Exception: {ex.Message}");
            }
        }
    }
}

With this code, I get the first response but then I see the exception: Exception: This instance has already started one or more requests. Properties can only be modified before sending the first request.

But I'm not doing anything to explicitly dispose of the first DeepgramClient. Can you advise me on how I should do that in .NET. I want to make sure I can reproduce the issue exactly as you have described it.

Hi Sandra.
You are correct, any call to the SettHttpClientTimeout after an operation will fail, with the same instance of the client or any other.
I specified dispose as dispose does not help - the problem is still there. Having it fail without a dispose would be a bit bad but not a showstopper, having it fail even when the first client is disposed is a bigger underlying issue that really shouldn't happen.
The normal pattern for something like the Deepgram client would be

using(var client = new DeepgramClient(credentials)) {
  // Use the client here
}
// DeepgramClient is now ready to be cleaned up by the system, and any unmanaged code has been disposed via the Dispose function on the client.

I'm working on a fix for this issue. :)

A customer reported that it's not possible to set the HTTP Timeout at all now.

When they try to set it to 300 it times out at 100

The timeout should be set as 300 seconds as per the following code and I believe I can confirm the timeout by printing it out to the log:

deepgram.SetHttpClientTimeout(TimeSpan.FromSeconds(ApiRequestTimeoutInSeconds));

_logger.LogInformation($"ApiRequestTimeoutInSeconds: {ApiRequestTimeoutInSeconds.ToString()}");

And the output from the log file:

2023-08-30 07:32:18.818 +10:00 [INF] BinConsulting.TOPS.TOPService.AsrWorker: ApiRequestTimeoutInSeconds: 300

Error:

Error while transcribing audio files: The request was canceled due to the configured HttpClient.Timeout of 100 seconds elapsing.
DeepgramClient deepgram =
    new DeepgramClient(
        new Credentials(
            ApiKey,
            ApiUrl,
            ApiUrlRequiresSsl));
deepgram.SetHttpClientTimeout(TimeSpan.FromSeconds(300));
transcript = await deepgram.Transcription.Prerecorded.GetTranscriptionAsync(
            new StreamSource(stream, "audio/mpeg"), 
            new PrerecordedTranscriptionOptions() 
            {
                Tier = "enhanced",
                //Model = "meeting",
                Version = "latest",
                Language = "en",
                Diarize = options.EnableSpeakerSeparation,
                NamedEntityRecognition = true,
                Numerals = true,
                Punctuate = true,
                Utterances = true,
            });