Skip to content

RunOnThreadPool: cancelling at the wrong time prevents returning to the main thread #669

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 16 additions & 18 deletions src/UniTask/Assets/Plugins/UniTask/Runtime/UniTask.Run.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,11 @@ public static async UniTask RunOnThreadPool(Action action, bool configureAwait =

await UniTask.SwitchToThreadPool();

cancellationToken.ThrowIfCancellationRequested();

if (configureAwait)
{
try
{
cancellationToken.ThrowIfCancellationRequested();
action();
}
finally
Expand All @@ -81,6 +80,7 @@ public static async UniTask RunOnThreadPool(Action action, bool configureAwait =
}
else
{
cancellationToken.ThrowIfCancellationRequested();
action();
}

Expand All @@ -94,12 +94,11 @@ public static async UniTask RunOnThreadPool(Action<object> action, object state,

await UniTask.SwitchToThreadPool();

cancellationToken.ThrowIfCancellationRequested();

if (configureAwait)
{
try
{
cancellationToken.ThrowIfCancellationRequested();
action(state);
}
finally
Expand All @@ -109,6 +108,7 @@ public static async UniTask RunOnThreadPool(Action<object> action, object state,
}
else
{
cancellationToken.ThrowIfCancellationRequested();
action(state);
}

Expand All @@ -122,12 +122,11 @@ public static async UniTask RunOnThreadPool(Func<UniTask> action, bool configure

await UniTask.SwitchToThreadPool();

cancellationToken.ThrowIfCancellationRequested();

if (configureAwait)
{
try
{
cancellationToken.ThrowIfCancellationRequested();
await action();
}
finally
Expand All @@ -137,6 +136,7 @@ public static async UniTask RunOnThreadPool(Func<UniTask> action, bool configure
}
else
{
cancellationToken.ThrowIfCancellationRequested();
await action();
}

Expand All @@ -150,12 +150,11 @@ public static async UniTask RunOnThreadPool(Func<object, UniTask> action, object

await UniTask.SwitchToThreadPool();

cancellationToken.ThrowIfCancellationRequested();

if (configureAwait)
{
try
{
cancellationToken.ThrowIfCancellationRequested();
await action(state);
}
finally
Expand All @@ -165,6 +164,7 @@ public static async UniTask RunOnThreadPool(Func<object, UniTask> action, object
}
else
{
cancellationToken.ThrowIfCancellationRequested();
await action(state);
}

Expand All @@ -178,12 +178,11 @@ public static async UniTask<T> RunOnThreadPool<T>(Func<T> func, bool configureAw

await UniTask.SwitchToThreadPool();

cancellationToken.ThrowIfCancellationRequested();

if (configureAwait)
{
try
{
cancellationToken.ThrowIfCancellationRequested();
return func();
}
finally
Expand All @@ -194,6 +193,7 @@ public static async UniTask<T> RunOnThreadPool<T>(Func<T> func, bool configureAw
}
else
{
cancellationToken.ThrowIfCancellationRequested();
return func();
}
}
Expand All @@ -205,23 +205,22 @@ public static async UniTask<T> RunOnThreadPool<T>(Func<UniTask<T>> func, bool co

await UniTask.SwitchToThreadPool();

cancellationToken.ThrowIfCancellationRequested();

if (configureAwait)
{
try
{
cancellationToken.ThrowIfCancellationRequested();
return await func();
}
finally
{
cancellationToken.ThrowIfCancellationRequested();
await UniTask.Yield();
cancellationToken.ThrowIfCancellationRequested();
}
}
else
{
cancellationToken.ThrowIfCancellationRequested();
var result = await func();
cancellationToken.ThrowIfCancellationRequested();
return result;
Expand All @@ -235,12 +234,11 @@ public static async UniTask<T> RunOnThreadPool<T>(Func<object, T> func, object s

await UniTask.SwitchToThreadPool();

cancellationToken.ThrowIfCancellationRequested();

if (configureAwait)
{
try
{
cancellationToken.ThrowIfCancellationRequested();
return func(state);
}
finally
Expand All @@ -251,6 +249,7 @@ public static async UniTask<T> RunOnThreadPool<T>(Func<object, T> func, object s
}
else
{
cancellationToken.ThrowIfCancellationRequested();
return func(state);
}
}
Expand All @@ -262,23 +261,22 @@ public static async UniTask<T> RunOnThreadPool<T>(Func<object, UniTask<T>> func,

await UniTask.SwitchToThreadPool();

cancellationToken.ThrowIfCancellationRequested();

if (configureAwait)
{
try
{
cancellationToken.ThrowIfCancellationRequested();
return await func(state);
}
finally
{
cancellationToken.ThrowIfCancellationRequested();
await UniTask.Yield();
cancellationToken.ThrowIfCancellationRequested();
}
}
else
{
cancellationToken.ThrowIfCancellationRequested();
var result = await func(state);
cancellationToken.ThrowIfCancellationRequested();
return result;
Expand Down