Browse Source

improve App Host shutdown (#3727)

Improve sequence and exception handling for outpost shut down
tags/v0.4.0.dev1
Niklas Gustafsson GitHub 1 year ago
parent
commit
dfd239105a
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
4 changed files with 46 additions and 26 deletions
  1. +0
    -1
      dotnet/samples/Hello/Program.cs
  2. +39
    -24
      dotnet/src/Microsoft.AutoGen.Agents/Client/AgentWorkerRuntime.cs
  3. +1
    -1
      dotnet/src/Microsoft.AutoGen.Agents/Client/App.cs
  4. +6
    -0
      dotnet/src/Microsoft.AutoGen.Agents/Runtime/WorkerProcessConnection.cs

+ 0
- 1
dotnet/samples/Hello/Program.cs View File

@@ -47,7 +47,6 @@ namespace Hello
Message = goodbye Message = goodbye
}.ToCloudEvent(this.AgentId.Key); }.ToCloudEvent(this.AgentId.Key);
await PublishEvent(evt).ConfigureAwait(false); await PublishEvent(evt).ConfigureAwait(false);
await Task.Delay(60000);
await App.ShutdownAsync(); await App.ShutdownAsync();
} }
public async Task<string> SayHello(string ask) public async Task<string> SayHello(string ask)


+ 39
- 24
dotnet/src/Microsoft.AutoGen.Agents/Client/AgentWorkerRuntime.cs View File

@@ -99,11 +99,21 @@ public sealed class AgentWorkerRuntime : IHostedService, IDisposable, IAgentWork
} }
} }
} }
catch (Exception ex)
catch (OperationCanceledException)
{
// Time to shut down.
break;
}
catch (Exception ex) when (!_shutdownCts.IsCancellationRequested)
{ {
_logger.LogError(ex, "Error reading from channel."); _logger.LogError(ex, "Error reading from channel.");
channel = RecreateChannel(channel); channel = RecreateChannel(channel);
} }
catch
{
// Shutdown requested.
break;
}
} }
} }


@@ -113,34 +123,39 @@ public sealed class AgentWorkerRuntime : IHostedService, IDisposable, IAgentWork
var outboundMessages = _outboundMessagesChannel.Reader; var outboundMessages = _outboundMessagesChannel.Reader;
while (!_shutdownCts.IsCancellationRequested) 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; 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; 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;
}
} }
} }


@@ -286,10 +301,6 @@ public sealed class AgentWorkerRuntime : IHostedService, IDisposable, IAgentWork
public async Task StopAsync(CancellationToken cancellationToken) public async Task StopAsync(CancellationToken cancellationToken)
{ {
_shutdownCts.Cancel(); _shutdownCts.Cancel();
lock (_channelLock)
{
_channel?.Dispose();
}


_outboundMessagesChannel.Writer.TryComplete(); _outboundMessagesChannel.Writer.TryComplete();


@@ -302,6 +313,10 @@ public sealed class AgentWorkerRuntime : IHostedService, IDisposable, IAgentWork
{ {
await writeTask.ConfigureAwait(false); await writeTask.ConfigureAwait(false);
} }
lock (_channelLock)
{
_channel?.Dispose();
}
} }


public ValueTask SendRequest(RpcRequest request) public ValueTask SendRequest(RpcRequest request)


+ 1
- 1
dotnet/src/Microsoft.AutoGen.Agents/Client/App.cs View File

@@ -49,7 +49,7 @@ public static class App
{ {
throw new InvalidOperationException("Client not started"); throw new InvalidOperationException("Client not started");
} }
await RuntimeApp!.StopAsync();
await ClientApp.StopAsync(); await ClientApp.StopAsync();
await RuntimeApp!.StopAsync();
} }
} }

+ 6
- 0
dotnet/src/Microsoft.AutoGen.Agents/Runtime/WorkerProcessConnection.cs View File

@@ -87,6 +87,9 @@ internal sealed class WorkerProcessConnection : IAsyncDisposable
_gateway.OnReceivedMessageAsync(this, message).Ignore(); _gateway.OnReceivedMessageAsync(this, message).Ignore();
} }
} }
catch (OperationCanceledException)
{
}
finally finally
{ {
_shutdownCancellationToken.Cancel(); _shutdownCancellationToken.Cancel();
@@ -104,6 +107,9 @@ internal sealed class WorkerProcessConnection : IAsyncDisposable
await ResponseStream.WriteAsync(message); await ResponseStream.WriteAsync(message);
} }
} }
catch (OperationCanceledException)
{
}
finally finally
{ {
_shutdownCancellationToken.Cancel(); _shutdownCancellationToken.Cancel();


Loading…
Cancel
Save