Skip to content
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

SslStream BeginReader throw StackOverFlowException #29024

Closed
czd890 opened this issue Mar 20, 2019 · 2 comments
Closed

SslStream BeginReader throw StackOverFlowException #29024

czd890 opened this issue Mar 20, 2019 · 2 comments

Comments

@czd890
Copy link

czd890 commented Mar 20, 2019

BeginRead() throw stackoverflowexception

net core 2.2 console application

void start(){
//init socket code...
var socket = new Socket()
socket.Connect(...)
var tcpStream = new NetworkStream(socket, true);
var tcpSSLStream = new SslStream(tcpStream, false,...)
tcpSSLStream.AuthenticateAsClient
beginread()
}
void beginread(){
this.tcpSSLStream.BeginRead(buffer, 0, buffer.length, this.receive, null);
}
void receive(){
var readerLength = this.tcpSSLStream.EndRead(asyncResult);
this.beginread();
}

stack info:

SwissQuoteFeed.Engine.dll!SwissQuoteFeed.Engine.FeedReader.SwissQuoteTcpReader.Receive(System.IAsyncResult asyncResult) 行 105 C#
System.Net.Security.dll!System.Threading.Tasks.TaskToApm.Begin(System.Threading.Tasks.Task task, System.AsyncCallback callback, object state) 未知
System.Net.Security.dll!System.Net.Security.SslStream.BeginRead(byte[] buffer, int offset, int count, System.AsyncCallback asyncCallback, object asyncState) 未知
SwissQuoteFeed.Engine.dll!SwissQuoteFeed.Engine.FeedReader.SwissQuoteTcpReader.BeginRead() 行 101 C#
SwissQuoteFeed.Engine.dll!SwissQuoteFeed.Engine.FeedReader.SwissQuoteTcpReader.Receive(System.IAsyncResult asyncResult) 行 128 C#
System.Net.Security.dll!System.Threading.Tasks.TaskToApm.Begin(System.Threading.Tasks.Task task, System.AsyncCallback callback, object state) 未知
System.Net.Security.dll!System.Net.Security.SslStream.BeginRead(byte[] buffer, int offset, int count, System.AsyncCallback asyncCallback, object asyncState) 未知
SwissQuoteFeed.Engine.dll!SwissQuoteFeed.Engine.FeedReader.SwissQuoteTcpReader.BeginRead() 行 101 C#
SwissQuoteFeed.Engine.dll!SwissQuoteFeed.Engine.FeedReader.SwissQuoteTcpReader.Receive(System.IAsyncResult asyncResult) 行 128 C#
System.Net.Security.dll!System.Threading.Tasks.TaskToApm.Begin(System.Threading.Tasks.Task task, System.AsyncCallback callback, object state) 未知
System.Net.Security.dll!System.Net.Security.SslStream.BeginRead(byte[] buffer, int offset, int count, System.AsyncCallback asyncCallback, object asyncState) 未知
SwissQuoteFeed.Engine.dll!SwissQuoteFeed.Engine.FeedReader.SwissQuoteTcpReader.BeginRead() 行 101 C#
SwissQuoteFeed.Engine.dll!SwissQuoteFeed.Engine.FeedReader.SwissQuoteTcpReader.Receive(System.IAsyncResult asyncResult) 行 128 C#
System.Net.Security.dll!System.Threading.Tasks.TaskToApm.Begin(System.Threading.Tasks.Task task, System.AsyncCallback callback, object state) 未知
System.Net.Security.dll!System.Net.Security.SslStream.BeginRead(byte[] buffer, int offset, int count, System.AsyncCallback asyncCallback, object asyncState) 未知
SwissQuoteFeed.Engine.dll!SwissQuoteFeed.Engine.FeedReader.SwissQuoteTcpReader.BeginRead() 行 101 C#
SwissQuoteFeed.Engine.dll!SwissQuoteFeed.Engine.FeedReader.SwissQuoteTcpReader.Receive(System.IAsyncResult asyncResult) 行 128 C#
System.Net.Security.dll!System.Threading.Tasks.TaskToApm.Begin(System.Threading.Tasks.Task task, System.AsyncCallback callback, object state) 未知
System.Net.Security.dll!System.Net.Security.SslStream.BeginRead(byte[] buffer, int offset, int count, System.AsyncCallback asyncCallback, object asyncState) 未知
SwissQuoteFeed.Engine.dll!SwissQuoteFeed.Engine.FeedReader.SwissQuoteTcpReader.BeginRead() 行 101 C#
SwissQuoteFeed.Engine.dll!SwissQuoteFeed.Engine.FeedReader.SwissQuoteTcpReader.Receive(System.IAsyncResult asyncResult) 行 128 C#
System.Net.Security.dll!System.Threading.Tasks.TaskToApm.Begin(System.Threading.Tasks.Task task, System.AsyncCallback callback, object state) 未知
System.Net.Security.dll!System.Net.Security.SslStream.BeginRead(byte[] buffer, int offset, int count, System.AsyncCallback asyncCallback, object asyncState) 未知
SwissQuoteFeed.Engine.dll!SwissQuoteFeed.Engine.FeedReader.SwissQuoteTcpReader.BeginRead() 行 101 C#
SwissQuoteFeed.Engine.dll!SwissQuoteFeed.Engine.FeedReader.SwissQuoteTcpReader.Receive(System.IAsyncResult asyncResult) 行 128 C#
System.Net.Security.dll!System.Threading.Tasks.TaskToApm.Begin(System.Threading.Tasks.Task task, System.AsyncCallback callback, object state) 未知
System.Net.Security.dll!System.Net.Security.SslStream.BeginRead(byte[] buffer, int offset, int count, System.AsyncCallback asyncCallback, object asyncState) 未知
SwissQuoteFeed.Engine.dll!SwissQuoteFeed.Engine.FeedReader.SwissQuoteTcpReader.BeginRead() 行 101 C#
SwissQuoteFeed.Engine.dll!SwissQuoteFeed.Engine.FeedReader.SwissQuoteTcpReader.Receive(System.IAsyncResult asyncResult) 行 128 C#
System.Net.Security.dll!System.Threading.Tasks.TaskToApm.Begin(System.Threading.Tasks.Task task, System.AsyncCallback callback, object state) 未知
System.Net.Security.dll!System.Net.Security.SslStream.BeginRead(byte[] buffer, int offset, int count, System.AsyncCallback asyncCallback, object asyncState) 未知
SwissQuoteFeed.Engine.dll!SwissQuoteFeed.Engine.FeedReader.SwissQuoteTcpReader.BeginRead() 行 101 C#
SwissQuoteFeed.Engine.dll!SwissQuoteFeed.Engine.FeedReader.SwissQuoteTcpReader.Receive(System.IAsyncResult asyncResult) 行 128 C#
System.Net.Security.dll!System.Threading.Tasks.TaskToApm.InvokeCallbackWhenTaskCompletes.AnonymousMethod__0() 未知
System.Private.CoreLib.dll!System.Runtime.CompilerServices.TaskAwaiter.OutputWaitEtwEvents.AnonymousMethod__12_0(System.Action innerContinuation, System.Threading.Tasks.Task innerTask) 未知
System.Private.CoreLib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) 未知
System.Private.CoreLib.dll!System.Threading.Tasks.AwaitTaskContinuation.RunCallback(System.Threading.ContextCallback callback, object state, ref System.Threading.Tasks.Task currentTask) 未知
System.Private.CoreLib.dll!System.Threading.Tasks.Task.RunContinuations(object continuationObject) 未知
System.Private.CoreLib.dll!System.Threading.Tasks.Task.TrySetResult(int result) 未知
System.Private.CoreLib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder.SetExistingTaskResult(int result) 未知
System.Private.CoreLib.dll!System.Runtime.CompilerServices.AsyncValueTaskMethodBuilder.SetResult(int result) 未知
System.Net.Security.dll!System.Net.Security.SslStreamInternal.ReadAsyncInternal<System.Net.Security.SslStreamInternal.SslReadAsync>(System.Net.Security.SslStreamInternal.SslReadAsync adapter, System.Memory buffer) 未知
System.Private.CoreLib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) 未知
System.Private.CoreLib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder.AsyncStateMachineBox<System.Net.Security.SslStreamInternal.d__34<System.Net.Security.SslStreamInternal.SslReadAsync>>.MoveNext() 未知
System.Private.CoreLib.dll!System.Runtime.CompilerServices.TaskAwaiter.OutputWaitEtwEvents.AnonymousMethod__12_0(System.Action innerContinuation, System.Threading.Tasks.Task innerTask) 未知
System.Private.CoreLib.dll!System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Action action, bool allowInlining) 未知
System.Private.CoreLib.dll!System.Threading.Tasks.Task.RunContinuations(object continuationObject) 未知
System.Private.CoreLib.dll!System.Threading.Tasks.Task.TrySetResult(int result) 未知
System.Private.CoreLib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder.SetExistingTaskResult(int result) 未知
System.Private.CoreLib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder.SetResult(int result) 未知
System.Net.Security.dll!System.Net.Security.SslStreamInternal.FillBufferAsync.__InternalFillBufferAsync|38_0<System.Net.Security.SslStreamInternal.SslReadAsync>(System.Net.Security.SslStreamInternal.SslReadAsync adap, System.Threading.Tasks.ValueTask task, int min, int initial) 未知
System.Private.CoreLib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) 未知
System.Private.CoreLib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder.AsyncStateMachineBox<System.Net.Security.SslStreamInternal.<g__InternalFillBufferAsync|38_0>d<System.Net.Security.SslStreamInternal.SslReadAsync>>.MoveNext() 未知
System.Net.Sockets.dll!System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.InvokeContinuation(System.Action continuation, object state, bool forceAsync) 未知
System.Net.Sockets.dll!System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.OnCompleted(System.Net.Sockets.SocketAsyncEventArgs _) 未知
System.Net.Sockets.dll!System.Net.Sockets.SocketAsyncEventArgs.FinishOperationAsyncSuccess(int bytesTransferred, System.Net.Sockets.SocketFlags flags) 未知
System.Net.Sockets.dll!System.Net.Sockets.SocketAsyncEventArgs..cctor.AnonymousMethod__177_0(uint errorCode, uint numBytes, System.Threading.NativeOverlapped* nativeOverlapped) 未知
System.Private.CoreLib.dll!System.Threading._IOCompletionCallback.PerformIOCompletionCallback(uint errorCode, uint numBytes, System.Threading.NativeOverlapped* pOVERLAP) 未知

@stephentoub
Copy link
Member

stephentoub commented Mar 20, 2019

This is a bug in your code. The call to SslStream.BeginRead is completing synchronously, and is therefore invoking receive() synchronously. Your receive() then synchronously invokes beginread() , which in turn invokes SslStream.BeginRead synchronously, and so on.

The correct way to use the APM pattern is to check in the callback whether IAsyncResult.CompletedSynchronously is true. If it it's true, then the callback should exit immediately, and the call site should proceed to do the completion work; if it's false, then the callback should do the work. CompletedSynchronously exists to avoid stack dives like this.

@czd890
Copy link
Author

czd890 commented Mar 21, 2019

thx! i think BeginRead is new task or new thread exec. i'm wrong

@msftgits msftgits transferred this issue from dotnet/corefx Feb 1, 2020
@msftgits msftgits added this to the 3.0 milestone Feb 1, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants