fix UnITaskTracker causes Stackoverflow

pull/61/head
neuecc 2020-06-02 22:49:40 +09:00
parent d4cf59bd2f
commit c31dab888e
1 changed files with 30 additions and 13 deletions

View File

@ -60,9 +60,9 @@ namespace Cysharp.Threading.Tasks
#endif #endif
static List<KeyValuePair<IUniTaskSource, (int trackingId, DateTime addTime, string stackTrace)>> listPool = new List<KeyValuePair<IUniTaskSource, (int trackingId, DateTime addTime, string stackTrace)>>(); static List<KeyValuePair<IUniTaskSource, (string formattedType, int trackingId, DateTime addTime, string stackTrace)>> listPool = new List<KeyValuePair<IUniTaskSource, (string formattedType, int trackingId, DateTime addTime, string stackTrace)>>();
static readonly WeakDictionary<IUniTaskSource, (int trackingId, DateTime addTime, string stackTrace)> tracking = new WeakDictionary<IUniTaskSource, (int trackingId, DateTime addTime, string stackTrace)>(); static readonly WeakDictionary<IUniTaskSource, (string formattedType, int trackingId, DateTime addTime, string stackTrace)> tracking = new WeakDictionary<IUniTaskSource, (string formattedType, int trackingId, DateTime addTime, string stackTrace)>();
[Conditional("UNITY_EDITOR")] [Conditional("UNITY_EDITOR")]
public static void TrackActiveTask(IUniTaskSource task, int skipFrame) public static void TrackActiveTask(IUniTaskSource task, int skipFrame)
@ -71,7 +71,9 @@ namespace Cysharp.Threading.Tasks
dirty = true; dirty = true;
if (!EditorEnableState.EnableTracking) return; if (!EditorEnableState.EnableTracking) return;
var stackTrace = EditorEnableState.EnableStackTrace ? new StackTrace(skipFrame, true).CleanupAsyncStackTrace() : ""; var stackTrace = EditorEnableState.EnableStackTrace ? new StackTrace(skipFrame, true).CleanupAsyncStackTrace() : "";
tracking.TryAdd(task, (Interlocked.Increment(ref trackingId), DateTime.UtcNow, stackTrace)); var sb = new StringBuilder();
TypeBeautify(task.GetType(), sb);
tracking.TryAdd(task, (sb.ToString(), Interlocked.Increment(ref trackingId), DateTime.UtcNow, stackTrace));
#endif #endif
} }
@ -104,14 +106,8 @@ namespace Cysharp.Threading.Tasks
{ {
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
{ {
var keyType = listPool[i].Key.GetType(); action(listPool[i].Value.trackingId, listPool[i].Value.formattedType, listPool[i].Key.UnsafeGetStatus(), listPool[i].Value.addTime, listPool[i].Value.stackTrace);
listPool[i] = default;
var sb = new StringBuilder();
TypeBeautify(keyType, sb);
var typeName = sb.ToString();
action(listPool[i].Value.trackingId, typeName, listPool[i].Key.UnsafeGetStatus(), listPool[i].Value.addTime, listPool[i].Value.stackTrace);
listPool[i] = new KeyValuePair<IUniTaskSource, (int trackingId, DateTime addTime, string stackTrace)>(null, (0, default(DateTime), null)); // clear
} }
} }
catch catch
@ -126,17 +122,31 @@ namespace Cysharp.Threading.Tasks
{ {
if (type.IsNested) if (type.IsNested)
{ {
TypeBeautify(type.DeclaringType, sb); // TypeBeautify(type.DeclaringType, sb);
sb.Append(type.DeclaringType.Name.ToString());
sb.Append("."); sb.Append(".");
} }
if (type.IsGenericType) if (type.IsGenericType)
{ {
var genericsStart = type.Name.IndexOf("`"); var genericsStart = type.Name.IndexOf("`");
if (genericsStart != -1)
{
sb.Append(type.Name.Substring(0, genericsStart)); sb.Append(type.Name.Substring(0, genericsStart));
}
else
{
sb.Append(type.Name);
}
sb.Append("<"); sb.Append("<");
var first = true;
foreach (var item in type.GetGenericArguments()) foreach (var item in type.GetGenericArguments())
{ {
if (!first)
{
sb.Append(", ");
}
first = false;
TypeBeautify(item, sb); TypeBeautify(item, sb);
} }
sb.Append(">"); sb.Append(">");
@ -146,6 +156,13 @@ namespace Cysharp.Threading.Tasks
sb.Append(type.Name); sb.Append(type.Name);
} }
} }
//static string RemoveUniTaskNamespace(string str)
//{
// return str.Replace("Cysharp.Threading.Tasks.CompilerServices", "")
// .Replace("Cysharp.Threading.Tasks.Linq", "")
// .Replace("Cysharp.Threading.Tasks", "");
//}
} }
} }