今天,我的IIS站点占用了很高的cpu,我的服务器非常慢!我从任务管理器那里得到一个转储文件,然后执行IISReset,问题就解决了。
在windbg中,我使用!runaway来查找执行了很长时间的线程,比如:enter image description here
并使用!clrstack找出前10个线程正在执行以下代码: System.Random.NextBytes(Byte[])),如下所示:
Thread 30
Current frame: (MethodDesc 00007ffb5252c418 +0x20 System.Random.InternalSample())
Child-SP RetAddr Caller, Callee
000000487470b460 00007ffb531d8138 (MethodDesc 00007ffb5252c458 +0x28 System.Random.NextBytes(Byte[]))
000000487470b4b0 00007ffaffafe87a (MethodDesc 00007ffafeeb4e20 +0x4a Pinpoint.Core.Util.SpanIdUtil.GetNewSpanId())
000000487470b4e0 00007ffb014a53fb (MethodDesc 00007ffafeeb4e30 +0x1b Pinpoint.Core.Util.SpanIdUtil.GetNextSpanId(Int64, Int64))
000000487470b520 00007ffb014a5370 (MethodDesc 00007ffafeeb4150 +0x30 Pinpoint.Core.TraceId.GetNextTraceId())
000000487470b5a0 00007ffaffae4dab (MethodDesc 00007ffaff317ed0 +0x1cb Mike.Pinpoint.Plugin.WebRequestPlugins.TraceHttpWebRequest.BeforeGetResponse(System.Net.HttpWebRequest))
000000487470bbb0 00007ffb529444e0 (MethodDesc 00007ffb52535888 +0x80 System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(System.Object, System.Object[], System.Object[]))
000000487470bcf0 00007ffb529444e0 (MethodDesc 00007ffb52535888 +0x80 System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(System.Object, System.Object[], System.Object[]))
000000487470bd60 00007ffb5292f4ee (MethodDesc 00007ffb52535870 +0x8e System.Reflection.RuntimeMethodInfo.Invoke(System.Object, System.Reflection.BindingFlags, System.Reflection.Binder, System.Object[], System.Globalization.CultureInfo))
000000487470bde0 00007ffaffae4b79 (MethodDesc 00007ffaff318238 +0x99 Mike.Pinpoint.Plugin.WebRequestPlugins.TraceFilterImpl.BeforeGetResponseForFilter(System.Net.HttpWebRequest))
000000487470bdf0 00007ffb528f93e0 (MethodDesc 00007ffb526b1270 +0x120 System.IO.StreamWriter.Dispose(Boolean))下面是我的原始代码:
using System;
namespace Pinpoint.Core.Util
{
public class SpanIdUtil
{
private static readonly Random rnd = new Random(Environment.TickCount);
public const long Null = -1;
public static long GetNewSpanId()
{
byte[] buf = new byte[8];
rnd.NextBytes(buf);
long newId = BitConverter.ToInt64(buf, 0);
return newId;
}
public static long GetNextSpanId(long spanId, long parentSpanId)
{
long newId = GetNewSpanId();
while (newId == spanId || newId == parentSpanId)
{
newId = GetNewSpanId();
}
return newId;
}
}
}谁能告诉我,是什么导致了这个错误?注意,这种情况只发生过一次。
发布于 2019-03-21 13:32:17
很可能是因为您在多个线程之间共享相同的Random实例,所以在随机类中遇到了一个问题,如here所述,在这个类中,返回零可能会卡住。随机不是线程安全的。
就我个人而言,在服务器应用程序上,我永远不会在没有倒计时保护的情况下使用while循环,如果它到了零,就会抛出异常。除非你能(从数学上)证明你的循环会结束,否则你应该防止它卡住,因为在服务器应用程序中,当它卡住时,你已经永远失去了一个线程。
https://stackoverflow.com/questions/55274210
复制相似问题