Skip to content

Commit d9ea1e3

Browse files
authored
.Net - Performance Fix for Orchestration Result (#13317)
### Motivation and Context <!-- Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> Using synchronous `WaitAll` creates extra CPU load at scale. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Use asynchronous `WhenAny` with timeout for modern .NET and using `Delay` task for .netstandard. ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [X] The code builds clean without any errors or warnings - [X] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [X] All unit tests pass, and I have added new tests where possible - [ ] I didn't break anyone 😄
1 parent 1c11258 commit d9ea1e3

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

dotnet/src/Agents/Orchestration/OrchestrationResult.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,24 @@ public async ValueTask<TValue> GetValueAsync(TimeSpan? timeout = null, Cancellat
7373

7474
if (timeout.HasValue)
7575
{
76-
Task[] tasks = { this._completion.Task };
77-
if (!Task.WaitAll(tasks, timeout.Value))
76+
#if NET
77+
try
78+
{
79+
await this._completion.Task.WaitAsync(timeout.Value, cancellationToken).ConfigureAwait(false);
80+
}
81+
catch (TimeoutException)
82+
{
83+
this._logger.LogOrchestrationResultTimeout(this.Orchestration, this.Topic);
84+
throw;
85+
}
86+
#else
87+
Task completedTask = await Task.WhenAny(this._completion.Task, Task.Delay(timeout.Value, cancellationToken)).ConfigureAwait(false);
88+
if (completedTask != this._completion.Task)
7889
{
7990
this._logger.LogOrchestrationResultTimeout(this.Orchestration, this.Topic);
8091
throw new TimeoutException($"Orchestration did not complete within the allowed duration ({timeout}).");
8192
}
93+
#endif
8294
}
8395

8496
this._logger.LogOrchestrationResultComplete(this.Orchestration, this.Topic);

dotnet/src/Agents/UnitTests/Orchestration/OrchestrationResultTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ public async Task GetValueAsync_WithTimeout_ThrowsTimeoutException_WhenTaskDoesN
7777

7878
// Act & Assert
7979
TimeoutException exception = await Assert.ThrowsAsync<TimeoutException>(() => result.GetValueAsync(timeout).AsTask());
80-
Assert.Contains("Orchestration did not complete within the allowed duration", exception.Message);
8180
}
8281

8382
[Fact]

0 commit comments

Comments
 (0)