1

We have a C# ASHX handler which calls into an unmanaged C library using [DllImport].

In some cases, the unmanaged C library got stuck in an infinite loop, however ASP.NET does not terminate it, causing the WorkerProcess w3wp.exe to consume 100% CPU core infinitely.

In another simple test case, where we add C# infinite loop (while(true){}) inside the C# ASHX handler, IIS terminates the execution as expected.

It looks like IIS does not take into account the execution of unmanaged code through [DllImport] into its executionTimeout logic.

Is this by design? How do we get around this, to get IIS to terminate the execution ?

user5133888
  • 139
  • 1
  • 11

3 Answers3

1

The only good way to abort unmanaged code is to run it in a separate process and kill it with Process.Kill().

amit dayama
  • 3,246
  • 2
  • 17
  • 28
0

Since you have unmanaged code, you have to put it in a managed container which correctly implements IDisposable. Inside the container you can control the life cycle of the task invoking unmanaged method, e.g. kill it after a certain timeout.

Community
  • 1
  • 1
Cheng Chen
  • 42,509
  • 16
  • 113
  • 174
  • Interesting idea, we'll give it a quick test. Thanks!! – user5133888 Nov 25 '15 at 08:55
  • as the thread is executed in threadpool, there is no easy way to kill it, the unmanaged thread needs to check whether it has been requested to terminate – user5133888 Nov 26 '15 at 01:38
  • @user5133888: If you want to control the life cycle of the thread, you can't run it via `ThreadPool`. Using `Task` or `Thread` instead. – Cheng Chen Nov 26 '15 at 01:48
0

So at the moment we actually kill the offending thread (not process) directly by checking if that offending thread has consumed too much resources.

user5133888
  • 139
  • 1
  • 11