Encountering error: "An application can only use one MCR component that uses Parallel Computing Toolbox functionality"
Show older comments
I have an application where I'm:
- invoking the Matlab Compiler Runtime (MCR) from a .NET (C#) Windows Service
- interacting with it via a Matlab handle class, "AosEmbeddedRuntimeEngine"
- using the Parallel Computing Toolbox within that class to run several worker processes within a parallel pool.
- occassionally attempting to restart the MCR (rather than the full Windows service) in order to address an internal memory leak I've observed within the MCR (a fix for which is anticipated in the next Matlab release)
Everything is working well when I first start the service: the parallel pool is starting successfully, and the workers are successfully processing what they're supposed to, and this will keep running for several days. However, when I attempt to restart the "MATLABRuntime" within my .NET application (service) the portion of the Matlab code that was initially successful now fails when it attempts to acquire the (local) Parallel Cluster for the Parallel Pool. Specifically, I encounter the following error:
Error using parallel.listProfiles (line 42)
An application can only use one MCR component that uses Parallel Computing Toolbox functionality.
Again, I only run into this after I attempt to restart the MCR. It works fine the first time. So, it would appear that, perhaps, the original MCR instance hasn't been properly cleaned up. During my restart of the MCR, I do the following:
- successfully call a method of my Matlab class to stop and cleanup the parallel pool
- successfully delete my Matlab class (AosEmbeddedRuntimeEngine)
- successfully call "WaitForFiguresToClose" (though there is no UI component) on the MATLABRuntime object
- successfully call "Dispose" on the MATLABRuntime object. I traced into this and verified that it's "Dispose(bool disposing)" override in .NET was successfully invoked and, itself, called RuntimeInterop.TerminateRuntime(_matlabHandle).
So, after all of this, I would expect that the original MCR should have been cleaned up, and so when I recreate a new one, the new one should be the only MCR component embedded within my application. Instead, I'm running into the error noted above after I instantiate the new MCR and attempt to reinitialize the parallel cluster. Would anyone have any insight as to what might be going on here ... and how I might get around this?
Thanks in advance.
The following is the C# code used to stop the MATLABRuntime object (of which no errors are logged):
public async Task StopMatlabSessionAsync()
{
if (IsDisposed || _matlabRuntime is null)
{
_logger.LogDebug("Nothing to do: the Matlab runtime has already been deleted.");
return;
}
bool wasStoppingOrCleaningUp = this.IsStoppingOrCleaningUp;
try
{
this.IsStoppingOrCleaningUp = true;
if (IsAtLeastPartiallyInitialized)
{
await CleanupAosRuntimeEngineAsync();
}
_logger.LogDebug("Stopping and disposing the Matlab Runtime ...");
_matlabRuntime.WaitForFiguresToClose();
_matlabRuntime.Dispose();
_matlabRuntime = null;
_logger.LogDebug("Successfully stopped/disposed the Matlab Runtime.");
}
catch (Exception ex)
{
_logger.LogError(ex, "Encountered an exception while stopping/disposing the Matlab runtime!");
}
finally
{
this.IsStoppingOrCleaningUp = wasStoppingOrCleaningUp;
}
}
Answers (0)
Categories
Find more on .NET Client Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!