Skip to content

Stop retrying http requests when script invocation result is failed #11210

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

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from

Conversation

satvu
Copy link
Member

@satvu satvu commented Jul 28, 2025

Issue describing the changes in this PR

Breaking down #11159 and putting the HTTP changes in a separate PR. These changes make sure we stop retrying an http request if the correlated function invocation is failed by the host elsewhere (in cases of worker channel shutdown, host-tracked function timeout).

Pull request checklist

IMPORTANT: Currently, changes must be backported to the in-proc branch to be included in Core Tools and non-Flex deployments.

  • Backporting to the in-proc branch is not required
    • Otherwise: Link to backporting PR
  • My changes do not require documentation changes
    • Otherwise: Documentation issue linked to PR
  • My changes should not be added to the release notes for the next release
    • Otherwise: I've added my notes to release_notes.md
  • My changes do not need to be backported to a previous version
    • Otherwise: Backport tracked by issue/PR #issue_or_pr
  • My changes do not require diagnostic events changes
    • Otherwise: I have added/updated all related diagnostic events and their documentation (Documentation issue linked to PR)
  • I have added all required tests (Unit tests, E2E tests)

@Copilot Copilot AI review requested due to automatic review settings July 28, 2025 20:53
@satvu satvu requested a review from a team as a code owner July 28, 2025 20:53
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR stops retrying HTTP requests when the associated script invocation has failed elsewhere in the system (e.g., worker channel shutdown, host-tracked function timeout). It implements this by passing the script invocation context through the HTTP proxy pipeline and checking the invocation result status before attempting retries.

Key changes:

  • Adds a new ScriptInvocationRequestTransformer to pass invocation context through HTTP requests
  • Modifies RetryProxyHandler to check invocation result status before retrying
  • Updates DefaultHttpProxyService to use the new transformer and include invocation context

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/WebJobs.Script/ScriptConstants.cs Adds constant for HTTP proxy script invocation context
src/WebJobs.Script/Http/ScriptInvocationRequestTransformer.cs New transformer that passes script invocation context to HTTP request options
src/WebJobs.Script/Http/RetryProxyHandler.cs Enhanced to check invocation result status before retrying requests
src/WebJobs.Script/Http/DefaultHttpProxyService.cs Updated to use new transformer and include invocation context in HTTP context
test/WebJobs.Script.Tests/HttpProxyService/DefaultHttpProxyServiceTests.cs Moved to Http namespace
test/WebJobs.Script.Tests/HttpProxyService/ScriptInvocationRequestTransformerTests.cs New comprehensive test file for the request transformer

Comment on lines 46 to 48
if (resultSource is not null && resultSource.Task.IsFaulted)
{
throw resultSource.Task.Exception?.InnerException ?? new HttpRequestException("The function invocation tied to this HTTP request failed.");
Copy link
Preview

Copilot AI Jul 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition only checks for IsFaulted but doesn't handle IsCanceled state. If the invocation is canceled, retries should also stop. Consider checking resultSource.Task.IsCompleted && !resultSource.Task.IsCompletedSuccessfully to cover both faulted and canceled scenarios.

Suggested change
if (resultSource is not null && resultSource.Task.IsFaulted)
{
throw resultSource.Task.Exception?.InnerException ?? new HttpRequestException("The function invocation tied to this HTTP request failed.");
if (resultSource is not null && resultSource.Task.IsCompleted && !resultSource.Task.IsCompletedSuccessfully)
{
throw resultSource.Task.Exception?.InnerException ?? new HttpRequestException("The function invocation tied to this HTTP request failed or was canceled.");

Copilot uses AI. Check for mistakes.

Copy link
Member Author

@satvu satvu Jul 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cancellation will be propagated to the HttpRequest and handled later in this file. I think we should leave the cancellation flow as is, and not add extra handling based on the script invocation result TCS.

Edit: Here seems to be the only location this tcs is set to canceled, and this is a path that would lead to cancellation flowing to the worker and http request.

public override async ValueTask TransformRequestAsync(HttpContext httpContext, HttpRequestMessage proxyRequest, string destinationPrefix, CancellationToken cancellationToken)
{
// this preserves previous behavior (which called the default transformer) - base method is also called inside of here
await HttpTransformer.Default.TransformRequestAsync(httpContext, proxyRequest, destinationPrefix, cancellationToken);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel calling await base.TransformRequestAsync instead here provides more clarity. you can also remove the comment.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has to be HttpTransformer.Default.TransformRequestAsync - the base method does not add the X-Forwarded headers. I can remove the comment since tests should be able to catch if this is changed, but I wanted to make sure it was clear that choosing this method was intentional.

@satvu satvu requested review from kshyju, liliankasem and jviau July 29, 2025 19:09
if (resultSource is not null && resultSource.Task.IsFaulted)
{
throw resultSource.Task.Exception.InnerException ??
new Exception($"The function invocation tied to this HTTP request failed. Invocation ID: {scriptInvocationContext.ExecutionContext.InvocationId}");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still undecided on what exception to throw here - we don't want to throw something that will cause retries (see below lines), but what makes the most sense (some cancellation-related exception? do we change the check for HttpRequestException below so we don't retry in some cases?

@satvu satvu self-assigned this Jul 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants