aws-sdk-net: Response unmarshalling code can potentially block forever
Description
We’ve had a repeated problem in production with a component that processes messages from Amazon SQS whereby it will stall (fail to process messages on the queue). The process never recovers and needs to be recycled manually.
From our application’s perspective we make a call to IAmazonSQS::ReceiveMessageAsync(request, ct) which never completes (we’re using a long-polling interval of 10 seconds).
We’ve examined a memory dump for a process in this failed state. We can see that it has made a HTTP call to AWS (https://sqs.us-east-1.amazonaws.com/xxx) and that the headers have come back but for whatever reason the content for the response has not. We don’t know the underlying cause behind this yet, current working theories are either
- A bug in the runtime (see https://github.com/dotnet/runtime/issues/53921)
- The AWS server simply not sending anything
Regardless of the underlying root cause, the AWS SDK should be made more resilient for this situation.
At time of writing, when making requests the AWS SDK awaits the call HttpClient::SendAsync, however due to the use of HttpCompletionOption.ResponseHeadersRead that task completes as soon as headers become available, without regard to the content (stream) - see https://github.com/aws/aws-sdk-net/blob/475822dec5e87954b7a47ac65995714ae1f1b115/sdk/src/Core/Amazon.Runtime/Pipeline/HttpHandler/_netstandard/HttpRequestMessageFactory.cs#L520
The content stream then gets indirectly passed in to the unmarshalling code (XmlResponseUnmarshaller, ReceiveMessageResponseUnmarshaller and friends).
Even though we’ve come through an async entrypoint, the unmarshalling code (indirectly) does a blocking (synchronous) read on the stream. If data is never added to the stream, it will block forever (which is exactly what we’ve seen).
Reproduction Steps
Consumer task (00007f8e081f4720) i.e. the one reading from SQS is tied up by AWS SDK Unmarshaller (comes directly after HTTP handler in the AWS pipeline).
The unmarshaller task is currently executing.
> dumpasync -tasks -addr 00007F8E08A2B3D0 -stacks
Address MT Size State Description
00007f8e08a2b3d0 00007f8e800e73a8 152 -1 Amazon.Runtime.Internal.Unmarshaller+<InvokeAsync>d__3`1[[System.__Canon, System.Private.CoreLib]]
Continuation chains:
.00007f8e08a2b468 (0) Amazon.SQS.Internal.ValidationResponseHandler+<InvokeAsync>d__1`1[[System.__Canon, System.Private.CoreLib]]
..00007f8e08a2b4f0 (0) Amazon.Runtime.Internal.ErrorHandler+<InvokeAsync>d__5`1[[System.__Canon, System.Private.CoreLib]]
...00007f8e08a2b578 (0) Amazon.Runtime.Internal.CallbackHandler+<InvokeAsync>d__9`1[[System.__Canon, System.Private.CoreLib]]
....00007f8e08a2b600 (0) Amazon.Runtime.Internal.EndpointDiscoveryHandler+<InvokeAsync>d__2`1[[System.__Canon, System.Private.CoreLib]]
.....00007f8e08a2b698 (1) Amazon.Runtime.Internal.CredentialsRetriever+<InvokeAsync>d__7`1[[System.__Canon, System.Private.CoreLib]]
......00007f8e08a2b738 (0) Amazon.Runtime.Internal.RetryHandler+<InvokeAsync>d__10`1[[System.__Canon, System.Private.CoreLib]]
.......00007f8e08a2b7f8 (0) Amazon.Runtime.Internal.CallbackHandler+<InvokeAsync>d__9`1[[System.__Canon, System.Private.CoreLib]]
........00007f8e08a2b880 (0) Amazon.Runtime.Internal.CallbackHandler+<InvokeAsync>d__9`1[[System.__Canon, System.Private.CoreLib]]
.........00007f8e08a2b908 (0) Amazon.Runtime.Internal.ErrorCallbackHandler+<InvokeAsync>d__5`1[[System.__Canon, System.Private.CoreLib]]
..........00007f8e08a2b990 (0) Amazon.Runtime.Internal.MetricsHandler+<InvokeAsync>d__1`1[[System.__Canon, System.Private.CoreLib]]
...........00007f8e081f4720 (0) Acme.Organisation.Processor.Queue.QueueService+<Write>d__6
............00007f8e081f4808 (0) Acme.Organisation.Processor.Queue.QueueService+<>c__DisplayClass5_1+<<ExecuteAsync>b__1>d
.............00007f8e081e2960 System.Threading.Tasks.UnwrapPromise`1[[System.Threading.Tasks.VoidTaskResult, System.Private.CoreLib]]
..............00007f8e081f2618 System.Threading.Tasks.Task+WhenAllPromise
...............00007f8e081f2668 (0) Acme.Organisation.QueueService+<ExecuteAsync>d__5
--------------------------------------------------------------------------------
See callstack for thread 11. It's using the stream returned by the http handler.
> clrstack
OS Thread Id: 0x820 (11)
Child SP IP Call Site
00007F8DD7FFB810 00007f8ef802d00c [GCFrame: 00007f8dd7ffb810]
00007F8DD7FFB8F0 00007f8ef802d00c [HelperMethodFrame_1OBJ: 00007f8dd7ffb8f0] System.Threading.Monitor.ObjWait(Boolean, Int32, System.Object)
00007F8DD7FFBA20 00007F8E811A3A25 System.Threading.ManualResetEventSlim.Wait(Int32, System.Threading.CancellationToken) [/_/src/System.Private.CoreLib/shared/System/Threading/ManualResetEventSlim.cs @ 626]
00007F8DD7FFBAB0 00007F8E8124865F System.Net.Sockets.SocketAsyncContext.PerformSyncOperation[[System.__Canon, System.Private.CoreLib]](OperationQueue`1<System.__Canon> ByRef, System.__Canon, Int32, Int32) [/_/src/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncContext.Unix.cs @ 1273]
00007F8DD7FFBB20 00007F8E81219D3C System.Net.Sockets.SocketAsyncContext.ReceiveFrom(System.Memory`1<Byte>, System.Net.Sockets.SocketFlags ByRef, Byte[], Int32 ByRef, Int32, Int32 ByRef) [/_/src/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncContext.Unix.cs @ 1465]
00007F8DD7FFBBC0 00007F8E81219A9D System.Net.Sockets.SocketPal.Receive(System.Net.Sockets.SafeSocketHandle, Byte[], Int32, Int32, System.Net.Sockets.SocketFlags, Int32 ByRef) [/_/src/System.Net.Sockets/src/System/Net/Sockets/SocketPal.Unix.cs @ 983]
00007F8DD7FFBC60 00007F8E81219726 System.Net.Sockets.Socket.Receive(Byte[], Int32, Int32, System.Net.Sockets.SocketFlags, System.Net.Sockets.SocketError ByRef) [/_/src/System.Net.Sockets/src/System/Net/Sockets/Socket.cs @ 1386]
00007F8DD7FFBCB0 00007F8E81219340 System.Net.Sockets.NetworkStream.Read(Byte[], Int32, Int32) [/_/src/System.Net.Sockets/src/System/Net/Sockets/NetworkStream.cs @ 266]
00007F8DD7FFBD00 00007F8E8116E1B4 System.Net.Security.SslStream.FillBufferAsync[[System.Net.Security.SslStream+SslReadSync, System.Net.Security]](SslReadSync, Int32) [/_/src/System.Net.Security/src/System/Net/Security/SslStream.Implementation.cs @ 1444]
00007F8DD7FFBDA0 00007F8E8116EE93 System.Net.Security.SslStream+<ReadAsyncInternal>d__214`1[[System.Net.Security.SslStream+SslReadSync, System.Net.Security]].MoveNext() [/_/src/System.Net.Security/src/System/Net/Security/SslStream.Implementation.cs @ 1439]
00007F8DD7FFBF10 00007F8E8116E904 System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[[System.Net.Security.SslStream+<ReadAsyncInternal>d__214`1[[System.Net.Security.SslStream+SslReadSync, System.Net.Security]], System.Net.Security]](<ReadAsyncInternal>d__214`1<SslReadSync> ByRef) [/_/src/System.Private.CoreLib/shared/System/Runtime/CompilerServices/AsyncMethodBuilder.cs @ 1036]
00007F8DD7FFBF50 00007F8E8116E82C System.Net.Security.SslStream.ReadAsyncInternal[[System.Net.Security.SslStream+SslReadSync, System.Net.Security]](SslReadSync, System.Memory`1<Byte>)
00007F8DD7FFC000 00007F8E8116E592 System.Net.Security.SslStream.Read(Byte[], Int32, Int32) [/_/src/System.Net.Security/src/System/Net/Security/SslStream.cs @ 753]
00007F8DD7FFC080 00007F8E8116E394 System.IO.Stream.Read(System.Span`1<Byte>) [/_/src/System.Private.CoreLib/shared/System/IO/Stream.cs @ 756]
00007F8DD7FFC0C0 00007F8E8116E056 System.Net.Http.HttpConnection.Read(System.Span`1<Byte>) [/_/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnection.cs @ 1481]
00007F8DD7FFC130 00007F8E811350CE System.Net.Http.HttpConnection+ContentLengthReadStream.Read(System.Span`1<Byte>) [/_/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ContentLengthReadStream.cs @ 26]
00007F8DD7FFC170 00007F8E81134F1F System.Net.Http.HttpBaseStream.Read(Byte[], Int32, Int32) [/_/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpBaseStream.cs @ 91]
00007F8DD7FFC1B0 00007F8E81163F12 System.IO.StreamReader.ReadBuffer(System.Span`1<Char>, Boolean ByRef) [/_/src/System.Private.CoreLib/shared/System/IO/StreamReader.cs @ 658]
00007F8DD7FFC210 00007F8E8117F6DE System.IO.StreamReader.ReadSpan(System.Span`1<Char>) [/_/src/System.Private.CoreLib/shared/System/IO/StreamReader.cs @ 360]
00007F8DD7FFC270 00007F8E8117F4EC System.IO.StreamReader.Read(Char[], Int32, Int32) [/_/src/System.Private.CoreLib/shared/System/IO/StreamReader.cs @ 338]
00007F8DD7FFC2A0 00007F8E8117F400 System.Xml.XmlTextReaderImpl.ReadData() [/_/src/System.Private.Xml/src/System/Xml/Core/XmlTextReaderImpl.cs @ 3358]
00007F8DD7FFC2D0 00007F8E811AD151 System.Xml.XmlTextReaderImpl.InitTextReaderInput(System.String, System.Uri, System.IO.TextReader) [/_/src/System.Private.Xml/src/System/Xml/Core/XmlTextReaderImpl.cs @ 2940]
00007F8DD7FFC300 00007F8E811AD011 System.Xml.XmlTextReaderImpl.FinishInitTextReader() [/_/src/System.Private.Xml/src/System/Xml/Core/XmlTextReaderImpl.cs @ 784]
00007F8DD7FFC320 00007F8E811AC5BF System.Xml.XmlTextReaderImpl..ctor(System.IO.TextReader, System.Xml.XmlReaderSettings, System.String, System.Xml.XmlParserContext) [/_/src/System.Private.Xml/src/System/Xml/Core/XmlTextReaderImpl.cs @ 768]
00007F8DD7FFC360 00007F8E811AC452 System.Xml.XmlReaderSettings.CreateReader(System.IO.TextReader, System.String, System.Xml.XmlParserContext) [/_/src/System.Private.Xml/src/System/Xml/Core/XmlReaderSettings.cs @ 487]
00007F8DD7FFC3A0 00007F8E7FE11302 Amazon.Runtime.Internal.Transform.XmlUnmarshallerContext.Read()
00007F8DD7FFC400 00007F8E7FE1051A Amazon.SQS.Model.Internal.MarshallTransformations.ReceiveMessageResponseUnmarshaller.Unmarshall(Amazon.Runtime.Internal.Transform.XmlUnmarshallerContext)
00007F8DD7FFC440 00007F8E81194CFA Amazon.Runtime.Internal.Transform.XmlResponseUnmarshaller.Unmarshall(Amazon.Runtime.Internal.Transform.UnmarshallerContext)
00007F8DD7FFC470 00007F8E81182889 Amazon.Runtime.Internal.Transform.ResponseUnmarshaller.UnmarshallResponse(Amazon.Runtime.Internal.Transform.UnmarshallerContext)
00007F8DD7FFC490 00007F8E8118251F Amazon.Runtime.Internal.Unmarshaller.UnmarshallResponse(Amazon.Runtime.Internal.Transform.UnmarshallerContext, Amazon.Runtime.IRequestContext)
00007F8DD7FFC4F0 00007F8E81181E93 Amazon.Runtime.Internal.Unmarshaller+<UnmarshallAsync>d__5.MoveNext()
00007F8DD7FFC590 00007F8E81181A24 System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[[Amazon.Runtime.Internal.Unmarshaller+<UnmarshallAsync>d__5, AWSSDK.Core]](<UnmarshallAsync>d__5 ByRef) [/_/src/System.Private.CoreLib/shared/System/Runtime/CompilerServices/AsyncMethodBuilder.cs @ 1036]
00007F8DD7FFC5D0 00007F8E8118197D Amazon.Runtime.Internal.Unmarshaller.UnmarshallAsync(Amazon.Runtime.IExecutionContext)
00007F8DD7FFC640 00007F8E811521A7 Amazon.Runtime.Internal.Unmarshaller+<InvokeAsync>d__3`1[[System.__Canon, System.Private.CoreLib]].MoveNext()
00007F8DD7FFC6E0 00007F8E7FE1963D System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object) [/_/src/System.Private.CoreLib/shared/System/Threading/ExecutionContext.cs @ 201]
00007F8DD7FFC730 00007F8E811817D5 System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1[[System.__Canon, System.Private.CoreLib],[Amazon.Runtime.Internal.Unmarshaller+<InvokeAsync>d__3`1[[System.__Canon, System.Private.CoreLib]], AWSSDK.Core]].MoveNext(System.Threading.Thread) [/_/src/System.Private.CoreLib/shared/System/Runtime/CompilerServices/AsyncMethodBuilder.cs @ 654]
00007F8DD7FFC770 00007F8E7FE19EE6 System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Runtime.CompilerServices.IAsyncStateMachineBox, Boolean) [/_/src/System.Private.CoreLib/shared/System/Threading/Tasks/TaskContinuation.cs @ 804]
Meanwhile, callstack for "event loop"
> clrstack
OS Thread Id: 0x21 (10)
Child SP IP Call Site
00007F8E07FFC740 00007f8ef7c317ef [InlinedCallFrame: 00007f8e07ffc740] Interop+Sys.WaitForSocketEvents(IntPtr, SocketEvent*, Int32*)
00007F8E07FFC740 00007f8e7f7dc06b [InlinedCallFrame: 00007f8e07ffc740] Interop+Sys.WaitForSocketEvents(IntPtr, SocketEvent*, Int32*)
00007F8E07FFC730 00007F8E7F7DC06B ILStubClass.IL_STUB_PInvoke(IntPtr, SocketEvent*, Int32*)
00007F8E07FFC7C0 00007F8E7FB0D25C System.Net.Sockets.SocketAsyncEngine.EventLoop() [/_/src/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEngine.Unix.cs @ 311]
00007F8E07FFC810 00007F8E7FB0D219 System.Net.Sockets.SocketAsyncEngine+<>c.<.ctor>b__23_0(System.Object) [/_/src/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEngine.Unix.cs @ 285]
00007F8E07FFC820 00007F8E7D874883 System.Threading.Tasks.Task.InnerInvoke() [/_/src/System.Private.CoreLib/shared/System/Threading/Tasks/Task.cs @ 2445]
00007F8E07FFC840 00007F8E7D874691 System.Threading.Tasks.Task.ExecuteWithThreadLocal(System.Threading.Tasks.Task ByRef, System.Threading.Thread) [/_/src/System.Private.CoreLib/shared/System/Threading/Tasks/Task.cs @ 2378]
00007F8E07FFC8C0 00007F8E7D8744E8 System.Threading.Tasks.Task.ExecuteEntryUnsafe(System.Threading.Thread) [/_/src/System.Private.CoreLib/shared/System/Threading/Tasks/Task.cs @ 2321]
00007F8E07FFC8E0 00007F8E7D88146B System.Threading.Tasks.ThreadPoolTaskScheduler+<>c.<.cctor>b__10_0(System.Object) [/_/src/System.Private.CoreLib/shared/System/Threading/Tasks/ThreadPoolTaskScheduler.cs @ 36]
00007F8E07FFC8F0 00007F8E7D85544A System.Threading.ThreadHelper.ThreadStart(System.Object) [/_/src/System.Private.CoreLib/src/System/Threading/Thread.CoreCLR.cs @ 81]
00007F8E07FFCC60 00007f8ef7142d5f [GCFrame: 00007f8e07ffcc60]
00007F8E07FFCD30 00007f8ef7142d5f [DebuggerU2MCatchHandlerFrame: 00007f8e07ffcd30]
ManualResetEventSlim shared:
> gcroot 00007f8e08a30058
Thread 21:
00007F8E07FFC7C0 00007F8E7FB0D25C System.Net.Sockets.SocketAsyncEngine.EventLoop() [/_/src/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEngine.Unix.cs @ 311]
rbx:
-> 00007F8E081E02E8 System.Net.Sockets.SocketAsyncEngine
-> 00007F8E081E0328 System.Collections.Concurrent.ConcurrentDictionary`2[[System.IntPtr, System.Private.CoreLib],[System.Net.Sockets.SocketAsyncContext, System.Net.Sockets]]
-> 00007F8E08382040 System.Collections.Concurrent.ConcurrentDictionary`2+Tables[[System.IntPtr, System.Private.CoreLib],[System.Net.Sockets.SocketAsyncContext, System.Net.Sockets]]
-> 00007F8E08381728 System.Collections.Concurrent.ConcurrentDictionary`2+Node[[System.IntPtr, System.Private.CoreLib],[System.Net.Sockets.SocketAsyncContext, System.Net.Sockets]][]
-> 00007F8E089F0D20 System.Collections.Concurrent.ConcurrentDictionary`2+Node[[System.IntPtr, System.Private.CoreLib],[System.Net.Sockets.SocketAsyncContext, System.Net.Sockets]]
-> 00007F8E089F0C48 System.Net.Sockets.SocketAsyncContext
-> 00007F8E08A2FFE8 System.Net.Sockets.SocketAsyncContext+BufferMemoryReceiveOperation
-> 00007F8E08A30058 System.Threading.ManualResetEventSlim
Thread 820:
00007F8DD7FFBA20 00007F8E811A3A25 System.Threading.ManualResetEventSlim.Wait(Int32, System.Threading.CancellationToken) [/_/src/System.Private.CoreLib/shared/System/Threading/ManualResetEventSlim.cs @ 626]
rbp-70: 00007f8dd7ffba30
-> 00007F8E08A30058 System.Threading.ManualResetEventSlim
00007F8DD7FFBAB0 00007F8E8124865F System.Net.Sockets.SocketAsyncContext.PerformSyncOperation[[System.__Canon, System.Private.CoreLib]](OperationQueue`1<System.__Canon> ByRef, System.__Canon, Int32, Int32) [/_/src/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncContext.Unix.cs @ 1273]
rbp-50: 00007f8dd7ffbac0
-> 00007F8E08A30058 System.Threading.ManualResetEventSlim
> dumpheap -type BufferMemoryReceiveOperation
Address MT Size
00007f8e08a2ffe8 00007f8e7fcc2a48 112 => context: 00007f8e089f0c48 (SocketAsyncContext)
> do 00007f8e08a2ffe8
Name: System.Net.Sockets.SocketAsyncContext+BufferMemoryReceiveOperation
MethodTable: 00007f8e7fcc2a48
EEClass: 00007f8e7fcb59e8
Size: 112(0x70) bytes
File: /usr/share/dotnet/shared/Microsoft.NETCore.App/3.1.1/System.Net.Sockets.dll
Fields:
MT Field Offset Type VT Attr Value Name
00007f8e7ddba0e8 4000298 28 System.Int32 1 instance 0 _state
00007f8e7fb8a6c8 4000299 8 ...ocketAsyncContext 0 instance 00007f8e089f0c48 AssociatedContext
00007f8e7fb8a000 400029a 10 ...xt+AsyncOperation 0 instance 00007f8e08a2ffe8 Next
00007f8e7d33c798 400029b 18 System.Object 0 instance 00007f8e08a30058 CallbackOrEvent
00007f8e7faacf48 400029c 2c System.Int32 1 instance 0 ErrorCode
00007f8e7ddc14c0 400029d 20 System.Byte[] 0 instance 0000000000000000 SocketAddress
00007f8e7ddba0e8 400029e 30 System.Int32 1 instance 0 SocketAddressLen
00007f8e7e41b5c8 400029f 38 ...TokenRegistration 1 instance 00007f8e08a30020 CancellationRegistration
00007f8e7faad098 40002af 48 System.Int32 1 instance 0 Flags
00007f8e7faad098 40002b0 4c System.Int32 1 instance 0 ReceivedFlags
00007f8e7ddba0e8 40002b1 50 System.Int32 1 instance 0 BytesTransferred
00007f8e7e66acb8 40002b2 58 ...Private.CoreLib]] 1 instance 00007f8e08a30040 Buffer
Environment
- SDK Version: 3.5.1.19 (AWSSDK.SQS)
- OS Info: Ubuntu 18 (Docker Container) / Linux db915f8a8c2c 5.4.72-microsoft-standard-WSL2 #1 SMP Wed Oct 28 23:40:43 UTC 2020 x86_64 GNU/Linux
- Build Environment dotnet CLI
- Targeted .NET Platform: netcoreapp3.1
Resolution
Presumably the use of HttpCompletionOption.ResponseHeadersRead is deliberate. Assuming that’s the case, I’d suggest one of the following solutions:
- While still in the
asynccode path, read the entire response stream into a buffer (memory stream) w/Stream.CopyToAsyncw/ aCancellationTokenSourcebased on a suitable timeout. From that point, code can be safely synchronous - Find a way to set
ReadTimeouton theNetworkStream(not sure if this will be possible)
If you’d be happy with either of these solutions, let me know and I’ll work on a PR 👋
This is a 🐛 bug-report
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 23 (10 by maintainers)
Hi @ppittle,
We’re already using the same technique that you proposed (racing the task against a delay task) as workaround. However a serious limitation (with a long running process) is that it won’t actually cancel the request, leaving the HTTP connection open.
Anyway, thanks for taking the time to look into this and glad to hear it’s being fixed holistically (which was what I was originally asking for anyway).
Cheers, Eddie
That’s fair. it can be an opt-in only.
@flakey-bit Thanks for sharing the information. I will discuss with development team to get this triaged.