dotnet/machinelearning · warning · OperationCanceledException

OperationCanceledException

Error message

OperationCanceledException

What it means

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.

Source

Thrown at src/Microsoft.ML.AutoML/AutoMLExperiment/Runner/SweepablePipelineRunner.cs:108

            throw new ArgumentException("IDatasetManager must be either ITrainTestDatasetManager or ICrossValidationDatasetManager");
        }

        public Task<TrialResult> RunAsync(TrialSettings settings, CancellationToken ct)
        {
            try
            {
                using (var ctRegistration = ct.Register(() =>
                {
                    _mLContext?.CancelExecution();
                }))
                {
                    return Task.FromResult(Run(settings));
                }
            }
            catch (Exception ex) when (ct.IsCancellationRequested)
            {
                throw new OperationCanceledException(ex.Message, ex.InnerException);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void Dispose()
        {
            _mLContext!.CancelExecution();
            _mLContext = null;
        }
    }
}

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Inspect ex.InnerException to find the underlying failure if cancellation was not intended
  2. Check ct.IsCancellationRequested to confirm cancellation was expected; if so, treat as normal stop
  3. Avoid cancelling the token before trials complete, or drain running trials gracefully
  4. If a genuine training error was masked, rerun without cancelling to surface the real exception

Example fix

// before
try { var r = await runner.RunAsync(settings, ct); }
catch (Exception e) { Log.Error(e.Message); }
// after
try { var r = await runner.RunAsync(settings, ct); }
catch (OperationCanceledException) when (ct.IsCancellationRequested) { Log.Info("Trial cancelled"); }
catch (Exception e) { Log.Error(e.InnerException?.Message ?? e.Message); }
Defensive patterns

Strategy: try-catch

Validate before calling

if (ct.IsCancellationRequested) return; // don't start trials already cancelled

Try / catch

try { var r = await runner.RunAsync(settings, ct); }
catch (OperationCanceledException oce) when (ct.IsCancellationRequested)
{
    // expected cancellation; inspect oce.InnerException for masked errors
}

Prevention

When it happens

Trigger: 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.

Common situations: 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).

Understand the failure class

Background: Request timed out: what client-side request timeouts mean across libraries (Request timed out, TIMED_OUT, APITimeoutError) — this error's family across 39 libraries.

Related errors


AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11). Data as JSON: /api/errors/0a4205a962b51bd1. Report an issue: GitHub.