Lost Http Connections in Azure FunctionApp (Flexible Consumption Plan)
- Dominant language
- PowerShell
- Stars
- 1.1k
- Forks
- 215
- Avg merge
- 4h 2m
- Merged PRs (30d)
- 1
Description
I am experiencing connection issues when making HTTP requests from an queue triggered Azure Function running on the Flexible Consumption Plan. Specifically, when I enqueue 30 messages into an Azure Storage Queue, a few of them do not receive a response. Instead, they fail with a TaskCanceledException.
This issue occurs when calling a third-party API, but I can reproduce it using https://httpbin.org/delay/3.
Function Code
```
public class TimeoutFunction
{
private readonly ILogger _logger;
private readonly HttpClient _httpClient;
public TimeoutFunction(ILogger logger, IHttpClientFactory httpClientFactory)
{
_logger = logger;
_httpClient = httpClientFactory.CreateClient("HttpClient");
_httpClient.Timeout = TimeSpan.FromSeconds(10);
_httpClient.DefaultRequestHeaders.ConnectionClose = false;
}
[Function("TimeoutFunction")]
public async Task Run([QueueTrigger("time-test-queue", Connection = "StorageQueue")] string url)
{
_logger.LogInformation("C# HTTP trigger function processed a request.");
try
{
var response = await _httpClient.GetAsync(url);
if (response.IsSuccessStatusCode)
{
_logger.LogInformation($"URL {url} is accessible. Status Code: {response.StatusCode}");
}
else
{
_logger.LogInformation($"URL {url} returned status: {response.StatusCode}");
}
}
catch (Exception ex)
{
_logger.LogError($"Error while checking URL: {ex.Message}");
}
}
}
```
Program.cs
```
using Microsoft.Azure.Functions.Worker.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Polly;
var builder = FunctionsApplication.CreateBuilder(args);
builder.ConfigureFunctionsWebApplication();
builder.Services.AddHttpClient("HttpCLient")
.SetHandlerLifetime(TimeSpan.FromMinutes(5))
.ConfigurePrimaryHttpMessageHandler(() =>
new HttpClientHandler
{
MaxConnectionsPerServer = 10
})
.AddTransientHttpErrorPolicy(policy =>
policy.WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))));
builder.Build().Run();
```
I have verified that:
The issue does not occur when running the function locally.
The issue does not occur when running the function on an App Service Plan.
Reproduction Steps
Deploy a Queue Triggered Azure Function using the Flexible Consumption Plan.
Push 30 messages to the queue.
Inside the function, make an HTTP request to https://httpbin.org/delay/3.
Additional Information
- Azure Function Runtime Version: v4
- Region: North Europe
- .NET Version: .Net 8
Any guidance or fixes would be greatly appreciated.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.