{"record":{"id":"0a4205a962b51bd1","repo":"dotnet/machinelearning","slug":"operationcanceledexception-0a4205","errorCode":null,"errorMessage":"OperationCanceledException","messagePattern":"OperationCanceledException","errorType":"exception","errorClass":"OperationCanceledException","httpStatus":null,"severity":"warning","filePath":"src/Microsoft.ML.AutoML/AutoMLExperiment/Runner/SweepablePipelineRunner.cs","lineNumber":108,"sourceCode":"\n            throw new ArgumentException(\"IDatasetManager must be either ITrainTestDatasetManager or ICrossValidationDatasetManager\");\n        }\n\n        public Task<TrialResult> RunAsync(TrialSettings settings, CancellationToken ct)\n        {\n            try\n            {\n                using (var ctRegistration = ct.Register(() =>\n                {\n                    _mLContext?.CancelExecution();\n                }))\n                {\n                    return Task.FromResult(Run(settings));\n                }\n            }\n            catch (Exception ex) when (ct.IsCancellationRequested)\n            {\n                throw new OperationCanceledException(ex.Message, ex.InnerException);\n            }\n            catch (Exception)\n            {\n                throw;\n            }\n        }\n\n        public void Dispose()\n        {\n            _mLContext!.CancelExecution();\n            _mLContext = null;\n        }\n    }\n}\n","sourceCodeStart":90,"sourceCodeEnd":123,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.ML.AutoML/AutoMLExperiment/Runner/SweepablePipelineRunner.cs#L90-L123","documentation":"SweepablePipelineRunner.RunAsync wraps trial execution; when the cancellation token is requested and the trial body throws, it rethrows as OperationCanceledException to signal cooperative cancellation. The message is the inner exception's message (or generic), so the real failure cause is preserved as the inner exception. This is the standard .NET cancellation-propagation pattern.","triggerScenarios":"The CancellationToken passed to RunAsync is cancelled (experiment stop/timeout) while Run(settings) or trial evaluation is in progress and throws; the catch filter `when (ct.IsCancellationRequested)` routes any exception into OperationCanceledException.","commonSituations":"Cancelling an AutoML experiment via its cancellation token or a timeout; host shutting down mid-trial; an underlying ML.NET failure occurring at the same moment cancellation was requested (masking the true error).","solutions":["Inspect ex.InnerException to find the underlying failure if cancellation was not intended","Check ct.IsCancellationRequested to confirm cancellation was expected; if so, treat as normal stop","Avoid cancelling the token before trials complete, or drain running trials gracefully","If a genuine training error was masked, rerun without cancelling to surface the real exception"],"exampleFix":"// before\ntry { var r = await runner.RunAsync(settings, ct); }\ncatch (Exception e) { Log.Error(e.Message); }\n// after\ntry { var r = await runner.RunAsync(settings, ct); }\ncatch (OperationCanceledException) when (ct.IsCancellationRequested) { Log.Info(\"Trial cancelled\"); }\ncatch (Exception e) { Log.Error(e.InnerException?.Message ?? e.Message); }","handlingStrategy":"try-catch","validationCode":"if (ct.IsCancellationRequested) return; // don't start trials already cancelled","typeGuard":null,"tryCatchPattern":"try { var r = await runner.RunAsync(settings, ct); }\ncatch (OperationCanceledException oce) when (ct.IsCancellationRequested)\n{\n    // expected cancellation; inspect oce.InnerException for masked errors\n}","preventionTips":["Always catch OperationCanceledException separately from general exceptions","Inspect InnerException for the true root cause","Pass linked cancellation tokens deliberately and log cancellations"],"tags":["dotnet","ml-automl","cancellation"],"backgroundTag":"request-timeout","analyzedSha":"7b76e69cf964daeca3f1377af6bc5543284d56c6","analyzedAt":"2026-09-11T12:35:38.930Z","contentChangedAt":"2026-09-11T12:35:38.930Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}