Exit OnConnected early if Negotiate fails (#733)

This commit is contained in:
BrennanConroy 2017-08-17 23:34:45 -07:00 committed by GitHub
parent a359da0c44
commit b12451025f
2 changed files with 37 additions and 3 deletions

View File

@ -61,7 +61,10 @@ namespace Microsoft.AspNetCore.SignalR
var connectionContext = new HubConnectionContext(output, connection);
await ProcessNegotiate(connectionContext);
if (!await ProcessNegotiate(connectionContext))
{
return;
}
// Hubs support multiple producers so we set up this loop to copy
// data written to the HubConnectionContext's channel to the transport channel
@ -103,7 +106,7 @@ namespace Microsoft.AspNetCore.SignalR
}
}
private async Task ProcessNegotiate(HubConnectionContext connection)
private async Task<bool> ProcessNegotiate(HubConnectionContext connection)
{
while (await connection.Input.WaitToReadAsync())
{
@ -130,10 +133,12 @@ namespace Microsoft.AspNetCore.SignalR
connection.ProtocolReaderWriter = new HubProtocolReaderWriter(protocol, dataEncoder);
return;
return true;
}
}
}
return false;
}
private async Task RunHubAsync(HubConnectionContext connection)

View File

@ -39,6 +39,26 @@ namespace Microsoft.AspNetCore.SignalR.Tests
}
}
[Fact]
public async Task MissingNegotiateAndMessageSentFromHubConnectionCanBeDisposedCleanly()
{
var serviceProvider = CreateServiceProvider();
var endPoint = serviceProvider.GetService<HubEndPoint<SimpleHub>>();
using (var client = new TestClient())
{
// TestClient automatically writes negotiate, for this test we want to assume negotiate never gets sent
client.Connection.Transport.In.TryRead(out var item);
var endPointTask = endPoint.OnConnectedAsync(client.Connection);
// kill the connection
client.Dispose();
await endPointTask;
}
}
[Fact]
public async Task LifetimeManagerOnDisconnectedAsyncCalledIfLifetimeManagerOnConnectedAsyncThrows()
{
@ -1094,5 +1114,14 @@ namespace Microsoft.AspNetCore.SignalR.Tests
return base.OnConnectedAsync();
}
}
public class SimpleHub : Hub
{
public override async Task OnConnectedAsync()
{
await Clients.All.InvokeAsync("Send", $"{Context.ConnectionId} joined");
await base.OnConnectedAsync();
}
}
}
}