dotnet / machinelearning

ML.NET is an open source and cross-platform machine learning framework for .NET.
https://dot.net/ml
MIT License
9.02k stars 1.88k forks source link

System.Threading.ThreadAbortException: "System error." #5591

Open CreedsCode opened 3 years ago

CreedsCode commented 3 years ago

System information

Issue

Source code / logs

System.Threading.ThreadAbortException
  HResult=0x80131530
  Message=System error.
  Source=System.Private.CoreLib
  StackTrace:
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
   at Microsoft.ML.Runtime.ComponentCatalog.LoadableClassInfo.CreateInstanceCore(Object[] ctorArgs)
   at Microsoft.ML.Runtime.ComponentCatalog.LoadableClassInfo.CreateInstance(IHostEnvironment env, Object args, Object[] extra)
   at Microsoft.ML.Runtime.ComponentCatalog.TryCreateInstance[TRes](IHostEnvironment env, Type signatureType, TRes& result, String name, String options, Object[] extra)
   at Microsoft.ML.Runtime.ComponentCatalog.TryCreateInstance[TRes,TSig](IHostEnvironment env, TRes& result, String name, String options, Object[] extra)
   at Microsoft.ML.ModelLoadContext.TryLoadModelCore[TRes,TSig](IHostEnvironment env, TRes& result, Object[] extra)
   at Microsoft.ML.ModelLoadContext.TryLoadModel[TRes,TSig](IHostEnvironment env, TRes& result, RepositoryReader rep, Entry ent, String dir, Object[] extra)
   at Microsoft.ML.ModelLoadContext.LoadModel[TRes,TSig](IHostEnvironment env, TRes& result, RepositoryReader rep, Entry ent, String dir, Object[] extra)
   at Microsoft.ML.ModelLoadContext.LoadModelOrNull[TRes,TSig](IHostEnvironment env, TRes& result, RepositoryReader rep, String dir, Object[] extra)
   at Microsoft.ML.ModelLoadContext.LoadModel[TRes,TSig](IHostEnvironment env, TRes& result, RepositoryReader rep, String dir, Object[] extra)
   at Microsoft.ML.ModelOperationsCatalog.Load(Stream stream, DataViewSchema& inputSchema)
   at Microsoft.ML.ModelOperationsCatalog.Load(String filePath, DataViewSchema& inputSchema)
   at ****.****.BaseProcessor.RLNModel.RLNModel.CreatePredictionEngine() in C:\Users\hoffm\source\repos\****:****\****.*****.BaseProcessor\RLNModel.cs:line 29
   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

Please paste or attach the code or logs or traces that would be helpful to diagnose the issue you are reporting.

public class RLNModel
    {
        private static Lazy<PredictionEngine<ModelInput, ModelOutput>> PredictionEngine = new Lazy<PredictionEngine<ModelInput, ModelOutput>>(CreatePredictionEngine);

        public static string _modelFile;

        public static ModelOutput Predict(ModelInput input) 
        {

            ModelOutput result = PredictionEngine.Value.Predict(input); // Throws here ################
            return result;
        }

        public static PredictionEngine<ModelInput, ModelOutput> CreatePredictionEngine()
        {
            MLContext mlContext = new MLContext();

            string userTempPath = Path.GetTempPath();
            string extractedModelsPath = userTempPath + "****.****.BaseProcessor\\"; 
            var fileDir = extractedModelsPath + "****.****.BaseProcessor.RLN.zip";

            ITransformer mlModel = mlContext.Model.Load(fileDir, out var modelInputSchema); # Line 29
            var predEngine = mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel);
            return predEngine;
        }
    }
michaelgsharp commented 3 years ago

Hi CreedsCode, is there a way for you to verify that the extraction finishes before the model loaded?

DanielStout5 commented 3 months ago

For anyone else arriving from Google: I wasn't actually seeing this with ML.Net at all, but I was seeing it in a different situation with Lazy and only in debug mode, and only when certain breakpoints are set. Seems like Lazy will throw ThreadAbortException when attempting to access the Value in debug mode sometimes.