Skip to content

Commit

Permalink
Ignoring fatal exceptions in InvocationHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiocav committed Oct 17, 2024
1 parent 7b1bd29 commit bad0d7d
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 8 deletions.
10 changes: 3 additions & 7 deletions sdk/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@
- My change description (#PR/#issue)
-->

### Microsoft.Azure.Functions.Worker.Sdk 1.18.1
### Microsoft.Azure.Functions.Worker.Sdk <version>

- Updated `Microsoft.Azure.Functions.Worker.Sdk.Generators` reference to 1.3.4.
- Changed exception handling in function invocation path to ensure fatal exceptions bubble up.

### Microsoft.Azure.Functions.Worker.Sdk.Generators 1.3.4

- Changed `FunctionExecutorGenerator` to avoid generation of long `if`/`else` chains for apps with a large number of functions.

- Use full namespace for `Task.FromResult` in function metadata provider generator to avoid namespace conflict (#2681)
### Microsoft.Azure.Functions.Worker.Sdk.Generators <version>

- <entry>
31 changes: 31 additions & 0 deletions src/DotNetWorker.Core/ExceptionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;

namespace Microsoft.Azure.Functions.Worker.Core
{
internal static class ExceptionExtensions
{
public static bool IsFatal(this Exception? exception)
{
while (exception is not null)
{
if (exception
is (OutOfMemoryException and not InsufficientMemoryException)
or AppDomainUnloadedException
or BadImageFormatException
or CannotUnloadAppDomainException
or InvalidProgramException
or AccessViolationException)
{
return true;
}

exception = exception.InnerException;
}

return false;
}
}
}
3 changes: 2 additions & 1 deletion src/DotNetWorker.Grpc/Handlers/InvocationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Functions.Worker.Context.Features;
using Microsoft.Azure.Functions.Worker.Core;
using Microsoft.Azure.Functions.Worker.Grpc;
using Microsoft.Azure.Functions.Worker.Grpc.Features;
using Microsoft.Azure.Functions.Worker.Grpc.Messages;
Expand Down Expand Up @@ -113,7 +114,7 @@ public async Task<InvocationResponse> InvokeAsync(InvocationRequest request)

response.Result.Status = StatusResult.Types.Status.Success;
}
catch (Exception ex)
catch (Exception ex) when (!ex.IsFatal())
{
response.Result.Exception = _workerOptions.EnableUserCodeException ? ex.ToUserRpcException() : ex.ToRpcException();
response.Result.Status = StatusResult.Types.Status.Failure;
Expand Down
36 changes: 36 additions & 0 deletions test/DotNetWorkerTests/ExceptionExtensionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using Microsoft.Azure.Functions.Worker.Core;
using Xunit;

namespace Microsoft.Azure.Functions.Worker.Tests
{
public class ExceptionExtensionTests
{
[Theory]
[ClassData(typeof(ExceptionTestData))]
public void IsFatal_ReturnsTrueForFatalExceptions(Exception exception, bool isFatal)
{
var result = exception.IsFatal();

Assert.Equal(result, isFatal);
}

public class ExceptionTestData : TheoryData<Exception, bool>
{
public ExceptionTestData()
{
Add(new OutOfMemoryException(), true);
Add(new AppDomainUnloadedException(), true);
Add(new BadImageFormatException(), true);
Add(new CannotUnloadAppDomainException(), true);
Add(new InvalidProgramException(), true);
Add(new AccessViolationException(), true);
Add(new InsufficientMemoryException(), false);
Add(new Exception("test", new OutOfMemoryException()), true);
}
}
}
}

0 comments on commit bad0d7d

Please sign in to comment.