Hanging on “Thread.StartInternal” when handling a ServiceStack request

心不动则不痛 提交于 2019-12-10 23:32:22

问题


I have a ServiceStack (4.0.46) web service which runs fine upon launch, however after having processed a few requests and a non deterministic duration (generally between 30mn and 24 hours), it will eventually get stuck upon handling a request (which it receives well), with the following stack trace:

This particular function from System.Threading.Thread seems to hang (on the last line):

[SecuritySafeCritical]
private void Start(ref StackCrawlMark stackMark)
{
  this.StartupSetApartmentStateInternal();
  if (this.m_Delegate != null)
    ((ThreadHelper) this.m_Delegate.Target).SetExecutionContextHelper(ExecutionContext.Capture(ref stackMark, ExecutionContext.CaptureOptions.IgnoreSyncCtx));
  this.StartInternal(CallContext.Principal, ref stackMark); //HANGS HERE
}

When the service runs fine, this.StartInternal will immediately return. It is extremely unlikely there would be a .NET framework bug here, so I am wondering what could be causing this behaviour.

The Thread instance is obtained from a ThreadPool in the following section of AppHostHttpListenerPoolBase.ListenerCallback(), which also calls Thread.Start():

  this.threadPoolManager.Peek((ThreadStart) (() =>
  {
    this.ProcessRequestContext(context);
    this.threadPoolManager.Free();
  })).Start(); //HANGS HERE

I tried inspecting the detailed stacks of all running threads (including framework and external library code), but do not see any sign of a deadlock or anything else that would explain why the above function would not complete. The project is hosted in a console. Using a AppSelfHostBase instead of a AppHostHttpListenerPoolBase did not help.

I have been struggling with this rather low-level and hard-to-quickly-reproduce issue for weeks, and am running short of ideas to debug and hopefully fix it. Any suggestion is therefore greatly welcome!

A very similar issue here: Thread.Start is not returning in some sparse cases in my c# application

来源:https://stackoverflow.com/questions/33376945/hanging-on-thread-startinternal-when-handling-a-servicestack-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!