Skip to content

Commit

Permalink
improve App Host shutdown (#3727)
Browse files Browse the repository at this point in the history
Improve sequence and exception handling for outpost shut down
  • Loading branch information
NiklasGustafsson authored Oct 10, 2024
1 parent 498854a commit dfd2391
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 26 deletions.
1 change: 0 additions & 1 deletion dotnet/samples/Hello/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public async Task Handle(ConversationClosed item)
Message = goodbye
}.ToCloudEvent(this.AgentId.Key);
await PublishEvent(evt).ConfigureAwait(false);
await Task.Delay(60000);
await App.ShutdownAsync();
}
public async Task<string> SayHello(string ask)
Expand Down
63 changes: 39 additions & 24 deletions dotnet/src/Microsoft.AutoGen.Agents/Client/AgentWorkerRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,21 @@ private async Task RunReadPump()
}
}
}
catch (Exception ex)
catch (OperationCanceledException)
{
// Time to shut down.
break;
}
catch (Exception ex) when (!_shutdownCts.IsCancellationRequested)
{
_logger.LogError(ex, "Error reading from channel.");
channel = RecreateChannel(channel);
}
catch
{
// Shutdown requested.
break;
}
}
}

Expand All @@ -113,34 +123,39 @@ private async Task RunWritePump()
var outboundMessages = _outboundMessagesChannel.Reader;
while (!_shutdownCts.IsCancellationRequested)
{
await outboundMessages.WaitToReadAsync().ConfigureAwait(false);

// Read the next message if we don't already have an unsent message
// waiting to be sent.
if (!outboundMessages.TryRead(out var message))
try
{
break;
}
await outboundMessages.WaitToReadAsync().ConfigureAwait(false);

while (!_shutdownCts.IsCancellationRequested)
{
try
// Read the next message if we don't already have an unsent message
// waiting to be sent.
if (!outboundMessages.TryRead(out var message))
{
await channel.RequestStream.WriteAsync(message, _shutdownCts.Token).ConfigureAwait(false);
break;
}
catch (Exception ex) when (!_shutdownCts.IsCancellationRequested)
{
_logger.LogError(ex, "Error writing to channel.");
channel = RecreateChannel(channel);
continue;
}
catch

while (!_shutdownCts.IsCancellationRequested)
{
// Shutdown requested.
await channel.RequestStream.WriteAsync(message, _shutdownCts.Token).ConfigureAwait(false);
break;
}
}
catch (OperationCanceledException)
{
// Time to shut down.
break;
}
catch (Exception ex) when (!_shutdownCts.IsCancellationRequested)
{
_logger.LogError(ex, "Error writing to channel.");
channel = RecreateChannel(channel);
continue;
}
catch
{
// Shutdown requested.
break;
}
}
}

Expand Down Expand Up @@ -286,10 +301,6 @@ void StartCore()
public async Task StopAsync(CancellationToken cancellationToken)
{
_shutdownCts.Cancel();
lock (_channelLock)
{
_channel?.Dispose();
}

_outboundMessagesChannel.Writer.TryComplete();

Expand All @@ -302,6 +313,10 @@ public async Task StopAsync(CancellationToken cancellationToken)
{
await writeTask.ConfigureAwait(false);
}
lock (_channelLock)
{
_channel?.Dispose();
}
}

public ValueTask SendRequest(RpcRequest request)
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/Microsoft.AutoGen.Agents/Client/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static async ValueTask ShutdownAsync()
{
throw new InvalidOperationException("Client not started");
}
await RuntimeApp!.StopAsync();
await ClientApp.StopAsync();
await RuntimeApp!.StopAsync();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ public async Task RunReadPump()
_gateway.OnReceivedMessageAsync(this, message).Ignore();
}
}
catch (OperationCanceledException)
{
}
finally
{
_shutdownCancellationToken.Cancel();
Expand All @@ -104,6 +107,9 @@ public async Task RunWritePump()
await ResponseStream.WriteAsync(message);
}
}
catch (OperationCanceledException)
{
}
finally
{
_shutdownCancellationToken.Cancel();
Expand Down

0 comments on commit dfd2391

Please sign in to comment.