Skip to content

Commit

Permalink
Avoid closure caused by local function (#31641)
Browse files Browse the repository at this point in the history
* Avoid closure caused by local function
- Made the local function static and passed state via parameters.
  • Loading branch information
davidfowl authored Apr 9, 2021
1 parent bd61ac7 commit 00b551e
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/SignalR/server/Core/src/Internal/DefaultHubDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,19 +334,30 @@ await SendInvocationError(hubMethodInvocationMessage.InvocationId, connection,
else
{
// Invoke or Send
async Task ExecuteInvocation()
static async Task ExecuteInvocation(DefaultHubDispatcher<THub> dispatcher,
ObjectMethodExecutor methodExecutor,
THub hub,
object?[] arguments,
IServiceScope scope,
IHubActivator<THub> hubActivator,
HubConnectionContext connection,
HubMethodInvocationMessage hubMethodInvocationMessage,
bool isStreamCall)
{
var logger = dispatcher._logger;
var enableDetailedErrors = dispatcher._enableDetailedErrors;

object? result;
try
{
result = await ExecuteHubMethod(methodExecutor, hub, arguments, connection, scope.ServiceProvider);
Log.SendingResult(_logger, hubMethodInvocationMessage.InvocationId, methodExecutor);
result = await dispatcher.ExecuteHubMethod(methodExecutor, hub, arguments, connection, scope.ServiceProvider);
Log.SendingResult(logger, hubMethodInvocationMessage.InvocationId, methodExecutor);
}
catch (Exception ex)
{
Log.FailedInvokingHubMethod(_logger, hubMethodInvocationMessage.Target, ex);
await SendInvocationError(hubMethodInvocationMessage.InvocationId, connection,
ErrorMessageHelper.BuildErrorMessage($"An unexpected error occurred invoking '{hubMethodInvocationMessage.Target}' on the server.", ex, _enableDetailedErrors));
Log.FailedInvokingHubMethod(logger, hubMethodInvocationMessage.Target, ex);
await dispatcher.SendInvocationError(hubMethodInvocationMessage.InvocationId, connection,
ErrorMessageHelper.BuildErrorMessage($"An unexpected error occurred invoking '{hubMethodInvocationMessage.Target}' on the server.", ex, enableDetailedErrors));
return;
}
finally
Expand All @@ -355,7 +366,7 @@ await SendInvocationError(hubMethodInvocationMessage.InvocationId, connection,
// And normal invocations handle cleanup below in the finally
if (isStreamCall)
{
await CleanupInvocation(connection, hubMethodInvocationMessage, hubActivator, hub, scope);
await dispatcher.CleanupInvocation(connection, hubMethodInvocationMessage, hubActivator, hub, scope);
}
}

Expand All @@ -366,7 +377,8 @@ await SendInvocationError(hubMethodInvocationMessage.InvocationId, connection,
await connection.WriteAsync(CompletionMessage.WithResult(hubMethodInvocationMessage.InvocationId, result));
}
}
invocation = ExecuteInvocation();

invocation = ExecuteInvocation(this, methodExecutor, hub, arguments, scope, hubActivator, connection, hubMethodInvocationMessage, isStreamCall);
}

if (isStreamCall || isStreamResponse)
Expand Down

0 comments on commit 00b551e

Please sign in to comment.