修复一直卡加载问题

This commit is contained in:
lujiajian 2025-11-05 14:07:34 +08:00
parent dab87a6c2b
commit c930f5d7b7
36 changed files with 794 additions and 9658 deletions

View File

@ -68,8 +68,7 @@ namespace Cysharp.Threading.Tasks.Editor
public static bool EnableStackTrace => TaskTracker.EditorEnableState.EnableStackTrace; public static bool EnableStackTrace => TaskTracker.EditorEnableState.EnableStackTrace;
static readonly GUIContent EnableAutoReloadHeadContent = EditorGUIUtility.TrTextContent("Enable AutoReload", "Reload automatically.", (Texture)null); static readonly GUIContent EnableAutoReloadHeadContent = EditorGUIUtility.TrTextContent("Enable AutoReload", "Reload automatically.", (Texture)null);
static readonly GUIContent ReloadHeadContent = EditorGUIUtility.TrTextContent("Reload", "Reload View.", (Texture)null); static readonly GUIContent ReloadHeadContent = EditorGUIUtility.TrTextContent("Reload", "Reload View.", (Texture)null);
// GC.Collect 按钮已移除,避免频繁强制垃圾回收影响性能 static readonly GUIContent GCHeadContent = EditorGUIUtility.TrTextContent("GC.Collect", "Invoke GC.Collect.", (Texture)null);
// static readonly GUIContent GCHeadContent = EditorGUIUtility.TrTextContent("GC.Collect", "Invoke GC.Collect.", (Texture)null);
static readonly GUIContent EnableTrackingHeadContent = EditorGUIUtility.TrTextContent("Enable Tracking", "Start to track async/await UniTask. Performance impact: low", (Texture)null); static readonly GUIContent EnableTrackingHeadContent = EditorGUIUtility.TrTextContent("Enable Tracking", "Start to track async/await UniTask. Performance impact: low", (Texture)null);
static readonly GUIContent EnableStackTraceHeadContent = EditorGUIUtility.TrTextContent("Enable StackTrace", "Capture StackTrace when task is started. Performance impact: high", (Texture)null); static readonly GUIContent EnableStackTraceHeadContent = EditorGUIUtility.TrTextContent("Enable StackTrace", "Capture StackTrace when task is started. Performance impact: high", (Texture)null);
@ -103,14 +102,10 @@ namespace Cysharp.Threading.Tasks.Editor
Repaint(); Repaint();
} }
// 移除 GC.Collect() 调用,避免频繁强制垃圾回收影响性能 if (GUILayout.Button(GCHeadContent, EditorStyles.toolbarButton, EmptyLayoutOption))
// .NET 的垃圾回收器会自动管理内存,不需要手动调用 GC.Collect() {
// 如果确实需要释放内存,应使用 using 语句或 IDisposable 接口释放非托管资源 GC.Collect(0);
// 注意:此按钮已移除,如需调试内存问题,请使用 Unity Profiler }
// if (GUILayout.Button(GCHeadContent, EditorStyles.toolbarButton, EmptyLayoutOption))
// {
// GC.Collect(0);
// }
EditorGUILayout.EndHorizontal(); EditorGUILayout.EndHorizontal();
EditorGUILayout.EndVertical(); EditorGUILayout.EndVertical();

View File

@ -99,19 +99,6 @@ namespace Cysharp.Threading.Tasks
awaiter.GetResult(); awaiter.GetResult();
completionSource.TrySetResult(); completionSource.TrySetResult();
} }
catch (OperationCanceledException ex)
{
// 操作被取消,设置为取消状态
completionSource.TrySetException(ex);
}
catch (ObjectDisposedException ex)
{
completionSource.TrySetException(ex);
}
catch (InvalidOperationException ex)
{
completionSource.TrySetException(ex);
}
catch (Exception ex) catch (Exception ex)
{ {
completionSource.TrySetException(ex); completionSource.TrySetException(ex);
@ -231,19 +218,6 @@ namespace Cysharp.Threading.Tasks
var result = awaiter.GetResult(); var result = awaiter.GetResult();
completionSource.TrySetResult(result); completionSource.TrySetResult(result);
} }
catch (OperationCanceledException ex)
{
// 操作被取消,设置为取消状态
completionSource.TrySetException(ex);
}
catch (ObjectDisposedException ex)
{
completionSource.TrySetException(ex);
}
catch (InvalidOperationException ex)
{
completionSource.TrySetException(ex);
}
catch (Exception ex) catch (Exception ex)
{ {
completionSource.TrySetException(ex); completionSource.TrySetException(ex);

View File

@ -56,17 +56,7 @@ namespace Cysharp.Threading.Tasks
public void Dispose() public void Dispose()
{ {
Dispose(true); triggerEvent.SetCompleted();
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
// 释放托管资源
triggerEvent.SetCompleted();
}
} }
public static implicit operator T(AsyncReactiveProperty<T> value) public static implicit operator T(AsyncReactiveProperty<T> value)
@ -379,22 +369,12 @@ namespace Cysharp.Threading.Tasks
public void Dispose() public void Dispose()
{ {
Dispose(true); if (enumerator != null)
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{ {
// 释放托管资源 enumerator.DisposeAsync().Forget();
if (enumerator != null)
{
enumerator.DisposeAsync().Forget();
}
triggerEvent.SetCompleted();
} }
triggerEvent.SetCompleted();
} }
public static implicit operator T(ReadOnlyAsyncReactiveProperty<T> value) public static implicit operator T(ReadOnlyAsyncReactiveProperty<T> value)

View File

@ -13,11 +13,6 @@ namespace Cysharp.Threading.Tasks
return 0; return 0;
} }
public override bool Equals(object obj)
{
return base.Equals(obj);
}
public bool Equals(AsyncUnit other) public bool Equals(AsyncUnit other)
{ {
return true; return true;

View File

@ -163,9 +163,9 @@ namespace Cysharp.Threading.Tasks
public bool IsCompleted => !cancellationToken.CanBeCanceled || cancellationToken.IsCancellationRequested; public bool IsCompleted => !cancellationToken.CanBeCanceled || cancellationToken.IsCancellationRequested;
public void GetResult()
{
}
public void OnCompleted(Action continuation) public void OnCompleted(Action continuation)
{ {

View File

@ -222,10 +222,6 @@ namespace Cysharp.Threading.Tasks
case WaitForSeconds wfs: case WaitForSeconds wfs:
innerCoroutine = UnwrapWaitForSeconds(wfs); innerCoroutine = UnwrapWaitForSeconds(wfs);
break; break;
default:
// 未知的YieldInstruction类型设为null以便后续处理
innerCoroutine = null;
break;
} }
if (innerCoroutine != null) if (innerCoroutine != null)
{ {

View File

@ -59,7 +59,7 @@ namespace Cysharp.Threading.Tasks.Internal
} }
finally finally
{ {
if (lockTaken) locks[index].Exit(false);
} }
} }
@ -97,7 +97,7 @@ namespace Cysharp.Threading.Tasks.Internal
} }
finally finally
{ {
if (lockTaken) locks[index].Exit(false);
} }
} }
} }

View File

@ -66,7 +66,7 @@ namespace Cysharp.Threading.Tasks.Internal
} }
finally finally
{ {
if (lockTaken) gate.Exit(false);
} }
} }
@ -180,7 +180,7 @@ namespace Cysharp.Threading.Tasks.Internal
} }
finally finally
{ {
if (lockTaken) gate.Exit(false);
} }
} }
@ -216,7 +216,7 @@ namespace Cysharp.Threading.Tasks.Internal
} }
finally finally
{ {
gate.Exit(false); if (lockTaken) gate.Exit(false);
} }
} }
} }

View File

@ -18,7 +18,7 @@ namespace Cysharp.Threading.Tasks.Internal
public MinimumQueue(int capacity) public MinimumQueue(int capacity)
{ {
if (capacity < 0)throw new ArgumentOutOfRangeException("capacity"); if (capacity < 0) throw new ArgumentOutOfRangeException("capacity");
array = new T[capacity]; array = new T[capacity];
head = tail = size = 0; head = tail = size = 0;
} }

View File

@ -188,11 +188,7 @@ namespace Cysharp.Threading.Tasks.Internal
{ {
unhandledExceptionCallback(ex); unhandledExceptionCallback(ex);
} }
catch (Exception callbackEx) catch { }
{
// 记录异常回调本身也抛出异常的情况,避免异常被静默忽略
Debug.LogError($"PlayerLoopRunner: 异常回调函数执行失败 - {callbackEx.Message}\n原始异常: {ex.Message}\n回调异常堆栈: {callbackEx.StackTrace}");
}
} }
} }
@ -227,11 +223,7 @@ namespace Cysharp.Threading.Tasks.Internal
{ {
unhandledExceptionCallback(ex); unhandledExceptionCallback(ex);
} }
catch (Exception callbackEx) catch { }
{
// 记录异常回调本身也抛出异常的情况,避免异常被静默忽略
Debug.LogError($"PlayerLoopRunner: 异常回调函数执行失败 - {callbackEx.Message}\n原始异常: {ex.Message}\n回调异常堆栈: {callbackEx.StackTrace}");
}
continue; // next j continue; // next j
} }
} }

View File

@ -33,17 +33,7 @@ namespace Cysharp.Threading.Tasks.Internal
public void Dispose() public void Dispose()
{ {
Dispose(true); StatePool<T1>.Return(this);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
// 释放托管资源
StatePool<T1>.Return(this);
}
} }
} }
@ -84,17 +74,7 @@ namespace Cysharp.Threading.Tasks.Internal
public void Dispose() public void Dispose()
{ {
Dispose(true); StatePool<T1, T2>.Return(this);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
// 释放托管资源
StatePool<T1, T2>.Return(this);
}
} }
} }
@ -139,17 +119,7 @@ namespace Cysharp.Threading.Tasks.Internal
public void Dispose() public void Dispose()
{ {
Dispose(true); StatePool<T1, T2, T3>.Return(this);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
// 释放托管资源
StatePool<T1, T2, T3>.Return(this);
}
} }
} }

View File

@ -231,14 +231,14 @@ namespace Cysharp.Threading.Tasks.Internal
// avoid allocate everytime. // avoid allocate everytime.
public int ToList(ref List<KeyValuePair<TKey, TValue>> list, bool clear = true) public int ToList(ref List<KeyValuePair<TKey, TValue>> list, bool clear = true)
{ {
if (clear)
{
list.Clear();
}
var listIndex = 0; var listIndex = 0;
bool lockTaken = false;
try try
{ {
for (int i = 0; i < buckets.Length; i++) for (int i = 0; i < buckets.Length; i++)
@ -271,7 +271,7 @@ namespace Cysharp.Threading.Tasks.Internal
} }
finally finally
{ {
gate.Exit(false); if (lockTaken) gate.Exit(false);
} }
return listIndex; return listIndex;

View File

@ -70,22 +70,6 @@ namespace Cysharp.Threading.Tasks.Linq
goto CONTINUE; goto CONTINUE;
} }
} }
catch (OperationCanceledException ex)
{
// 操作被取消,设置为取消状态
completionSource.TrySetException(ex);
return;
}
catch (ObjectDisposedException ex)
{
completionSource.TrySetException(ex);
return;
}
catch (InvalidOperationException ex)
{
completionSource.TrySetException(ex);
return;
}
catch (Exception ex) catch (Exception ex)
{ {
completionSource.TrySetException(ex); completionSource.TrySetException(ex);
@ -233,22 +217,6 @@ namespace Cysharp.Threading.Tasks.Linq
completionSource.TrySetResult(result); completionSource.TrySetResult(result);
} }
} }
catch (OperationCanceledException ex)
{
// 操作被取消,设置为取消状态
completionSource.TrySetException(ex);
return;
}
catch (ObjectDisposedException ex)
{
completionSource.TrySetException(ex);
return;
}
catch (InvalidOperationException ex)
{
completionSource.TrySetException(ex);
return;
}
catch (Exception ex) catch (Exception ex)
{ {
completionSource.TrySetException(ex); completionSource.TrySetException(ex);

File diff suppressed because it is too large Load Diff

View File

@ -109,20 +109,6 @@ namespace Cysharp.Threading.Tasks.Linq
await task; await task;
goto DONE; goto DONE;
} }
catch (OperationCanceledException ex)
{
// 操作被取消,设置为取消状态
Volatile.Write(ref state, -2);
completionSource.TrySetException(ex);
return;
}
catch (InvalidOperationException ex)
{
Volatile.Write(ref state, -2);
completionSource.TrySetException(ex);
return;
}
catch (Exception ex) catch (Exception ex)
{ {
Volatile.Write(ref state, -2); Volatile.Write(ref state, -2);

View File

@ -175,20 +175,6 @@ namespace Cysharp.Threading.Tasks.Linq
goto DONE; goto DONE;
} }
} }
catch (OperationCanceledException ex)
{
// 操作被取消,设置为取消状态
state = -2;
completionSource.TrySetException(ex);
return;
}
catch (InvalidOperationException ex)
{
state = -2;
completionSource.TrySetException(ex);
return;
}
catch (Exception ex) catch (Exception ex)
{ {
state = -2; state = -2;
@ -333,20 +319,6 @@ namespace Cysharp.Threading.Tasks.Linq
goto DONE; goto DONE;
} }
} }
catch (OperationCanceledException ex)
{
// 操作被取消,设置为取消状态
state = -2;
completionSource.TrySetException(ex);
return;
}
catch (InvalidOperationException ex)
{
state = -2;
completionSource.TrySetException(ex);
return;
}
catch (Exception ex) catch (Exception ex)
{ {
state = -2; state = -2;
@ -505,20 +477,6 @@ namespace Cysharp.Threading.Tasks.Linq
goto DONE; goto DONE;
} }
} }
catch (OperationCanceledException ex)
{
// 操作被取消,设置为取消状态
state = -2;
completionSource.TrySetException(ex);
return;
}
catch (InvalidOperationException ex)
{
state = -2;
completionSource.TrySetException(ex);
return;
}
catch (Exception ex) catch (Exception ex)
{ {
state = -2; state = -2;
@ -677,20 +635,6 @@ namespace Cysharp.Threading.Tasks.Linq
goto DONE; goto DONE;
} }
} }
catch (OperationCanceledException ex)
{
// 操作被取消,设置为取消状态
state = -2;
completionSource.TrySetException(ex);
return;
}
catch (InvalidOperationException ex)
{
state = -2;
completionSource.TrySetException(ex);
return;
}
catch (Exception ex) catch (Exception ex)
{ {
state = -2; state = -2;

View File

@ -15,7 +15,7 @@ namespace Cysharp.Threading.Tasks.Linq
public static readonly IUniTaskAsyncEnumerable<T> Instance = new Empty<T>(); public static readonly IUniTaskAsyncEnumerable<T> Instance = new Empty<T>();
Empty() Empty()
{UnityEngine.Debug.Log("Empty<T> created"); {
} }
public IUniTaskAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default) public IUniTaskAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default)

View File

@ -57,16 +57,6 @@ namespace Cysharp.Threading.Tasks.Linq
} }
trigger.SetCompleted(); trigger.SetCompleted();
} }
catch (OperationCanceledException ex)
{
// 操作被取消,设置为错误状态
trigger.SetError(ex);
}
catch (InvalidOperationException ex)
{
trigger.SetError(ex);
}
catch (Exception ex) catch (Exception ex)
{ {
trigger.SetError(ex); trigger.SetError(ex);

View File

@ -10,7 +10,7 @@ namespace Cysharp.Threading.Tasks.Linq
if (count < 0) throw Error.ArgumentOutOfRange(nameof(count)); if (count < 0) throw Error.ArgumentOutOfRange(nameof(count));
var end = (long)start + count - 1L; var end = (long)start + count - 1L;
throw Error.ArgumentOutOfRange(nameof(count)); if (end > int.MaxValue) throw Error.ArgumentOutOfRange(nameof(count));
if (count == 0) UniTaskAsyncEnumerable.Empty<int>(); if (count == 0) UniTaskAsyncEnumerable.Empty<int>();

View File

@ -322,29 +322,6 @@ namespace Cysharp.Threading.Tasks.Linq
} }
onCompleted(); onCompleted();
} }
catch (OperationCanceledException)
{
// 操作被取消,直接返回,不触发错误回调
return;
}
catch (ObjectDisposedException ex)
{
if (onError == NopError)
{
UniTaskScheduler.PublishUnobservedTaskException(ex);
return;
}
onError(ex);
}
catch (InvalidOperationException ex)
{
if (onError == NopError)
{
UniTaskScheduler.PublishUnobservedTaskException(ex);
return;
}
onError(ex);
}
catch (Exception ex) catch (Exception ex)
{ {
if (onError == NopError) if (onError == NopError)
@ -353,6 +330,8 @@ namespace Cysharp.Threading.Tasks.Linq
return; return;
} }
if (ex is OperationCanceledException) return;
onError(ex); onError(ex);
} }
finally finally
@ -382,29 +361,6 @@ namespace Cysharp.Threading.Tasks.Linq
} }
onCompleted(); onCompleted();
} }
catch (OperationCanceledException)
{
// 操作被取消,直接返回,不触发错误回调
return;
}
catch (ObjectDisposedException ex)
{
if (onError == NopError)
{
UniTaskScheduler.PublishUnobservedTaskException(ex);
return;
}
onError(ex);
}
catch (InvalidOperationException ex)
{
if (onError == NopError)
{
UniTaskScheduler.PublishUnobservedTaskException(ex);
return;
}
onError(ex);
}
catch (Exception ex) catch (Exception ex)
{ {
if (onError == NopError) if (onError == NopError)
@ -413,6 +369,8 @@ namespace Cysharp.Threading.Tasks.Linq
return; return;
} }
if (ex is OperationCanceledException) return;
onError(ex); onError(ex);
} }
finally finally
@ -442,29 +400,6 @@ namespace Cysharp.Threading.Tasks.Linq
} }
onCompleted(); onCompleted();
} }
catch (OperationCanceledException)
{
// 操作被取消,直接返回,不触发错误回调
return;
}
catch (ObjectDisposedException ex)
{
if (onError == NopError)
{
UniTaskScheduler.PublishUnobservedTaskException(ex);
return;
}
onError(ex);
}
catch (InvalidOperationException ex)
{
if (onError == NopError)
{
UniTaskScheduler.PublishUnobservedTaskException(ex);
return;
}
onError(ex);
}
catch (Exception ex) catch (Exception ex)
{ {
if (onError == NopError) if (onError == NopError)
@ -473,6 +408,8 @@ namespace Cysharp.Threading.Tasks.Linq
return; return;
} }
if (ex is OperationCanceledException) return;
onError(ex); onError(ex);
} }
finally finally
@ -502,18 +439,10 @@ namespace Cysharp.Threading.Tasks.Linq
} }
observer.OnCompleted(); observer.OnCompleted();
} }
catch (OperationCanceledException)
{
// 操作被取消,直接返回,不触发错误回调
return;
}
catch (InvalidOperationException ex)
{
observer.OnError(ex);
}
catch (Exception ex) catch (Exception ex)
{ {
if (ex is OperationCanceledException) return;
observer.OnError(ex); observer.OnError(ex);
} }
finally finally
@ -543,29 +472,6 @@ namespace Cysharp.Threading.Tasks.Linq
} }
onCompleted(); onCompleted();
} }
catch (OperationCanceledException)
{
// 操作被取消,直接返回,不触发错误回调
return;
}
catch (ObjectDisposedException ex)
{
if (onError == NopError)
{
UniTaskScheduler.PublishUnobservedTaskException(ex);
return;
}
onError(ex);
}
catch (InvalidOperationException ex)
{
if (onError == NopError)
{
UniTaskScheduler.PublishUnobservedTaskException(ex);
return;
}
onError(ex);
}
catch (Exception ex) catch (Exception ex)
{ {
if (onError == NopError) if (onError == NopError)
@ -574,6 +480,8 @@ namespace Cysharp.Threading.Tasks.Linq
return; return;
} }
if (ex is OperationCanceledException) return;
onError(ex); onError(ex);
} }
finally finally
@ -603,29 +511,6 @@ namespace Cysharp.Threading.Tasks.Linq
} }
onCompleted(); onCompleted();
} }
catch (OperationCanceledException)
{
// 操作被取消,直接返回,不触发错误回调
return;
}
catch (ObjectDisposedException ex)
{
if (onError == NopError)
{
UniTaskScheduler.PublishUnobservedTaskException(ex);
return;
}
onError(ex);
}
catch (InvalidOperationException ex)
{
if (onError == NopError)
{
UniTaskScheduler.PublishUnobservedTaskException(ex);
return;
}
onError(ex);
}
catch (Exception ex) catch (Exception ex)
{ {
if (onError == NopError) if (onError == NopError)
@ -634,6 +519,8 @@ namespace Cysharp.Threading.Tasks.Linq
return; return;
} }
if (ex is OperationCanceledException) return;
onError(ex); onError(ex);
} }
finally finally

View File

@ -143,25 +143,6 @@ namespace Cysharp.Threading.Tasks.Linq
return true; return true;
} }
} }
catch (OperationCanceledException ex)
{
// 操作被取消,设置为取消状态
completionSource.TrySetException(ex);
DisposeAsync().Forget();
return false;
}
catch (InvalidOperationException ex)
{
completionSource.TrySetException(ex);
DisposeAsync().Forget();
return false;
}
catch (NullReferenceException ex)
{
completionSource.TrySetException(ex);
DisposeAsync().Forget();
return false;
}
catch (Exception ex) catch (Exception ex)
{ {
completionSource.TrySetException(ex); completionSource.TrySetException(ex);
@ -295,25 +276,6 @@ namespace Cysharp.Threading.Tasks.Linq
return true; return true;
} }
} }
catch (OperationCanceledException ex)
{
// 操作被取消,设置为取消状态
completionSource.TrySetException(ex);
DisposeAsync().Forget();
return false;
}
catch (InvalidOperationException ex)
{
completionSource.TrySetException(ex);
DisposeAsync().Forget();
return false;
}
catch (NullReferenceException ex)
{
completionSource.TrySetException(ex);
DisposeAsync().Forget();
return false;
}
catch (Exception ex) catch (Exception ex)
{ {
completionSource.TrySetException(ex); completionSource.TrySetException(ex);

View File

@ -125,22 +125,6 @@ namespace Cysharp.Threading.Tasks.Linq
{ {
self.secondAwaiter = self.secondEnumerator.MoveNextAsync().GetAwaiter(); self.secondAwaiter = self.secondEnumerator.MoveNextAsync().GetAwaiter();
} }
catch (OperationCanceledException ex)
{
// 操作被取消,设置为取消状态
self.completionSource.TrySetException(ex);
return;
}
catch (ObjectDisposedException ex)
{
self.completionSource.TrySetException(ex);
return;
}
catch (InvalidOperationException ex)
{
self.completionSource.TrySetException(ex);
return;
}
catch (Exception ex) catch (Exception ex)
{ {
self.completionSource.TrySetException(ex); self.completionSource.TrySetException(ex);
@ -175,19 +159,6 @@ namespace Cysharp.Threading.Tasks.Linq
{ {
self.Current = self.resultSelector(self.firstEnumerator.Current, self.secondEnumerator.Current); self.Current = self.resultSelector(self.firstEnumerator.Current, self.secondEnumerator.Current);
} }
catch (OperationCanceledException ex)
{
// 操作被取消,设置为取消状态
self.completionSource.TrySetException(ex);
}
catch (InvalidOperationException ex)
{
self.completionSource.TrySetException(ex);
}
catch (ArgumentNullException ex)
{
self.completionSource.TrySetException(ex);
}
catch (Exception ex) catch (Exception ex)
{ {
self.completionSource.TrySetException(ex); self.completionSource.TrySetException(ex);
@ -308,22 +279,6 @@ namespace Cysharp.Threading.Tasks.Linq
{ {
self.secondAwaiter = self.secondEnumerator.MoveNextAsync().GetAwaiter(); self.secondAwaiter = self.secondEnumerator.MoveNextAsync().GetAwaiter();
} }
catch (OperationCanceledException ex)
{
// 操作被取消,设置为取消状态
self.completionSource.TrySetException(ex);
return;
}
catch (ObjectDisposedException ex)
{
self.completionSource.TrySetException(ex);
return;
}
catch (InvalidOperationException ex)
{
self.completionSource.TrySetException(ex);
return;
}
catch (Exception ex) catch (Exception ex)
{ {
self.completionSource.TrySetException(ex); self.completionSource.TrySetException(ex);
@ -366,19 +321,6 @@ namespace Cysharp.Threading.Tasks.Linq
self.resultAwaiter.SourceOnCompleted(resultAwaitCoreDelegate, self); self.resultAwaiter.SourceOnCompleted(resultAwaitCoreDelegate, self);
} }
} }
catch (OperationCanceledException ex)
{
// 操作被取消,设置为取消状态
self.completionSource.TrySetException(ex);
}
catch (InvalidOperationException ex)
{
self.completionSource.TrySetException(ex);
}
catch (ArgumentNullException ex)
{
self.completionSource.TrySetException(ex);
}
catch (Exception ex) catch (Exception ex)
{ {
self.completionSource.TrySetException(ex); self.completionSource.TrySetException(ex);
@ -509,22 +451,6 @@ namespace Cysharp.Threading.Tasks.Linq
{ {
self.secondAwaiter = self.secondEnumerator.MoveNextAsync().GetAwaiter(); self.secondAwaiter = self.secondEnumerator.MoveNextAsync().GetAwaiter();
} }
catch (OperationCanceledException ex)
{
// 操作被取消,设置为取消状态
self.completionSource.TrySetException(ex);
return;
}
catch (ObjectDisposedException ex)
{
self.completionSource.TrySetException(ex);
return;
}
catch (InvalidOperationException ex)
{
self.completionSource.TrySetException(ex);
return;
}
catch (Exception ex) catch (Exception ex)
{ {
self.completionSource.TrySetException(ex); self.completionSource.TrySetException(ex);
@ -567,19 +493,6 @@ namespace Cysharp.Threading.Tasks.Linq
self.resultAwaiter.SourceOnCompleted(resultAwaitCoreDelegate, self); self.resultAwaiter.SourceOnCompleted(resultAwaitCoreDelegate, self);
} }
} }
catch (OperationCanceledException ex)
{
// 操作被取消,设置为取消状态
self.completionSource.TrySetException(ex);
}
catch (InvalidOperationException ex)
{
self.completionSource.TrySetException(ex);
}
catch (ArgumentNullException ex)
{
self.completionSource.TrySetException(ex);
}
catch (Exception ex) catch (Exception ex)
{ {
self.completionSource.TrySetException(ex); self.completionSource.TrySetException(ex);

View File

@ -299,11 +299,7 @@ namespace Cysharp.Threading.Tasks
{ {
applicationDataPath = Application.dataPath; applicationDataPath = Application.dataPath;
} }
catch (Exception ex) catch { }
{
// 记录初始化Application.dataPath时发生的异常使用空字符串作为默认值
UnityEngine.Debug.LogWarning($"PlayerLoopHelper: 无法获取Application.dataPath使用空字符串作为默认值。错误: {ex.Message}\n堆栈: {ex.StackTrace}");
}
#if UNITY_EDITOR && UNITY_2019_3_OR_NEWER #if UNITY_EDITOR && UNITY_2019_3_OR_NEWER
// When domain reload is disabled, re-initialization is required when entering play mode; // When domain reload is disabled, re-initialization is required when entering play mode;

View File

@ -101,25 +101,7 @@ namespace Cysharp.Threading.Tasks
public void Dispose() public void Dispose()
{ {
Dispose(true); isDisposed = true;
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!isDisposed)
{
if (disposing)
{
// 释放托管资源
// 停止定时器运行
tryStop = true;
isRunning = false;
}
// 释放非托管资源(如果有)
isDisposed = true;
}
} }
bool IPlayerLoopItem.MoveNext() bool IPlayerLoopItem.MoveNext()

View File

@ -30,11 +30,7 @@ namespace Cysharp.Threading.Tasks
} }
} }
} }
catch (Exception ex) catch { }
{
// 记录读取环境变量失败的情况使用默认值MaxPoolSize = int.MaxValue
UnityEngine.Debug.LogWarning($"TaskPool: 无法读取环境变量 UNITASK_MAX_POOLSIZE使用默认值 int.MaxValue。错误: {ex.Message}");
}
MaxPoolSize = int.MaxValue; MaxPoolSize = int.MaxValue;
} }

View File

@ -25,12 +25,9 @@ namespace Cysharp.Threading.Tasks
void LogError(Exception ex) void LogError(Exception ex)
{ {
#if UNITY_2018_3_OR_NEWER #if UNITY_2018_3_OR_NEWER
// Unity平台的 LogException 方法直接传递异常对象,是安全的
UnityEngine.Debug.LogException(ex); UnityEngine.Debug.LogException(ex);
#else #else
// 使用安全清理方法处理异常消息,防止日志注入攻击 Console.WriteLine(ex);
string safeMessage = UniTaskScheduler.SanitizeExceptionMessage(ex.ToString());
Console.WriteLine("TriggerEvent Error: " + safeMessage);
#endif #endif
} }
@ -50,22 +47,6 @@ namespace Cysharp.Threading.Tasks
{ {
h.OnNext(value); h.OnNext(value);
} }
catch (OperationCanceledException ex)
{
// 操作被取消,记录错误
LogError(ex);
Remove(h);
}
catch (InvalidOperationException ex)
{
LogError(ex);
Remove(h);
}
catch (NullReferenceException ex)
{
LogError(ex);
Remove(h);
}
catch (Exception ex) catch (Exception ex)
{ {
LogError(ex); LogError(ex);
@ -100,19 +81,6 @@ namespace Cysharp.Threading.Tasks
{ {
h.OnCanceled(cancellationToken); h.OnCanceled(cancellationToken);
} }
catch (OperationCanceledException ex)
{
// 操作被取消,记录错误
LogError(ex);
}
catch (InvalidOperationException ex)
{
LogError(ex);
}
catch (NullReferenceException ex)
{
LogError(ex);
}
catch (Exception ex) catch (Exception ex)
{ {
LogError(ex); LogError(ex);
@ -147,19 +115,6 @@ namespace Cysharp.Threading.Tasks
{ {
h.OnCompleted(); h.OnCompleted();
} }
catch (OperationCanceledException ex)
{
// 操作被取消,记录错误
LogError(ex);
}
catch (InvalidOperationException ex)
{
LogError(ex);
}
catch (NullReferenceException ex)
{
LogError(ex);
}
catch (Exception ex) catch (Exception ex)
{ {
LogError(ex); LogError(ex);
@ -194,19 +149,6 @@ namespace Cysharp.Threading.Tasks
{ {
h.OnError(exception); h.OnError(exception);
} }
catch (OperationCanceledException ex)
{
// 操作被取消,记录错误
LogError(ex);
}
catch (InvalidOperationException ex)
{
LogError(ex);
}
catch (NullReferenceException ex)
{
LogError(ex);
}
catch (Exception ex) catch (Exception ex)
{ {
LogError(ex); LogError(ex);

View File

@ -1088,7 +1088,7 @@ namespace Cysharp.Threading.Tasks
public bool IsCompleted => false; public bool IsCompleted => false;
public void GetResult() {Debug.Log("GetResult"); } public void GetResult() { }
public void OnCompleted(Action continuation) public void OnCompleted(Action continuation)
{ {

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -113,10 +113,8 @@ namespace Cysharp.Threading.Tasks
UniTaskScheduler.PublishUnobservedTaskException(e.GetException().SourceException); UniTaskScheduler.PublishUnobservedTaskException(e.GetException().SourceException);
} }
} }
catch (Exception reportEx) catch
{ {
// 记录报告未处理异常时发生的错误,避免异常被静默忽略
UnityEngine.Debug.LogError($"UniTaskCompletionSource: 报告未处理的异常时发生错误 - {reportEx.Message}\n堆栈: {reportEx.StackTrace}");
} }
} }
} }

View File

@ -1,7 +1,6 @@
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
using System; using System;
using System.Diagnostics;
using System.Runtime.ExceptionServices; using System.Runtime.ExceptionServices;
using System.Threading; using System.Threading;
using Cysharp.Threading.Tasks.Internal; using Cysharp.Threading.Tasks.Internal;
@ -296,18 +295,11 @@ namespace Cysharp.Threading.Tasks.Internal
EmptyDisposable() EmptyDisposable()
{ {
UnityEngine.Debug.Log("EmptyDisposable.Instance created.");
} }
public void Dispose() public void Dispose()
{ {
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
// 空实现,无需释放资源
} }
} }
@ -546,33 +538,24 @@ namespace Cysharp.Threading.Tasks.Internal
public void Dispose() public void Dispose()
{ {
Dispose(true); lock (gate)
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{ {
lock (gate) if (parent != null)
{ {
if (parent != null) lock (parent.observerLock)
{ {
lock (parent.observerLock) var listObserver = parent.outObserver as ListObserver<T>;
if (listObserver != null)
{ {
var listObserver = parent.outObserver as ListObserver<T>; parent.outObserver = listObserver.Remove(unsubscribeTarget);
if (listObserver != null)
{
parent.outObserver = listObserver.Remove(unsubscribeTarget);
}
else
{
parent.outObserver = EmptyObserver<T>.Instance;
}
unsubscribeTarget = null;
parent = null;
} }
else
{
parent.outObserver = EmptyObserver<T>.Instance;
}
unsubscribeTarget = null;
parent = null;
} }
} }
} }
@ -648,15 +631,15 @@ namespace Cysharp.Threading.Tasks.Internal
} }
public void OnCompleted() public void OnCompleted()
{UnityEngine.Debug.Log("EmptyObserver<T>.OnCompleted"); {
} }
public void OnError(Exception error) public void OnError(Exception error)
{UnityEngine.Debug.Log("EmptyObserver<T>.OnError"); {
} }
public void OnNext(T value) public void OnNext(T value)
{UnityEngine.Debug.Log("EmptyObserver<T>.OnNext"); {
} }
} }
@ -670,7 +653,7 @@ namespace Cysharp.Threading.Tasks.Internal
} }
public void OnCompleted() public void OnCompleted()
{UnityEngine.Debug.Log("ThrowObserver<T>.OnCompleted"); {
} }
public void OnError(Exception error) public void OnError(Exception error)
@ -679,7 +662,7 @@ namespace Cysharp.Threading.Tasks.Internal
} }
public void OnNext(T value) public void OnNext(T value)
{UnityEngine.Debug.Log("ThrowObserver<T>.OnNext"); {
} }
} }

View File

@ -1,7 +1,6 @@
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
using System; using System;
using System.Text;
using System.Threading; using System.Threading;
namespace Cysharp.Threading.Tasks namespace Cysharp.Threading.Tasks
@ -11,65 +10,6 @@ namespace Cysharp.Threading.Tasks
public static class UniTaskScheduler public static class UniTaskScheduler
{ {
/// <summary>
/// 安全清理异常消息字符串,防止日志注入攻击
/// 清理控制字符、限制长度、转义特殊字符
/// </summary>
/// <param name="message">原始异常消息</param>
/// <param name="maxLength">最大长度限制默认1000字符</param>
/// <returns>清理后的安全消息字符串</returns>
internal static string SanitizeExceptionMessage(string message, int maxLength = 1000)
{
if (string.IsNullOrEmpty(message))
{
return string.Empty;
}
// 创建StringBuilder用于构建清理后的消息
var sanitized = new StringBuilder(message.Length);
// 遍历每个字符进行清理
for (int i = 0; i < message.Length && sanitized.Length < maxLength; i++)
{
char c = message[i];
// 保留可打印字符ASCII 32-126和常见的Unicode字符
// 移除控制字符ASCII 0-31除了换行符和制表符
if (char.IsControl(c) && c != '\n' && c != '\r' && c != '\t')
{
// 将控制字符替换为安全的转义表示
sanitized.Append($"\\u{(int)c:X4}");
}
else if (c == '\n')
{
// 将换行符替换为安全表示,防止日志注入
sanitized.Append("\\n");
}
else if (c == '\r')
{
// 将回车符替换为安全表示
sanitized.Append("\\r");
}
else if (c == '\t')
{
// 将制表符替换为空格
sanitized.Append(" ");
}
else
{
// 保留正常的可打印字符
sanitized.Append(c);
}
}
// 如果消息被截断,添加截断标记
if (message.Length > maxLength)
{
sanitized.Append("...[消息已截断]");
}
return sanitized.ToString();
}
public static event Action<Exception> UnobservedTaskException; public static event Action<Exception> UnobservedTaskException;
/// <summary> /// <summary>
@ -102,7 +42,7 @@ namespace Cysharp.Threading.Tasks
{ {
if (ex != null) if (ex != null)
{ {
if (ex is OperationCanceledException) if (!PropagateOperationCanceledException && ex is OperationCanceledException)
{ {
return; return;
} }
@ -130,9 +70,7 @@ namespace Cysharp.Threading.Tasks
string msg = null; string msg = null;
if (UnobservedExceptionWriteLogType != UnityEngine.LogType.Exception) if (UnobservedExceptionWriteLogType != UnityEngine.LogType.Exception)
{ {
// 使用安全清理方法处理异常消息,防止日志注入攻击 msg = "UnobservedTaskException: " + ex.ToString();
string safeExceptionMessage = SanitizeExceptionMessage(ex.ToString());
msg = "UnobservedTaskException: " + safeExceptionMessage;
} }
switch (UnobservedExceptionWriteLogType) switch (UnobservedExceptionWriteLogType)
{ {
@ -155,9 +93,7 @@ namespace Cysharp.Threading.Tasks
break; break;
} }
#else #else
// 使用安全清理方法处理异常消息,防止日志注入攻击 Console.WriteLine("UnobservedTaskException: " + ex.ToString());
string safeMessage = SanitizeExceptionMessage(ex.ToString());
Console.WriteLine("UnobservedTaskException: " + safeMessage);
#endif #endif
} }
} }

View File

@ -65,7 +65,7 @@ namespace Cysharp.Threading.Tasks
} }
finally finally
{ {
if (lockTaken) gate.Exit(false);
} }
} }
@ -97,7 +97,7 @@ namespace Cysharp.Threading.Tasks
} }
finally finally
{ {
gate.Exit(false); if (lockTaken) gate.Exit(false);
} }
} }
@ -125,7 +125,7 @@ namespace Cysharp.Threading.Tasks
} }
finally finally
{ {
gate.Exit(false); if (lockTaken) gate.Exit(false);
} }
} }
} }

View File

@ -13,7 +13,6 @@ namespace Cysharp.Threading.Tasks
{ {
public void Forget() public void Forget()
{ {
UnityEngine.Debug.Log("UniTaskVoid.Forget called.");
} }
} }
} }

View File

@ -259,8 +259,6 @@ namespace Cysharp.Threading.Tasks
public static UniTask<UnityEngine.Object> ToUniTask(this ResourceRequest asyncOperation, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false) public static UniTask<UnityEngine.Object> ToUniTask(this ResourceRequest asyncOperation, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false)
{ {
Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
// 注意CancellationToken 是结构体不能为null。default(CancellationToken) 是有效值,其 IsCancellationRequested 为 false
// 此处的访问是安全的,静态分析工具可能因为可选参数而误报
if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled<UnityEngine.Object>(cancellationToken); if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled<UnityEngine.Object>(cancellationToken);
if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.asset); if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.asset);
return new UniTask<UnityEngine.Object>(ResourceRequestConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, cancelImmediately, out var token), token); return new UniTask<UnityEngine.Object>(ResourceRequestConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, cancelImmediately, out var token), token);
@ -352,12 +350,12 @@ namespace Cysharp.Threading.Tasks
result.asyncOperation = asyncOperation; result.asyncOperation = asyncOperation;
result.progress = progress; result.progress = progress;
result.cancellationToken = cancellationToken; result.cancellationToken = cancellationToken;
result.cancelImmediately = false; result.cancelImmediately = cancelImmediately;
result.completed = false; result.completed = false;
asyncOperation.completed += result.continuationAction; asyncOperation.completed += result.continuationAction;
if (cancellationToken.CanBeCanceled) if (cancelImmediately && cancellationToken.CanBeCanceled)
{ {
result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state =>
{ {

View File

@ -344,16 +344,7 @@ namespace Cysharp.Threading.Tasks
public void Dispose() public void Dispose()
{ {
Dispose(true); innerEvent.RemoveListener(invokeDelegate);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
innerEvent.RemoveListener(invokeDelegate);
}
} }
} }
@ -417,13 +408,7 @@ namespace Cysharp.Threading.Tasks
public void Dispose() public void Dispose()
{ {
Dispose(true); if (!isDisposed)
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!isDisposed && disposing)
{ {
isDisposed = true; isDisposed = true;
TaskTracker.RemoveTracking(this); TaskTracker.RemoveTracking(this);
@ -533,13 +518,7 @@ namespace Cysharp.Threading.Tasks
public void Dispose() public void Dispose()
{ {
Dispose(true); if (!isDisposed)
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!isDisposed && disposing)
{ {
isDisposed = true; isDisposed = true;
TaskTracker.RemoveTracking(this); TaskTracker.RemoveTracking(this);