diff --git a/backends/nxp/backend/neutron_converter_manager.py b/backends/nxp/backend/neutron_converter_manager.py index 7124929411e..a53a773f2ce 100644 --- a/backends/nxp/backend/neutron_converter_manager.py +++ b/backends/nxp/backend/neutron_converter_manager.py @@ -78,23 +78,35 @@ def convert(self, tflite_model: bytes, target: str) -> bytes: cctx.compilationOpts.minNumOpsPerGraph = 1 cctx.compilationOpts.excludeGraphPasses = "MergeTranspose" - logger = multiprocessing.log_to_stderr() - logger.setLevel(logging.WARNING) - queue = multiprocessing.Manager().Queue() + # Try to use multiprocessing for isolation, but fall back to direct execution + # if the environment doesn't support it (e.g., in sandcastle/build environments) + try: + logger = multiprocessing.log_to_stderr() + logger.setLevel(logging.WARNING) + queue = multiprocessing.Manager().Queue() + + process = multiprocessing.Process( + target=convert_unsafe, + args=(self.neutron_converter, tflite_model, cctx, queue), + ) + process.start() + process.join() # waits until the subprocess is complete - process = multiprocessing.Process( - target=convert_unsafe, - args=(self.neutron_converter, tflite_model, cctx, queue), - ) - process.start() - process.join() # waits until the subprocess is complete + if queue.empty(): # signals the unsafe task did not run till the end + raise RuntimeError( + f"Neutron converter module terminated unexpectedly with exit code {process.exitcode}" + ) - if queue.empty(): # signals the unsafe task did not run till the end - raise RuntimeError( - f"Neutron converter module terminated unexpectedly with exit code {process.exitcode}" + model_converted = queue.get() + process.close() + except (EOFError, OSError) as e: + # Multiprocessing failed (likely due to environment restrictions) + # Fall back to direct execution + logging.warning( + f"Multiprocessing not available ({e}), running neutron converter directly" + ) + model_converted = self.neutron_converter.convertModel( + list(tflite_model), cctx ) - model_converted = queue.get() - - process.close() return bytes(model_converted)