dotnet/machinelearning · error · ArgumentNullException

engine (Parameter 'engine')

Error message

engine (Parameter 'engine')

What it means

ArgumentNullException in PredictionEnginePool.ReturnPredictionEngine: the engine argument (possibly forwarded from the parameterless overload) is null. The method looks the engine up in the rented-engines map to return it to the correct model generation; a null engine has no rental record and is rejected, with 'engine' named as the parameter at fault.

Source

Thrown at src/Microsoft.Extensions.ML/PredictionEnginePool.cs:148

        public void ReturnPredictionEngine(PredictionEngine<TData, TPrediction> engine)
        {
            ReturnPredictionEngine(string.Empty, engine);
        }

        /// <summary>
        /// Returns a rented PredictionEngine to the pool.
        /// </summary>
        /// <param name="modelName">
        /// The name of the model which allows for uniquely identifying the model when
        /// multiple models have the same <typeparamref name="TData"/> and
        /// <typeparamref name="TPrediction"/> types.
        /// </param>
        /// <param name="engine">The rented PredictionEngine.</param>
        public void ReturnPredictionEngine(string modelName, PredictionEngine<TData, TPrediction> engine)
        {
            if (engine == null)
            {
                throw new ArgumentNullException(nameof(engine));
            }

            if (Volatile.Read(ref _disposed) != 0)
            {
                engine.Dispose();
                return;
            }

            if (string.IsNullOrEmpty(modelName))
            {
                if (_defaultEnginePool != null)
                {
                    _defaultEnginePool.Return(engine);
                }
                else
                {
                    engine.Dispose();
                }

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Null-check before returning
  2. Only call ReturnPredictionEngine for engines actually obtained from GetPredictionEngine

Example fix

// before
finally { pool.ReturnPredictionEngine("m", engine); }
// after
finally { if (engine != null) pool.ReturnPredictionEngine("m", engine); }
Defensive patterns

Strategy: type-guard

Validate before calling

if (engine is null) return; // nothing to return

Type guard

bool IsValidEngine(PredictionEngine<TData,TPred> e) => e != null;

Try / catch

null // prevent by null-checking before the call

Prevention

When it happens

Trigger: Calling ReturnPredictionEngine(modelName, null) — commonly when GetPredictionEngine threw earlier and a finally block returns an unassigned variable.

Common situations: try/finally patterns where the engine rental failed; refactored code where the engine variable is never initialized.

Related errors


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