0

背景:
我有一个 C#.Net (.Net 4.0) 网站,它调用引擎通过我无法修改的供应商可执行文件从外部数据库中获取数据。每当用户单击特定按钮时,网页将指示引擎产生几个线程,每个线程都会产生一个可执行程序(我们称之为 ABC.exe)的进程来获取数据。然后可执行文件将运行,并将抓取的数据保存到服务器上的 CSV 文件中。然后线程首先读取 CSV 并合并所有数据,进行一些计算并返回到网站。

问题:
当我们将网站部署到 Web 服务器上的 IIS 7.5 时(运行 Window 7,四个虚拟处理器),如果我们产生超过 4 个线程,进程就会卡在那里。

测试完成:

  1. 当我们在同一台服务器上使用 Visual Studio 运行确切的代码时,没有发生错误。所有线程都正确生成,并且所有进程都正确运行。这很奇怪。
  2. 当两个用户单击按钮并且两个单击都将产生 4 个线程时,前四个线程将正常工作,而后四个线程将卡在那里。
  3. 当我们使用不执行任何连接的不同可执行文件运行线程时,代码可以工作。
  4. 在我们的代码中,使用 Task 或 Thread 并没有什么不同。

我们怀疑这可能与 IIS 中允许到特定 IP 的出站连接有关。

任何人都可以对此有所了解吗?让我知道是否需要任何进一步的信息。谢谢!

4

1 回答 1

0

A couple of thoughts:

  1. Could this be tied to thread pool threads? The CLR tries to prevent the CPU from unnecessary spinning by not handing out (initially anyways) more threads on the thread pool thread than there are processors present. Are there 4 virtual processors present? Thread pool threads should resume eventually, though, so make sure that your threads are really stuck, and not running serial.
  2. I know of a deadlock condition when redirecting standard output in C#. Double check the MSDN article to make sure this is not happening to you.

Additional thoughts:
At this point, it sounds like your ABC.exe is suspect. Since you have Visual Studio on the server, I recommend that you fire up a separate instance of Visual Studio, and attach to one of the ABC.exe processes to see where it is hanging. Also try to run something that you are reasonably sure would exit, e.g. cmd /c dir instead of your ABC.exe.

Edit: 5/29:
I find it hard to believe that IIS would restrict outbound connections this way. Try the following simple downloaded instead of ABC.exe:

class Program
{
    public static void Main(string[] args)
    {
        using (var reader = new System.IO.StreamReader(
            System.Net.HttpWebRequest.Create("http://www.google.com")
            .GetResponse().GetResponseStream()))
        {
            System.Console.WriteLine(reader.ReadToEnd());
        }
    }
}
于 2013-05-29T04:22:16.093 回答