Skip to content

Commit

Permalink
Fixing an issue leading to a race when proxying HTTP requests to the …
Browse files Browse the repository at this point in the history
…worker. (#10121)
  • Loading branch information
fabiocav committed May 8, 2024
1 parent a77902a commit 03ab073
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
3 changes: 2 additions & 1 deletion release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
- Update Node.js Worker Version to [3.10.0](https://github.com/Azure/azure-functions-nodejs-worker/releases/tag/v3.10.0) (#9999)
- Update PowerShell worker 7.2 to [4.0.3220](https://github.com/Azure/azure-functions-powershell-worker/releases/tag/v4.0.3220)
- Update PowerShell worker 7.4 to [4.0.3219](https://github.com/Azure/azure-functions-powershell-worker/releases/tag/v4.0.3219)
- Ensuring proxies are disabled, with a warning, when running in Flex Consumption.
- Ensuring proxies are disabled, with a warning, when running in Flex Consumption.
- Fixed an issue leading to a race when invocation responses returned prior to HTTP requests being sent in proxied scenarios.
13 changes: 8 additions & 5 deletions src/WebJobs.Script.Grpc/Channel/GrpcWorkerChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,14 @@ internal async Task SendInvocationRequest(ScriptInvocationContext context)
_executingInvocations.TryAdd(invocationRequest.InvocationId, new(context, _messageDispatcherFactory.Create(invocationRequest.InvocationId)));
_metricsLogger.LogEvent(string.Format(MetricEventNames.WorkerInvoked, Id), functionName: Sanitizer.Sanitize(context.FunctionMetadata.Name));

// If the worker supports HTTP proxying, ensure this request is forwarded prior
// to sending the invocation request to the worker, as this will ensure the context
// is properly set up.
if (IsHttpProxyingWorker && context.FunctionMetadata.IsHttpTriggerFunction())
{
_httpProxyService.StartForwarding(context, _httpProxyEndpoint);
}

await SendStreamingMessageAsync(new StreamingMessage
{
InvocationRequest = invocationRequest
Expand All @@ -900,11 +908,6 @@ internal async Task SendInvocationRequest(ScriptInvocationContext context)
var cancellationCtr = context.CancellationToken.Register(() => SendInvocationCancel(invocationRequest.InvocationId));
context.Properties.Add(ScriptConstants.CancellationTokenRegistration, cancellationCtr);
}

if (IsHttpProxyingWorker && context.FunctionMetadata.IsHttpTriggerFunction())
{
_ = _httpProxyService.ForwardAsync(context, _httpProxyEndpoint);
}
}
catch (Exception invokeEx)
{
Expand Down
4 changes: 1 addition & 3 deletions src/WebJobs.Script.Grpc/Server/DefaultHttpProxyService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public async Task EnsureSuccessfulForwardingAsync(ScriptInvocationContext contex
}
}

public Task ForwardAsync(ScriptInvocationContext context, Uri httpUri)
public void StartForwarding(ScriptInvocationContext context, Uri httpUri)
{
ArgumentNullException.ThrowIfNull(context);

Expand All @@ -89,8 +89,6 @@ public Task ForwardAsync(ScriptInvocationContext context, Uri httpUri)

var forwardingTask = _httpForwarder.SendAsync(httpContext, httpUri.ToString(), _messageInvoker, _forwarderRequestConfig).AsTask();
context.Properties.Add(ScriptConstants.HttpProxyTask, forwardingTask);

return forwardingTask;
}
}
}
7 changes: 6 additions & 1 deletion src/WebJobs.Script.Grpc/Server/IHttpProxyService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ public interface IHttpProxyService
{
Task EnsureSuccessfulForwardingAsync(ScriptInvocationContext context);

Task ForwardAsync(ScriptInvocationContext context, Uri httpUri);
/// <summary>
/// Initiates a request forward and updates the current context to track forwarding operation.
/// </summary>
/// <param name="context">The <see cref="ScriptInvocationContext"/> for the HTTP invocation.</param>
/// <param name="httpUri">The target URI used for forwarding.</param>
void StartForwarding(ScriptInvocationContext context, Uri httpUri);
}
}

0 comments on commit 03ab073

Please sign in to comment.