From b6b0b4000d897d3ff86600cd8ea0707067193377 Mon Sep 17 00:00:00 2001 From: Artem Perepelitsa Date: Sat, 16 Oct 2021 11:55:30 +0300 Subject: [PATCH] Add unobserved task exception publishing if an exception inside ExceptionResultSource is never thrown --- .../UniTask/Runtime/UniTask.Factory.cs | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/UniTask.Factory.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/UniTask.Factory.cs index c56cb03..2f6a6a9 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/UniTask.Factory.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/UniTask.Factory.cs @@ -194,6 +194,7 @@ namespace Cysharp.Threading.Tasks sealed class ExceptionResultSource : IUniTaskSource { readonly ExceptionDispatchInfo exception; + bool calledGet; public ExceptionResultSource(Exception exception) { @@ -202,6 +203,11 @@ namespace Cysharp.Threading.Tasks public void GetResult(short token) { + if (!calledGet) + { + calledGet = true; + GC.SuppressFinalize(this); + } exception.Throw(); } @@ -219,11 +225,20 @@ namespace Cysharp.Threading.Tasks { continuation(state); } + + ~ExceptionResultSource() + { + if (!calledGet) + { + UniTaskScheduler.PublishUnobservedTaskException(exception.SourceException); + } + } } sealed class ExceptionResultSource : IUniTaskSource { readonly ExceptionDispatchInfo exception; + bool calledGet; public ExceptionResultSource(Exception exception) { @@ -232,12 +247,22 @@ namespace Cysharp.Threading.Tasks public T GetResult(short token) { + if (!calledGet) + { + calledGet = true; + GC.SuppressFinalize(this); + } exception.Throw(); return default; } void IUniTaskSource.GetResult(short token) { + if (!calledGet) + { + calledGet = true; + GC.SuppressFinalize(this); + } exception.Throw(); } @@ -255,6 +280,14 @@ namespace Cysharp.Threading.Tasks { continuation(state); } + + ~ExceptionResultSource() + { + if (!calledGet) + { + UniTaskScheduler.PublishUnobservedTaskException(exception.SourceException); + } + } } sealed class CanceledResultSource : IUniTaskSource