From 9d56717be4fa5d0c05e42bb1137b30ef121960d8 Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Fri, 26 May 2017 14:19:23 -0700 Subject: [PATCH 1/3] throw if server started with unbound ports --- src/csharp/Grpc.Core/Server.cs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/csharp/Grpc.Core/Server.cs b/src/csharp/Grpc.Core/Server.cs index 63c1d9cd00f..7ba204a0b6f 100644 --- a/src/csharp/Grpc.Core/Server.cs +++ b/src/csharp/Grpc.Core/Server.cs @@ -34,6 +34,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Threading.Tasks; using Grpc.Core.Internal; @@ -155,6 +156,7 @@ namespace Grpc.Core /// /// Starts the server. + /// Throws IOException if not successful. /// public void Start() { @@ -163,7 +165,8 @@ namespace Grpc.Core GrpcPreconditions.CheckState(!startRequested); GrpcPreconditions.CheckState(!shutdownRequested); startRequested = true; - + + CheckPortsBoundSuccessfully(); handle.Start(); for (int i = 0; i < requestCallTokensPerCq; i++) @@ -316,6 +319,20 @@ namespace Grpc.Core } } + /// + /// Checks that all ports have been bound successfully. + /// + private void CheckPortsBoundSuccessfully() + { + lock (myLock) + { + if (!ports.All((port) => port.BoundPort != 0)) + { + throw new IOException("Failed to bind some of ports exposed by the server."); + } + } + } + private void DisposeHandle() { var activeCallCount = activeCallCounter.Count; From 67206428a41124abcde3798fa39728064ef59ff8 Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Fri, 26 May 2017 14:28:39 -0700 Subject: [PATCH 2/3] add StartThrowsWithUnboundPortTest --- src/csharp/Grpc.Core.Tests/ServerTest.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/csharp/Grpc.Core.Tests/ServerTest.cs b/src/csharp/Grpc.Core.Tests/ServerTest.cs index 3b51aa63300..5e95ed9ea61 100644 --- a/src/csharp/Grpc.Core.Tests/ServerTest.cs +++ b/src/csharp/Grpc.Core.Tests/ServerTest.cs @@ -32,6 +32,7 @@ #endregion using System; +using System.IO; using System.Linq; using Grpc.Core; using Grpc.Core.Internal; @@ -80,6 +81,21 @@ namespace Grpc.Core.Tests server.ShutdownAsync().Wait(); } + [Test] + public void StartThrowsWithUnboundPorts() + { + int twiceBoundPort = 9999; + Server server = new Server(new[] { new ChannelOption(ChannelOptions.SoReuseport, 0) }) + { + Ports = { + new ServerPort("localhost", twiceBoundPort, ServerCredentials.Insecure), + new ServerPort("localhost", twiceBoundPort, ServerCredentials.Insecure) + } + }; + Assert.Throws(typeof(IOException), () => server.Start()); + server.ShutdownAsync().Wait(); + } + [Test] public void CannotModifyAfterStarted() { From c4096469299bb9d417b7a3ebacea1dbfd31ed34f Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Thu, 1 Jun 2017 21:56:24 +0200 Subject: [PATCH 3/3] improve exception message --- src/csharp/Grpc.Core/Server.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/csharp/Grpc.Core/Server.cs b/src/csharp/Grpc.Core/Server.cs index 7ba204a0b6f..1fb5c62c8fb 100644 --- a/src/csharp/Grpc.Core/Server.cs +++ b/src/csharp/Grpc.Core/Server.cs @@ -326,9 +326,11 @@ namespace Grpc.Core { lock (myLock) { - if (!ports.All((port) => port.BoundPort != 0)) + var unboundPort = ports.FirstOrDefault(port => port.BoundPort == 0); + if (unboundPort != null) { - throw new IOException("Failed to bind some of ports exposed by the server."); + throw new IOException( + string.Format("Failed to bind port \"{0}:{1}\"", unboundPort.Host, unboundPort.Port)); } } }