C
C#•6mo ago
Natro

No error log - app quits (async problem?)

Hey, I am working on an app that has multiple background workers:
List<Tasks> tasks = [];
foreach (var item in items)
{
// Could be I am doing this wrong? I am learning async as I go
tasks.Add(Task
.Run(async () => await StepOne(foo))
.ContinueWith(async x => await StepTwo(boo));
}

await Task.WhenAll(tasks);
// End app
List<Tasks> tasks = [];
foreach (var item in items)
{
// Could be I am doing this wrong? I am learning async as I go
tasks.Add(Task
.Run(async () => await StepOne(foo))
.ContinueWith(async x => await StepTwo(boo));
}

await Task.WhenAll(tasks);
// End app
StepOne works correctly, in StepTwo I am calling OpenAI API via this NuGet (https://github.com/OkGoDoIt/OpenAI-API-dotnet).
// OAI is just wrapper around OpenAPI NuGet
await OAI.Service.TextToSpeech.SaveSpeechToFileAsync(new TextToSpeechRequest()
{
Input = "Lorem ipsum",
ResponseFormat = ResponseFormats.MP3,
Model = Model.TTS_HD,
Voice = Voices.Shimmer,
Speed = 1.1,
}, outputFile);
// OAI is just wrapper around OpenAPI NuGet
await OAI.Service.TextToSpeech.SaveSpeechToFileAsync(new TextToSpeechRequest()
{
Input = "Lorem ipsum",
ResponseFormat = ResponseFormats.MP3,
Model = Model.TTS_HD,
Voice = Voices.Shimmer,
Speed = 1.1,
}, outputFile);
On this line my app (Console application) just turns off - no error log, nothing. So I tried doing try-catch block - nothing. I debugged and figured out problem is here (L133): https://github.com/OkGoDoIt/OpenAI-API-dotnet/blob/5f7c23a928be39da87e89d5105a044ecb7401727/OpenAI_API/EndpointBase.cs#L133 It's being called with following:
req: {Method: POST, RequestUri: 'https://api.openai.com/v1/audio/speech', Version: 1.1, Content: System.Net.Http.StringContent, Headers:
{
Content-Type: application/json; charset=utf-8
}}
streaming: false
req: {Method: POST, RequestUri: 'https://api.openai.com/v1/audio/speech', Version: 1.1, Content: System.Net.Http.StringContent, Headers:
{
Content-Type: application/json; charset=utf-8
}}
streaming: false
Seems to be correct (calling other endpoint with same NuGet - e.g. chat completion works) I cloned the repo, put try-catch block around that line - still nothing. When trying to debug the HttpClient method my debugger is just jumping all around place without sense. I guess I would have to get the code of HttpClient locally and do it like that - but at that point I am wondering. Anyone has idea why this is happening? I expect it's because of how I use Tasks? I am clueless here, not getting any output or error anywhere :/
10 Replies
Natro
Natro•6mo ago
The reason was in fact using Task.Run instead of just putting awaits after each other. Why though?
Angius
Angius•6mo ago
Also, that ContinueWith()... Can't be sure that is the issue, but
foreach (var item in items)
{
// Could be I am doing this wrong? I am learning async as I go
tasks.Add(Task
.Run(async () => await StepOne(foo))
.ContinueWith(async x => await StepTwo(boo));
}
foreach (var item in items)
{
// Could be I am doing this wrong? I am learning async as I go
tasks.Add(Task
.Run(async () => await StepOne(foo))
.ContinueWith(async x => await StepTwo(boo));
}
can just be
foreach (var item in items)
{
// Could be I am doing this wrong? I am learning async as I go
tasks.Add(async () => {
await StepOne(foo);
await StepTwo(boo);
});
}
foreach (var item in items)
{
// Could be I am doing this wrong? I am learning async as I go
tasks.Add(async () => {
await StepOne(foo);
await StepTwo(boo);
});
}
Natro
Natro•6mo ago
Yeah, I figured I am doing something wrong the moment I used ContinueWith and had no clue what the 'x' is for I assume I can pass stuff from prev task?
Angius
Angius•6mo ago
Yep Basically,
Foo().ContinueWith(num => Bar(num));
Foo().ContinueWith(num => Bar(num));
is the pre-async version of
var num = await Foo();
Bar(num);
var num = await Foo();
Bar(num);
Natro
Natro•6mo ago
Oh that makes sense. Thank you 🙂 Do you know why the errors were not logged though?
Angius
Angius•6mo ago
¯\_(ツ)_/¯ With async code, it usually happens when something's fucky-wucky with that, like not awaiting a task or something But everything seems fine at a glance
Natro
Natro•6mo ago
Yeah, the only thing I changed was the continue with to await and it works now. Fun times
Angius
Angius•6mo ago
There you go lol Unfortunately, C# has its fair share of old APIs like that that should not be sued anymore Non-generic collections, WebClient, etc There's a nice analyzer, BannedSymbolsAnalyzer I believe it's called, that can help you ban anything you don't want from the code
Natro
Natro•6mo ago
Will check it out, thank you
Want results from more Discord servers?
Add your server
More Posts