-
Notifications
You must be signed in to change notification settings - Fork 873
Description
Describe the bug
I have an ASP.NET MVC Core application that publishes to an SQS queue. This works fine in my local development and when I run the container locally using Docker. When I publish the container to ECR and then deploy the container to my AppRunner application, my code hangs at the point I try to send a message to the queue. When calling await _sqsClient.SendMessageAsync(message);
Expected Behavior
When calling the AmazonSQSClient.SendMessageAsync() method the message is sent or an exception is raised.
Current Behavior
When calling the AmazonSQSClient.SendMessageAsync() method the code hangs. No further event logs are received and no exception is raised until the application tries to shut down, at which point a TimeoutException is raised.
04-24-2023 10:33:07 AM Connection id "0HMQ49229A2N6", Request id "0HMQ49229A2N6:00000005": An unhandled exception was thrown by the application.
04-24-2023 10:33:07 AM fail: Microsoft.AspNetCore.Server.Kestrel[13]
04-24-2023 10:29:31 AM at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)
04-24-2023 10:29:31 AM at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
04-24-2023 10:29:31 AM at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
04-24-2023 10:29:31 AM at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
04-24-2023 10:29:31 AM at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
04-24-2023 10:29:31 AM at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
04-24-2023 10:29:31 AM at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
04-24-2023 10:29:31 AM at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
04-24-2023 10:29:31 AM at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
04-24-2023 10:29:31 AM at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
04-24-2023 10:29:31 AM at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
04-24-2023 10:29:31 AM at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
04-24-2023 10:29:31 AM at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
04-24-2023 10:29:31 AM at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
04-24-2023 10:29:31 AM at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskResultExecutor.Execute(ActionContext actionContext, IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
04-24-2023 10:29:31 AM at QueueTest.Controllers.QueueController.Queue() in /App/Controllers/QueueController.cs:line 40
04-24-2023 10:29:31 AM at Amazon.Runtime.Internal.MetricsHandler.InvokeAsync[T](IExecutionContext executionContext)
04-24-2023 10:29:31 AM at Amazon.Runtime.Internal.ErrorCallbackHandler.InvokeAsync[T](IExecutionContext executionContext)
04-24-2023 10:29:31 AM at Amazon.Runtime.Internal.CallbackHandler.InvokeAsync[T](IExecutionContext executionContext)
04-24-2023 10:29:31 AM at Amazon.Runtime.Internal.CallbackHandler.InvokeAsync[T](IExecutionContext executionContext)
04-24-2023 10:29:31 AM at Amazon.Runtime.Internal.RetryHandler.InvokeAsync[T](IExecutionContext executionContext)
04-24-2023 10:29:31 AM at Amazon.Runtime.Internal.RetryHandler.InvokeAsync[T](IExecutionContext executionContext)
04-24-2023 10:29:31 AM at Amazon.Runtime.Internal.CredentialsRetriever.InvokeAsync[T](IExecutionContext executionContext)
04-24-2023 10:29:31 AM at Amazon.Runtime.Internal.EndpointDiscoveryHandler.InvokeAsync[T](IExecutionContext executionContext)
04-24-2023 10:29:31 AM at Amazon.Runtime.Internal.EndpointDiscoveryHandler.InvokeAsync[T](IExecutionContext executionContext)
04-24-2023 10:29:31 AM at Amazon.Runtime.Internal.Signer.InvokeAsync[T](IExecutionContext executionContext)
04-24-2023 10:29:31 AM at Amazon.Runtime.Internal.CallbackHandler.InvokeAsync[T](IExecutionContext executionContext)
04-24-2023 10:29:31 AM at Amazon.Runtime.Internal.ErrorHandler.InvokeAsync[T](IExecutionContext executionContext)
04-24-2023 10:29:31 AM at Amazon.Runtime.Internal.ErrorHandler.InvokeAsync[T](IExecutionContext executionContext)
04-24-2023 10:29:31 AM at Amazon.SQS.Internal.ValidationResponseHandler.InvokeAsync[T](IExecutionContext executionContext)
04-24-2023 10:29:31 AM at Amazon.Runtime.Internal.Unmarshaller.InvokeAsync[T](IExecutionContext executionContext)
04-24-2023 10:29:31 AM at Amazon.Runtime.Internal.HttpHandler`1.InvokeAsync[T](IExecutionContext executionContext)
04-24-2023 10:29:31 AM at Amazon.Runtime.HttpWebRequestMessage.GetResponseAsync(CancellationToken cancellationToken)
04-24-2023 10:29:31 AM --- End of inner exception stack trace ---
04-24-2023 10:29:31 AM at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)
04-24-2023 10:29:31 AM at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
04-24-2023 10:29:31 AM at System.Net.Http.DiagnosticsHandler.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
04-24-2023 10:29:31 AM at System.Net.Http.HttpConnectionPool.SendWithVersionDetectionAndRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken)
04-24-2023 10:29:31 AM at System.Net.Http.HttpConnectionPool.HttpConnectionWaiter`1.WaitForConnectionAsync(Boolean async, CancellationToken requestCancellationToken)
04-24-2023 10:29:31 AM at System.Threading.Tasks.TaskCompletionSourceWithCancellation`1.WaitWithCancellationAsync(CancellationToken cancellationToken)
04-24-2023 10:29:31 AM ---> System.Threading.Tasks.TaskCanceledException: A task was canceled.
04-24-2023 10:29:31 AM System.TimeoutException: A task was canceled.
04-24-2023 10:29:31 AM Connection id "0HMQ49229A2MH", Request id "0HMQ49229A2MH:00000001": An unhandled exception was thrown by the application.
04-24-2023 10:29:31 AM fail: Microsoft.AspNetCore.Server.Kestrel[13]
04-24-2023 10:25:15 AM Application is shutting down...
04-24-2023 10:25:15 AM �[40m�[32minfo�[39m�[22m�[49m: Microsoft.Hosting.Lifetime[0]
04-24-2023 10:24:13 AM Sending message to SQS
04-24-2023 10:24:13 AM info: QueueTest.Controllers.QueueController[0]
04-24-2023 10:24:13 AM Preparing to send message to SQS
Reproduction Steps
Create a new .NET 6 API MVC project with Docker support:
Add the AWSSDK.SQS package, version 3.7.100.117
Add the following Controller example:
using System;
using System.Net;
using System.Text.Json;
using System.Threading.Tasks;
using Amazon;
using Amazon.Runtime;
using Amazon.SQS;
using Amazon.SQS.Model;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace QueueTest.Controllers
{
[ApiController]
[Route("[controller]")]
public class QueueController : ControllerBase
{
private readonly ILogger<QueueController> _logger;
public QueueController(ILogger<QueueController> logger)
{
_logger = logger;
}
[HttpGet]
public async Task Queue()
{
var client = new AmazonSQSClient(new BasicAWSCredentials("[access-key]", "[secret-key]"), RegionEndpoint.USEast1);
_logger.LogInformation("Preparing to send message to SQS");
var body = JsonSerializer.Serialize(new
{
FirstName = "John",
LastName = "Doe"
});
var request = new SendMessageRequest
{
MessageBody = body,
QueueUrl = "https://sqs.us-east-1.amazonaws.com/[queue-url]"
};
_logger.LogInformation("Sending message to SQS");
var result = await client.SendMessageAsync(request);
if (result.HttpStatusCode != HttpStatusCode.OK)
{
throw new Exception($"Failed to publish payload to SQS. Error code {result.HttpStatusCode}");
}
_logger.LogInformation("Message sent to SQS");
}
}
}
Example dockerfile:
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
WORKDIR /App
# Copy everything
COPY . ./
# Restore as distinct layers
RUN dotnet restore
# Build and publish a release
RUN dotnet publish -c Release -o out /p:UseAppHost=false
# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:6.0
ENV ASPNETCORE_URLS="http://+:5000"
EXPOSE 5000
WORKDIR /App
COPY --from=build-env /App/out .
ENTRYPOINT ["dotnet", "[ProjectName].dll"]
Publish to an ECR repo and then create an AppRunner application deploying from the ECR repo.
Possible Solution
No response
Additional Information/Context
No response
AWS .NET SDK and/or Package version used
AWSSDK.SQS 3.7.100.117
Targeted .NET Platform
.NET 6 (Also tried .NET 7 and 3.1 with the same issue)
Operating System and version
Container deployed through AppRunner