Merge pull request #11353 from jtattermusch/csharp_dont_start_with_unbound_ports

Don't start a server if there's unbound ports (for v1.4.x branch)
pull/11373/head
Jan Tattermusch 8 years ago committed by GitHub
commit bf10926784
  1. 16
      src/csharp/Grpc.Core.Tests/ServerTest.cs
  2. 19
      src/csharp/Grpc.Core/Server.cs

@ -32,6 +32,7 @@
#endregion #endregion
using System; using System;
using System.IO;
using System.Linq; using System.Linq;
using Grpc.Core; using Grpc.Core;
using Grpc.Core.Internal; using Grpc.Core.Internal;
@ -80,6 +81,21 @@ namespace Grpc.Core.Tests
server.ShutdownAsync().Wait(); 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] [Test]
public void CannotModifyAfterStarted() public void CannotModifyAfterStarted()
{ {

@ -34,6 +34,7 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Grpc.Core.Internal; using Grpc.Core.Internal;
@ -155,6 +156,7 @@ namespace Grpc.Core
/// <summary> /// <summary>
/// Starts the server. /// Starts the server.
/// Throws <c>IOException</c> if not successful.
/// </summary> /// </summary>
public void Start() public void Start()
{ {
@ -164,6 +166,7 @@ namespace Grpc.Core
GrpcPreconditions.CheckState(!shutdownRequested); GrpcPreconditions.CheckState(!shutdownRequested);
startRequested = true; startRequested = true;
CheckPortsBoundSuccessfully();
handle.Start(); handle.Start();
for (int i = 0; i < requestCallTokensPerCq; i++) for (int i = 0; i < requestCallTokensPerCq; i++)
@ -316,6 +319,22 @@ namespace Grpc.Core
} }
} }
/// <summary>
/// Checks that all ports have been bound successfully.
/// </summary>
private void CheckPortsBoundSuccessfully()
{
lock (myLock)
{
var unboundPort = ports.FirstOrDefault(port => port.BoundPort == 0);
if (unboundPort != null)
{
throw new IOException(
string.Format("Failed to bind port \"{0}:{1}\"", unboundPort.Host, unboundPort.Port));
}
}
}
private void DisposeHandle() private void DisposeHandle()
{ {
var activeCallCount = activeCallCounter.Count; var activeCallCount = activeCallCounter.Count;

Loading…
Cancel
Save