diff --git a/extensions/Worker.Extensions.Http.AspNetCore/release_notes.md b/extensions/Worker.Extensions.Http.AspNetCore/release_notes.md index 97bb2a3ba..f56e86df4 100644 --- a/extensions/Worker.Extensions.Http.AspNetCore/release_notes.md +++ b/extensions/Worker.Extensions.Http.AspNetCore/release_notes.md @@ -6,7 +6,7 @@ ### Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore -- +- Update `ContextReference` to no longer use a given invocation's cancellation token (#2894) ### Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore.Analyzers diff --git a/extensions/Worker.Extensions.Http.AspNetCore/src/Coordinator/ContextReference.cs b/extensions/Worker.Extensions.Http.AspNetCore/src/Coordinator/ContextReference.cs index ddaac3487..9dcd8b026 100644 --- a/extensions/Worker.Extensions.Http.AspNetCore/src/Coordinator/ContextReference.cs +++ b/extensions/Worker.Extensions.Http.AspNetCore/src/Coordinator/ContextReference.cs @@ -8,7 +8,7 @@ namespace Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore { - internal class ContextReference : IDisposable + internal class ContextReference { private readonly TaskCompletionSource _functionStartTask = new(); private readonly TaskCompletionSource _functionCompletionTask = new(); @@ -16,9 +16,6 @@ internal class ContextReference : IDisposable private TaskCompletionSource _httpContextValueSource = new(); private TaskCompletionSource _functionContextValueSource = new(); - private CancellationToken _token; - private CancellationTokenRegistration _tokenRegistration; - public ContextReference(string invocationId) { InvocationId = invocationId; @@ -32,18 +29,6 @@ public ContextReference(string invocationId) public TaskCompletionSource FunctionContextValueSource { get => _functionContextValueSource; set => _functionContextValueSource = value; } - internal void SetCancellationToken(CancellationToken token) - { - _token = token; - _tokenRegistration = _token.Register(() => - { - _functionStartTask.TrySetCanceled(); - _functionCompletionTask.TrySetCanceled(); - _functionContextValueSource.TrySetCanceled(); - _httpContextValueSource.TrySetCanceled(); - }); - } - internal Task InvokeFunctionAsync() { _functionStartTask.SetResult(true); @@ -59,27 +44,12 @@ internal void CompleteFunction() if (_httpContextValueSource.Task.IsCompleted) { - if (_httpContextValueSource.Task.IsCanceled || _token.IsCancellationRequested) - { - _functionCompletionTask.TrySetCanceled(); - } - else - { - _functionCompletionTask.TrySetResult(true); - } + _functionCompletionTask.TrySetResult(true); } else { // we should never reach here b/c the class that calls this needs httpContextValueSource to complete to reach this method } } - - public void Dispose() - { - if (_tokenRegistration != default) - { - _tokenRegistration.Dispose(); - } - } } } diff --git a/extensions/Worker.Extensions.Http.AspNetCore/src/Coordinator/DefaultHttpCoordinator.cs b/extensions/Worker.Extensions.Http.AspNetCore/src/Coordinator/DefaultHttpCoordinator.cs index cbe542cdf..9f9a5d69b 100644 --- a/extensions/Worker.Extensions.Http.AspNetCore/src/Coordinator/DefaultHttpCoordinator.cs +++ b/extensions/Worker.Extensions.Http.AspNetCore/src/Coordinator/DefaultHttpCoordinator.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the MIT License. See License.txt in the project root for license information. using System; @@ -44,7 +44,6 @@ public async Task SetHttpContextAsync(string invocationId, Http public async Task SetFunctionContextAsync(string invocationId, FunctionContext context) { var contextRef = _contextReferenceList.GetOrAdd(invocationId, static id => new ContextReference(id)); - contextRef.SetCancellationToken(context.CancellationToken); contextRef.FunctionContextValueSource.SetResult(context); _logger.FunctionContextSet(invocationId); @@ -85,7 +84,6 @@ public void CompleteFunctionInvocation(string invocationId) if (_contextReferenceList.TryRemove(invocationId, out var contextRef)) { contextRef.CompleteFunction(); - contextRef.Dispose(); } else { diff --git a/extensions/Worker.Extensions.Http/release_notes.md b/extensions/Worker.Extensions.Http/release_notes.md index f1bef606f..e1fc96c72 100644 --- a/extensions/Worker.Extensions.Http/release_notes.md +++ b/extensions/Worker.Extensions.Http/release_notes.md @@ -7,3 +7,4 @@ ### Microsoft.Azure.Functions.Worker.Extensions.Http - The 'FromBody' converter now utilizes `DeserializeAsync` for deserializing JSON content from the request body, enhancing support for asynchronous deserialization. (#2901) +- Update `DefaultFromBodyConversionFeature` to no longer use a given invocation's cancellation token (#2894) diff --git a/extensions/Worker.Extensions.Http/src/DefaultFromBodyConversionFeature.cs b/extensions/Worker.Extensions.Http/src/DefaultFromBodyConversionFeature.cs index 767832406..fbe9c95ba 100644 --- a/extensions/Worker.Extensions.Http/src/DefaultFromBodyConversionFeature.cs +++ b/extensions/Worker.Extensions.Http/src/DefaultFromBodyConversionFeature.cs @@ -55,7 +55,7 @@ internal class DefaultFromBodyConversionFeature : IFromBodyConversionFeature _ when HasJsonContentType(requestData) => await (requestData.FunctionContext.InstanceServices.GetService>()?.Value?.Serializer ?? throw new InvalidOperationException("A serializer is not configured for the worker.")) - .DeserializeAsync(requestData.Body, targetType, context.CancellationToken), + .DeserializeAsync(requestData.Body, targetType, CancellationToken.None), _ => throw new InvalidOperationException($"The type '{targetType}' is not supported by the '{nameof(DefaultFromBodyConversionFeature)}'.") };