-
Notifications
You must be signed in to change notification settings - Fork 188
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
Update ContextReference & DefaultFromBodyConversionFeature to remove invocation cancellation token usage #2894
base: main
Are you sure you want to change the base?
Conversation
sdk/Sdk.Generators/FunctionExecutor/FunctionExecutorGenerator.Parser.cs
Outdated
Show resolved
Hide resolved
...dk.Generators/FunctionMetadataProviderGenerator/FunctionMetadataProviderGenerator.Emitter.cs
Outdated
Show resolved
Hide resolved
...Sdk.Generators/FunctionMetadataProviderGenerator/FunctionMetadataProviderGenerator.Parser.cs
Outdated
Show resolved
Hide resolved
extensions/Worker.Extensions.Http/src/DefaultFromBodyConversionFeature.cs
Outdated
Show resolved
Hide resolved
extensions/Worker.Extensions.Http.AspNetCore/src/Coordinator/ContextReference.cs
Outdated
Show resolved
Hide resolved
extensions/Worker.Extensions.Http.AspNetCore/src/Coordinator/ContextReference.cs
Outdated
Show resolved
Hide resolved
627a808
to
70644de
Compare
68a5025
to
ac93ee7
Compare
extensions/Worker.Extensions.Http.AspNetCore/src/Coordinator/ContextReference.cs
Outdated
Show resolved
Hide resolved
extensions/Worker.Extensions.Http.AspNetCore/src/Coordinator/ContextReference.cs
Outdated
Show resolved
Hide resolved
extensions/Worker.Extensions.Http.AspNetCore/src/Coordinator/DefaultHttpCoordinator.cs
Outdated
Show resolved
Hide resolved
extensions/Worker.Extensions.Http/src/DefaultFromBodyConversionFeature.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approving. If you have any formal explanation for why removing that token registration in ContextReference
is a good change that would be helpful.
I know I suggested that change 😆 but it is still a significant behavior change, wondering what the impact of it really is.
I may have something in the design doc! I'll fill in any blanks and can update the PR desc with more info |
Issue describing the changes in this PR
resolves #2417
Pull request checklist
release_notes.md
Additional information
ContextReference
to remove and reference or use of the invocation cancellation tokenDefaultFromBodyConversionFeature
to remove and reference or use of the invocation cancellation tokenIssue
Currently, ContextReference registers the invocation’s cancellation token to multiple internal tasks and value sources. One of these, _functionCompletionTask, is responsible for disposing of the HttpContext. When the invocation is canceled, this results in the HttpContext being disposed prematurely, leading to an ObjectDisposedException when the function attempts to complete the HTTP response.
We have reason to believe that canceling _functionCompletionTask propagates cancellation through the ASP.NET Core pipeline, causing the HttpContext to be disposed earlier than expected. Since HttpContext is typically disposed of naturally when the request completes, this explicit disposal is unnecessary and introduces unwanted side effects.
Proposed Change
This PR removes the cancellation token registration from ContextReference, ensuring that:
By following the design principle that framework code should propagate cancellation but never react to it, this change ensures that handling cancellations remains the responsibility of customer code while preventing unintended HttpContext disposal.
Impact
With this change, function executions will continue running even if the client disconnects, which is the default behavior in ASP.NET Core. Users who want to cancel function execution explicitly can still do so by checking the CancellationToken within their function logic.
_functionStartTask and _functionCompletionTask
These tasks will no longer be canceled automatically when the cancellation token is triggered. This means that if a client disconnects or the invocation is canceled, these tasks will not be marked as canceled, and they will continue to track the function’s start and completion until the function completes. I believe this is behaviour we want.
_httpContextValueSource and _functionContextValueSource
Removing the cancellation token registration will prevent these value sources from being canceled automatically when the cancellation token is triggered. This means that HttpContext and FunctionContext will not be disposed of prematurely, allowing them to remain available until the function invocation completes. I believe this is behaviour we want.
This change is beneficial because it treats canceled invocations just like regular invocations, without any special handling. This gives customers the flexibility to manage cancellation in a way that best fits their needs.