fix(coroutine): 修复协程完成状态处理中的异常情况

- 为 CoroutineCompletionStatus 枚举添加默认分支处理
- 抛出 ArgumentOutOfRangeException 以处理不支持的协程完成状态
- 防止因未知状态值导致的运行时错误
- 提高协程调度器的健壮性和错误处理能力
This commit is contained in:
GeWuYou 2026-04-05 15:18:29 +08:00
parent 03346fbfe7
commit ccffb121b3

View File

@ -140,20 +140,16 @@ public sealed class CoroutineScheduler(
/// <returns>包含所有活跃协程的快照列表。</returns> /// <returns>包含所有活跃协程的快照列表。</returns>
public IReadOnlyList<CoroutineSnapshot> GetActiveSnapshots() public IReadOnlyList<CoroutineSnapshot> GetActiveSnapshots()
{ {
var snapshots = new List<CoroutineSnapshot>(_metadata.Count); return _metadata
.Select(pair => pair.Value)
foreach (var pair in _metadata) .Select(meta => new
{
var slot = _slots[pair.Value.SlotIndex];
if (slot == null)
{ {
continue; Metadata = meta,
} Slot = _slots[meta.SlotIndex]
})
snapshots.Add(CreateSnapshot(pair.Value, slot)); .Where(item => item.Slot is not null)
} .Select(item => CreateSnapshot(item.Metadata, item.Slot!))
.ToArray();
return snapshots;
} }
/// <summary> /// <summary>
@ -689,6 +685,12 @@ public sealed class CoroutineScheduler(
case CoroutineCompletionStatus.Cancelled: case CoroutineCompletionStatus.Cancelled:
meta.State = CoroutineState.Cancelled; meta.State = CoroutineState.Cancelled;
break; break;
default:
throw new ArgumentOutOfRangeException(
nameof(completionStatus),
completionStatus,
"Unsupported coroutine completion status.");
} }
} }
@ -855,7 +857,7 @@ public sealed class CoroutineScheduler(
/// <param name="metadata">协程元数据。</param> /// <param name="metadata">协程元数据。</param>
/// <param name="slot">协程槽位。</param> /// <param name="slot">协程槽位。</param>
/// <returns>与当前槽位一致的只读快照。</returns> /// <returns>与当前槽位一致的只读快照。</returns>
private CoroutineSnapshot CreateSnapshot(CoroutineMetadata metadata, CoroutineSlot slot) private static CoroutineSnapshot CreateSnapshot(CoroutineMetadata metadata, CoroutineSlot slot)
{ {
return new CoroutineSnapshot( return new CoroutineSnapshot(
slot.Handle, slot.Handle,