【发布时间】:2017-03-23 22:09:58
【问题描述】:
以下是一个完整的控制台程序,它重现了我遇到的一个奇怪错误。该程序读取一个包含远程文件 url 的文件,每行一个。它会启动 50 个线程以将它们全部下载。
static void Main(string[] args)
{
try
{
string filePath = ConfigurationManager.AppSettings["filePath"],
folder = ConfigurationManager.AppSettings["folder"];
Directory.CreateDirectory(folder);
List<string> urls = File.ReadAllLines(filePath).Take(10000).ToList();
int urlIX = -1;
Task.WaitAll(Enumerable.Range(0, 50).Select(x => Task.Factory.StartNew(() =>
{
while (true)
{
int curUrlIX = Interlocked.Increment(ref urlIX);
if (curUrlIX >= urls.Count)
break;
string url = urls[curUrlIX];
try
{
var req = (HttpWebRequest)WebRequest.Create(url);
using (var res = (HttpWebResponse)req.GetResponse())
using (var resStream = res.GetResponseStream())
using (var fileStream = File.Create(Path.Combine(folder, Guid.NewGuid() + url.Substring(url.LastIndexOf('.')))))
resStream.CopyTo(fileStream);
}
catch (Exception ex)
{
Console.WriteLine("Error downloading img: " + url + "\n" + ex);
continue;
}
}
})).ToArray());
}
catch
{
Console.WriteLine("Something bad happened.");
}
}
在我的本地计算机上它工作正常。在服务器上,下载了几百张图片后,显示Attempted to read or write protected memory 或Unable to read data from the transport connection: A blocking operation was interrupted by a call to WSACancelBlockingCall. 的错误。
这似乎是一个原生错误,因为内部和外部的 catch 都没有捕捉到它。我从没见过Something bad happened.。
我在WinDbg 中运行它,它显示如下:
(3200.1790): Access violation - code c0000005 (first chance)
First chance exceptions are reported before any exception handling.
This exception may be expected and handled.
LavasoftTcpService64+0x765f:
00000001`8000765f 807a1900 cmp byte ptr [rdx+19h],0 ds:baadf00d`0000001a=??
0:006> g
(3200.326c): CLR exception - code e0434352 (first chance)
(3200.326c): CLR exception - code e0434352 (first chance)
(3200.2b9c): Access violation - code c0000005 (!!! second chance !!!)
LavasoftTcpService64!WSPStartup+0x9749:
00000001`8002c8b9 f3a4 rep movs byte ptr [rdi],byte ptr [rsi]
我刚刚关闭了 Lavasoft,现在 WinDbg 显示如下:
Critical error detected c0000374
(3c4.3494): Break instruction exception - code 80000003 (first chance)
ntdll!RtlReportCriticalFailure+0x4b:
00007fff`4acf1b2f cc int 3
0:006> g
(3c4.3494): Unknown exception - code c0000374 (first chance)
(3c4.3494): Unknown exception - code c0000374 (!!! second chance !!!)
ntdll!RtlReportCriticalFailure+0x8c:
00007fff`4acf1b70 eb00 jmp ntdll!RtlReportCriticalFailure+0x8e (00007fff`4acf1b72)
0:006> g
WARNING: Continuing a non-continuable exception
(3c4.3494): C++ EH exception - code e06d7363 (first chance)
HEAP[VIPJobsTest.exe]: HEAP: Free Heap block 0000007AB96CC5D0 modified at 0000007AB96CC748 after it was freed
(3c4.3494): Break instruction exception - code 80000003 (first chance)
ntdll!RtlpBreakPointHeap+0x1d:
00007fff`4acf3991 cc int 3
【问题讨论】:
-
你的外部catch不会捕捉到线程中的东西,线程中有一堆东西不在catch内
-
尝试捕获整个 while 循环
-
您是否需要转到服务器上的 lavasoft 广告感知程序,并从广告软件扫描中排除您的可执行文件。
-
@SqlSurfer 这不是 Lavasoft - 请查看我附加到问题的内容。
-
@KeithNicholas 我刚刚尝试将
try移高 4 行,但还是一样。
标签: c# multithreading task-parallel-library