Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignoring fatal exceptions in InvocationHandler #2789

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}
}
Loading