Skip to content

Commit bdcf5c5

Browse files
committed
ensuring GrpcServer is only shut down once
1 parent 9fb8b40 commit bdcf5c5

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/WebJobs.Script.Grpc/Server/GrpcServer.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Linq;
6+
using System.Threading;
67
using System.Threading.Tasks;
78
using Grpc.Core;
89
using Microsoft.Azure.WebJobs.Script.Abstractions;
@@ -12,6 +13,7 @@ namespace Microsoft.Azure.WebJobs.Script.Grpc
1213
{
1314
public class GrpcServer : IRpcServer, IDisposable
1415
{
16+
private int _shutdown = 0;
1517
private Server _server;
1618
private bool _disposed = false;
1719
public const int MaxMessageLengthBytes = 128 * 1024 * 1024;
@@ -36,7 +38,16 @@ public Task StartAsync()
3638
return Task.CompletedTask;
3739
}
3840

39-
public Task ShutdownAsync() => _server.ShutdownAsync();
41+
public Task ShutdownAsync()
42+
{
43+
// The Grpc server will throw if it is shutdown multiple times.
44+
if (Interlocked.CompareExchange(ref _shutdown, 1, 0) == 0)
45+
{
46+
return _server.ShutdownAsync();
47+
}
48+
49+
return Task.CompletedTask;
50+
}
4051

4152
public Task KillAsync() => _server.KillAsync();
4253

@@ -46,7 +57,7 @@ protected virtual void Dispose(bool disposing)
4657
{
4758
if (disposing)
4859
{
49-
_server.ShutdownAsync();
60+
ShutdownAsync();
5061
}
5162
_disposed = true;
5263
}

0 commit comments

Comments
 (0)