Skip to content

Commit 387ccf4

Browse files
authored
Improve error handling for invocation request cancellations (#9158)
1 parent 23ae5d2 commit 387ccf4

File tree

5 files changed

+32
-6
lines changed

5 files changed

+32
-6
lines changed

src/WebJobs.Script.Grpc/Channel/GrpcWorkerChannel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ internal async Task SendInvocationRequest(ScriptInvocationContext context)
738738
// do not send an invocation request if cancellation has been requested
739739
if (context.CancellationToken.IsCancellationRequested)
740740
{
741-
_workerChannelLogger.LogWarning("Cancellation has been requested. The invocation request with id '{invocationId}' is cancelled and will not be sent to the worker.", invocationId);
741+
_workerChannelLogger.LogWarning("Cancellation has been requested. The invocation request with id '{invocationId}' is canceled and will not be sent to the worker.", invocationId);
742742
context.ResultSource.TrySetCanceled();
743743
return;
744744
}

src/WebJobs.Script.WebHost/WebJobsScriptHostService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public async Task StartAsync(CancellationToken cancellationToken)
180180
}
181181
catch (OperationCanceledException ex)
182182
{
183-
_logger.LogWarning("Host startup operation has been cancelled", ex);
183+
_logger.LogWarning("Host startup operation has been canceled", ex);
184184

185185
if (cancellationToken.IsCancellationRequested)
186186
{
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using System;
5+
using Microsoft.Azure.WebJobs.Host;
6+
7+
namespace Microsoft.Azure.WebJobs.Script.Description
8+
{
9+
internal class FunctionInvocationCanceledException : FunctionInvocationException
10+
{
11+
public FunctionInvocationCanceledException(string invocationId, Exception innerException)
12+
: base($"The invocation request with id '{invocationId}' was canceled before the request was sent to the worker.", innerException) { }
13+
}
14+
}

src/WebJobs.Script/Description/Workers/WorkerFunctionInvoker.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,23 @@ protected override async Task<object> InvokeCore(object[] parameters, FunctionIn
9191
};
9292

9393
string invocationId = context.ExecutionContext.InvocationId.ToString();
94-
_logger.LogTrace($"Sending invocation id:{invocationId}");
94+
ScriptInvocationResult result = new();
95+
96+
_logger.LogTrace("Sending invocation id: '{id}", invocationId);
9597
await _functionDispatcher.InvokeAsync(invocationContext);
96-
var result = await invocationContext.ResultSource.Task;
98+
99+
try
100+
{
101+
result = await invocationContext.ResultSource.Task;
102+
}
103+
catch (OperationCanceledException ex)
104+
{
105+
// Only catch the exception when the task is cancelled, otherwise let it be handled by the ExceptionMiddleware
106+
throw new FunctionInvocationCanceledException(invocationId, ex);
107+
}
97108

98109
await BindOutputsAsync(triggerValue, context.Binder, result);
110+
99111
return result.Return;
100112
}
101113

test/WebJobs.Script.Tests/Workers/Rpc/GrpcWorkerChannelTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -450,11 +450,11 @@ public async Task SendInvocationRequest_SignalCancellation_WithoutCapability_NoA
450450
}
451451

452452
[Fact]
453-
public async Task SendInvocationRequest_CancellationAlreadyRequested_ResultSourceCancelled()
453+
public async Task SendInvocationRequest_CancellationAlreadyRequested_ResultSourceCanceled()
454454
{
455455
var cancellationWaitTimeMs = 3000;
456456
var invocationId = Guid.NewGuid();
457-
var expectedCancellationLog = $"Cancellation has been requested. The invocation request with id '{invocationId}' is cancelled and will not be sent to the worker.";
457+
var expectedCancellationLog = $"Cancellation has been requested. The invocation request with id '{invocationId}' is canceled and will not be sent to the worker.";
458458

459459
var cts = new CancellationTokenSource();
460460
cts.CancelAfter(cancellationWaitTimeMs);

0 commit comments

Comments
 (0)