Skip to content

Commit bb7c4a9

Browse files
Require CancellationToken for client results (and fix cancel closing connection) (#43249)
* WIP default cancel timeout for invokes * change * remove default token and default timeout * fix build * Update src/SignalR/common/Protocols.NewtonsoftJson/src/Protocol/NewtonsoftJsonHubProtocol.cs
1 parent a81d189 commit bb7c4a9

28 files changed

+301
-93
lines changed

src/Components/Server/src/Microsoft.AspNetCore.Components.Server.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
<Compile Include="$(RepoRoot)src\SignalR\common\Shared\BinaryMessageParser.cs" LinkBase="BlazorPack" />
7070
<Compile Include="$(RepoRoot)src\SignalR\common\Shared\MemoryBufferWriter.cs" LinkBase="BlazorPack" />
7171
<Compile Include="$(RepoRoot)src\SignalR\common\Protocols.MessagePack\src\Protocol\MessagePackHubProtocolWorker.cs" LinkBase="BlazorPack" />
72+
<Compile Include="$(RepoRoot)src\SignalR\common\Shared\TryGetReturnType.cs" LinkBase="BlazorPack" />
7273

7374
<!-- MessagePack -->
7475
<Compile Include="$(MessagePackRoot)BufferWriter.cs" LinkBase="BlazorPack\MessagePack" />

src/Servers/Kestrel/Core/src/Microsoft.AspNetCore.Server.Kestrel.Core.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<Description>Core components of ASP.NET Core Kestrel cross-platform web server.</Description>
@@ -29,6 +29,7 @@
2929
<Compile Include="$(SharedSourceRoot)runtime\Http3\**\*.cs" Link="Shared\runtime\Http3\%(Filename)%(Extension)" />
3030
<Compile Include="$(SharedSourceRoot)Hpack\**\*.cs" Link="Shared\Hpack\%(Filename)%(Extension)" />
3131
<Compile Include="$(SharedSourceRoot)ServerInfrastructure\**\*.cs" />
32+
<Compile Include="$(SharedSourceRoot)CancellationTokenSourcePool.cs" />
3233
<Compile Include="$(RepoRoot)src\Shared\TaskToApm.cs" Link="Internal\TaskToApm.cs" />
3334
</ItemGroup>
3435

src/Servers/Kestrel/Core/src/Middleware/HttpsConnectionMiddleware.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Microsoft.AspNetCore.Connections;
1313
using Microsoft.AspNetCore.Connections.Features;
1414
using Microsoft.AspNetCore.Http.Features;
15+
using Microsoft.AspNetCore.Internal;
1516
using Microsoft.AspNetCore.Server.Kestrel.Core;
1617
using Microsoft.AspNetCore.Server.Kestrel.Core.Features;
1718
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal;

src/Servers/Kestrel/Core/src/Internal/CancellationTokenSourcePool.cs renamed to src/Shared/CancellationTokenSourcePool.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
using System.Collections.Concurrent;
55

6-
namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal;
6+
namespace Microsoft.AspNetCore.Internal;
77

88
internal sealed class CancellationTokenSourcePool
99
{

src/SignalR/clients/ts/FunctionalTests/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger<
232232
{
233233
try
234234
{
235-
var result = await hubContext.Clients.Client(id).InvokeAsync<int>("Result");
235+
var result = await hubContext.Clients.Client(id).InvokeAsync<int>("Result", cancellationToken: default);
236236
return result.ToString(CultureInfo.InvariantCulture);
237237
}
238238
catch (Exception ex)

src/SignalR/common/Protocols.Json/src/Microsoft.AspNetCore.SignalR.Protocols.Json.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<Compile Include="$(SignalRSharedSourceRoot)Utf8BufferTextReader.cs" Link="Utf8BufferTextReader.cs" />
1818
<Compile Include="$(SignalRSharedSourceRoot)Utf8BufferTextWriter.cs" Link="Utf8BufferTextWriter.cs" />
1919
<Compile Include="$(SignalRSharedSourceRoot)ReusableUtf8JsonWriter.cs" Link="ReusableUtf8JsonWriter.cs" />
20+
<Compile Include="$(SignalRSharedSourceRoot)TryGetReturnType.cs" Link="TryGetReturnType.cs" />
2021
</ItemGroup>
2122

2223
<ItemGroup>

src/SignalR/common/Protocols.Json/src/Protocol/JsonHubProtocol.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,16 @@ public ReadOnlyMemory<byte> GetMessageBytes(HubMessage message)
230230
else
231231
{
232232
// If we have an invocation id already we can parse the end result
233-
var returnType = binder.GetReturnType(invocationId);
234-
result = BindType(ref reader, input, returnType);
233+
var returnType = ProtocolHelper.TryGetReturnType(binder, invocationId);
234+
if (returnType is null)
235+
{
236+
reader.Skip();
237+
result = null;
238+
}
239+
else
240+
{
241+
result = BindType(ref reader, input, returnType);
242+
}
235243
}
236244
}
237245
else if (reader.ValueTextEquals(ItemPropertyNameBytes.EncodedUtf8Bytes))
@@ -408,8 +416,15 @@ public ReadOnlyMemory<byte> GetMessageBytes(HubMessage message)
408416

409417
if (hasResultToken)
410418
{
411-
var returnType = binder.GetReturnType(invocationId);
412-
result = BindType(ref resultToken, input, returnType);
419+
var returnType = ProtocolHelper.TryGetReturnType(binder, invocationId);
420+
if (returnType is null)
421+
{
422+
result = null;
423+
}
424+
else
425+
{
426+
result = BindType(ref resultToken, input, returnType);
427+
}
413428
}
414429

415430
message = BindCompletionMessage(invocationId, error, result, hasResult);

src/SignalR/common/Protocols.MessagePack/src/Microsoft.AspNetCore.SignalR.Protocols.MessagePack.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<Compile Include="$(SignalRSharedSourceRoot)BinaryMessageFormatter.cs" Link="BinaryMessageFormatter.cs" />
1313
<Compile Include="$(SignalRSharedSourceRoot)BinaryMessageParser.cs" Link="BinaryMessageParser.cs" />
1414
<Compile Include="$(SignalRSharedSourceRoot)MemoryBufferWriter.cs" Link="Internal\MemoryBufferWriter.cs" />
15+
<Compile Include="$(SignalRSharedSourceRoot)TryGetReturnType.cs" Link="TryGetReturnType.cs" />
1516
</ItemGroup>
1617

1718
<ItemGroup>

src/SignalR/common/Protocols.MessagePack/src/Protocol/MessagePackHubProtocolWorker.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,21 @@ private CompletionMessage CreateCompletionMessage(ref MessagePackReader reader,
162162
error = ReadString(ref reader, "error");
163163
break;
164164
case NonVoidResult:
165-
var itemType = binder.GetReturnType(invocationId);
166-
if (itemType == typeof(RawResult))
165+
var itemType = ProtocolHelper.TryGetReturnType(binder, invocationId);
166+
if (itemType is null)
167167
{
168-
result = new RawResult(reader.ReadRaw());
168+
reader.Skip();
169169
}
170170
else
171171
{
172-
result = DeserializeObject(ref reader, itemType, "argument");
172+
if (itemType == typeof(RawResult))
173+
{
174+
result = new RawResult(reader.ReadRaw());
175+
}
176+
else
177+
{
178+
result = DeserializeObject(ref reader, itemType, "argument");
179+
}
173180
}
174181
hasResult = true;
175182
break;

src/SignalR/common/Protocols.NewtonsoftJson/src/Microsoft.AspNetCore.SignalR.Protocols.NewtonsoftJson.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<Compile Include="$(SignalRSharedSourceRoot)Utf8BufferTextReader.cs" Link="Utf8BufferTextReader.cs" />
1616
<Compile Include="$(SignalRSharedSourceRoot)Utf8BufferTextWriter.cs" Link="Utf8BufferTextWriter.cs" />
1717
<Compile Include="$(SignalRSharedSourceRoot)MemoryBufferWriter.cs" Link="MemoryBufferWriter.cs" />
18+
<Compile Include="$(SignalRSharedSourceRoot)TryGetReturnType.cs" Link="TryGetReturnType.cs" />
1819
</ItemGroup>
1920

2021
<ItemGroup>

0 commit comments

Comments
 (0)