none
HttpWebRequest Thread Block RRS feed

  • Question

  • Envionment:

    OS: Win7(x86), WinServer2008(x64)

    .net framework: 4.0

    language: C# and C++ mixcode

    ----------------------------------------------------

    Description:

    【Thread Block】

    I use

    【HttpWebRequest.BeginGetRequestStream】,【HttpWebRequest.BeginGetResponse】 and【Steam.BeginRead】 which are asynchronous operation to access Internet Resource(in order to low the cpu useage),although, i use a 【ManualResetEventSlim】 to keep this operation synchronization. But Some Times this thread is hang/block.

    And I use DebugDiag to dump the process, and find this thread is unnormal:

    Thread 40 - System ID 1056

    Entry point 0x00000000
    Create time 2013-3-25 11:43:38
    Time spent in user mode 0 Days 00:07:08.437
    Time spent in kernel mode 0 Days 00:00:46.421


    This thread is waiting on data to be returned from another server via WinSock.

    The call to WinSock originated from DomainBoundILStubClass.IL_STUB_PInvoke(IntPtr, Byte*, Int32, System.Net.Sockets.SocketFlags) and is destined forport 80 at IP address xx.xx.xx.xx


    .NET Call Stack
    Function
    DomainBoundILStubClass.IL_STUB_PInvoke(IntPtr, Byte*, Int32, System.Net.Sockets.SocketFlags)
    System.Net.UnsafeNclNativeMethods+OSSOCK.recv(IntPtr, Byte*, Int32, System.Net.Sockets.SocketFlags)
    System.Net.Sockets.Socket.Receive(Byte[], Int32, Int32, System.Net.Sockets.SocketFlags, System.Net.Sockets.SocketError ByRef)
    System.Net.Sockets.Socket.Receive(Byte[], Int32, Int32, System.Net.Sockets.SocketFlags)
    System.Net.Sockets.NetworkStream.Read(Byte[], Int32, Int32)
    System.Net.PooledStream.Read(Byte[], Int32, Int32)
    System.Net.ConnectStream.InternalRead(Byte[], Int32, Int32)
    System.Net.ConnectStream.ReadWithoutValidation(Byte[], Int32, Int32, Boolean)
    System.Net.ConnectStream.DrainSocket()
    System.Net.ConnectStream.CloseInternal(Boolean, Boolean)
    System.Net.ConnectStream.System.Net.ICloseEx.CloseEx(System.Net.CloseExState)
    System.Net.ConnectStream.Dispose(Boolean)
    System.IO.Stream.Close()
    MyApplication.AsyncRequestContext.Dispose()
    MyApplication.PostDataToUrlAsync[[System.__Canon, mscorlib]](Byte[], System.String, Int32, System.__Canon ByRef)
    MyApplication.PostReponse[[System.__Canon, mscorlib]](System.String, Smc20.Device.Com.HWTerminalPublic.PostAPIMethodName, Int32, System.__Canon ByRef)
    MyApplication.LoginOut()
    MyApplication.CloseConnection(Smc20.Device.Com.Public.DeviceStateCloseConnectRequest)
    MyApplication.DeviceStateCloseConnectRequest.SendMessage(Smc20.Device.Com.Public.IDeviceMessageSender)
    MyApplication.SyncSendThread.Run()
    System.Threading.ThreadHelper.ThreadStart_Context(System.Object)
    System.Threading.ExecutionContext.runTryCode(System.Object)
    System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode, CleanupCode, System.Object)
    System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
    System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
    System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
    System.Threading.ThreadHelper.ThreadStart()

    Full Call Stack
    Function Source
    ntdll!ZwWaitForSingleObject+15
    mswsock!SockWaitForSingleObject+1ba
    mswsock!WSPRecv+2a7
    ws2_32!recv+87
    DomainBoundILStubClass.IL_STUB_PInvoke(IntPtr, Byte*, Int32, System.Net.Sockets.SocketFlags)
    System.Net.Sockets.Socket.Receive(Byte[], Int32, Int32, System.Net.Sockets.SocketFlags, System.Net.Sockets.SocketError ByRef)
    System.Net.Sockets.Socket.Receive(Byte[], Int32, Int32, System.Net.Sockets.SocketFlags)
    System.Net.Sockets.NetworkStream.Read(Byte[], Int32, Int32)
    System.Net.PooledStream.Read(Byte[], Int32, Int32)
    System.Net.ConnectStream.InternalRead(Byte[], Int32, Int32)
    System.Net.ConnectStream.ReadWithoutValidation(Byte[], Int32, Int32, Boolean)
    System.Net.ConnectStream.DrainSocket()
    System.Net.ConnectStream.CloseInternal(Boolean, Boolean)
    System.Net.ConnectStream.System.Net.ICloseEx.CloseEx(System.Net.CloseExState)
    System.Net.ConnectStream.Dispose(Boolean)
    System.IO.Stream.Close()
    MyApplication.AsyncRequestContext.Dispose()
    MyApplication.PostDataToUrlAsync[[System.__Canon, mscorlib]](Byte[], System.String, Int32, System.__Canon ByRef)
    System.Threading.ThreadHelper.ThreadStart_Context(System.Object)
    System.Threading.ExecutionContext.runTryCode(System.Object)
    clr!CallDescrWorker+33
    clr!CallDescrWorkerWithHandler+8e
    clr!MethodDesc::CallDescr+194
    clr!MethodDesc::CallTargetWorker+21
    clr!MethodDescCallSite::CallWithValueTypes+1c
    clr!ExecuteCodeWithGuaranteedCleanupHelper+bb
    clr!ReflectionInvocation::ExecuteCodeWithGuaranteedCleanup+138
    System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
    System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
    System.Threading.ThreadHelper.ThreadStart()
    clr!CallDescrWorker+33
    clr!CallDescrWorkerWithHandler+8e
    clr!MethodDesc::CallDescr+194
    clr!MethodDesc::CallTargetWorker+21
    clr!ThreadNative::KickOffThread_Worker+1e1
    clr!Thread::DoExtraWorkForFinalizer+114
    clr!Thread::ShouldChangeAbortToUnload+101
    clr!Thread::ShouldChangeAbortToUnload+399
    clr!Thread::ShouldChangeAbortToUnload+43a
    clr!ManagedThreadBase::KickOff+15
    clr!ThreadNative::KickOffThread+23e
    clr!Thread::intermediateThreadProc+4b
    kernel32!BaseThreadInitThunk+e
    ntdll!__RtlUserThreadStart+70
    ntdll!_RtlUserThreadStart+1b


    Socket properties:
    Source Port 53273
    Destination IP 200.54.10.246
    Destination Port 80



    Thursday, March 28, 2013 12:30 PM

All replies

  • Hi, Dear all

                By the way, If the async read from stream isn't completed ,but the sync signal is times up, and call the stream close method, that will be a problem ?

    Thanks.

    Thursday, March 28, 2013 5:20 PM
  • If there are multiThread to access the Stream, as  one thread is reading the data, another call the close method, it will be locked?
    Friday, March 29, 2013 3:21 AM
  • Hi 匍匐前进,

    Please edit your initial post and insert the source code into the Code Block to format and ease community members out of reading then helping you out:


    Visual Studio Cloud Services

    Friday, March 29, 2013 7:00 AM

  • Hi 匍匐前进,

    For those .NET classes which uses the beginXXX/endXXX async programming pattern, it would be difficult for us to write code which will put multiple async function calls (or even nested ones) together. For your case, it is possible that some async callback function has share the same data and blocking each other. .NET 4.5 (and also provided as add-on component) introduces the Task based asynchronous programming pattern which can makes multiple async function calls easier to write and also make the code very clear (the same syntax as synchronous code). And here is a web article which mentioned how to convert between the traditional APM and task based async pattern:

    #C# 5 Async, Part 3: Preparing Existing code For Await
    http://reedcopsey.com/2010/12/14/c-5-async-part-3-preparing-existing-code-for-await/

    #Using Tasks to implement the APM Pattern
    http://blogs.msdn.com/b/pfxteam/archive/2011/06/27/10179452.aspx

    You can get more information about TAP in the following references:

    #Task-based Asynchronous Pattern (TAP)
    http://msdn.microsoft.com/en-us/library/hh873175.aspx

    #Implementing the Task-based Asynchronous Pattern
    http://msdn.microsoft.com/en-us/library/hh873177.aspx

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, March 29, 2013 10:43 AM
    Moderator
  • Sorry, i can't post my code on it, cuz company's security permission.

    However ,when i review the code, i found a problem:

    when thread "A" is excuting Dispose method, another thread "B" call "A"'s Abort method.

    I know it's a problem. but i am not sure about that will cause both thread "A" and "B" block or dead.

    Best Regards

    Saturday, March 30, 2013 9:59 AM