{"record":{"id":"c7b7335c9dcf1219","repo":"dotnet/machinelearning","slug":"engine-parameter-engine-c7b733","errorCode":null,"errorMessage":"engine (Parameter 'engine')","messagePattern":"engine \\(Parameter 'engine'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.Extensions.ML/PredictionEnginePool.cs","lineNumber":148,"sourceCode":"        public void ReturnPredictionEngine(PredictionEngine<TData, TPrediction> engine)\n        {\n            ReturnPredictionEngine(string.Empty, engine);\n        }\n\n        /// <summary>\n        /// Returns a rented PredictionEngine to the pool.\n        /// </summary>\n        /// <param name=\"modelName\">\n        /// The name of the model which allows for uniquely identifying the model when\n        /// multiple models have the same <typeparamref name=\"TData\"/> and\n        /// <typeparamref name=\"TPrediction\"/> types.\n        /// </param>\n        /// <param name=\"engine\">The rented PredictionEngine.</param>\n        public void ReturnPredictionEngine(string modelName, PredictionEngine<TData, TPrediction> engine)\n        {\n            if (engine == null)\n            {\n                throw new ArgumentNullException(nameof(engine));\n            }\n\n            if (Volatile.Read(ref _disposed) != 0)\n            {\n                engine.Dispose();\n                return;\n            }\n\n            if (string.IsNullOrEmpty(modelName))\n            {\n                if (_defaultEnginePool != null)\n                {\n                    _defaultEnginePool.Return(engine);\n                }\n                else\n                {\n                    engine.Dispose();\n                }","sourceCodeStart":130,"sourceCodeEnd":166,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.Extensions.ML/PredictionEnginePool.cs#L130-L166","documentation":"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.","triggerScenarios":"Calling ReturnPredictionEngine(modelName, null) — commonly when GetPredictionEngine threw earlier and a finally block returns an unassigned variable.","commonSituations":"try/finally patterns where the engine rental failed; refactored code where the engine variable is never initialized.","solutions":["Null-check before returning","Only call ReturnPredictionEngine for engines actually obtained from GetPredictionEngine"],"exampleFix":"// before\nfinally { pool.ReturnPredictionEngine(\"m\", engine); }\n// after\nfinally { if (engine != null) pool.ReturnPredictionEngine(\"m\", engine); }","handlingStrategy":"type-guard","validationCode":"if (engine is null) return; // nothing to return","typeGuard":"bool IsValidEngine(PredictionEngine<TData,TPred> e) => e != null;","tryCatchPattern":"null // prevent by null-checking before the call","preventionTips":["Declare engine variable inside try and null-check in finally","Avoid returning engines that were never rented"],"tags":["null-argument","pooling"],"backgroundTag":"null-argument","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"}