Skip to content

Commit c5eac8a

Browse files
authored
Merge pull request #30 from Cysharp/hotfix/ThroughOperationCanceledException
OperationCanceledException by user code should not be handled.
2 parents c9bd9c8 + 8b8af7c commit c5eac8a

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

sandbox/SingleContainedApp/Program.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.IO;
1111
using System.Reflection;
1212
using System.Text;
13+
using System.Threading;
1314
using System.Threading.Tasks;
1415

1516
namespace SingleContainedApp
@@ -137,19 +138,36 @@ public class Person
137138
public string Name { get; set; }
138139
}
139140

141+
142+
public class ThrowOperationCanceledException : ConsoleAppBase
143+
{
144+
public async Task Throw()
145+
{
146+
//while (true)
147+
//{
148+
// await Task.Delay(10);
149+
// Context.CancellationToken.ThrowIfCancellationRequested();
150+
//}
151+
152+
var cts = new CancellationTokenSource();
153+
cts.Cancel();
154+
cts.Token.ThrowIfCancellationRequested();
155+
}
156+
}
157+
140158
class Program
141159
{
142160
static async Task Main(string[] args)
143161
{
144-
args = new[] { "-array", "10,20,30", "-person", @"{""Age"":10,""Name"":""foo""}" };
162+
//args = new[] { "-array", "10,20,30", "-person", @"{""Age"":10,""Name"":""foo""}" };
145163

146164

147165
await Host.CreateDefaultBuilder()
148166
.ConfigureLogging(logging =>
149167
{
150168
logging.SetMinimumLevel(LogLevel.Trace).ReplaceToSimpleConsole();
151169
})
152-
.RunConsoleAppFrameworkAsync<ComplexArgTest>(args);
170+
.RunConsoleAppFrameworkAsync<ThrowOperationCanceledException>(args);
153171
// .RunConsoleAppEngineAsync
154172
//.ConfigureServices((hostContext, services) =>
155173
//{

src/ConsoleAppFramework/ConsoleAppEngine.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,11 @@ async Task RunCore(ConsoleAppContext ctx, Type type, MethodInfo methodInfo, stri
164164
}
165165
catch (Exception ex)
166166
{
167-
if (ex is OperationCanceledException || ex is TaskCanceledException)
167+
if (ex is OperationCanceledException operationCanceledException && operationCanceledException.CancellationToken == cancellationToken)
168168
{
169-
return; // do nothing
169+
// NOTE: Do nothing if the exception has thrown by the CancellationToken of ConsoleAppEngine.
170+
// If the user code throws OperationCanceledException, ConsoleAppEngine should not handle that.
171+
return;
170172
}
171173

172174
if (ex is TargetInvocationException tex)

0 commit comments

Comments
 (0)